<template>
  <div class="box">
    <el-card class="tree-box" shadow="always" :body-style="{ padding: '5px' }" ref='cardTree'>
      <div slot="header" class="clearfix">
        <span v-show="!status">选择库房</span>
        <el-button style="float: right; padding:  0" type="text" @click='btns'>
          <el-tooltip class="item" effect="dark" v-if="!status" content="折叠" placement="right">
            <svg-icon icon-class="leftzd" v-if="!status" />
          </el-tooltip>
          <el-tooltip class="item" effect="dark" v-if="status" content="展开" placement="left">
            <svg-icon icon-class="rightzd" v-if="status" />
          </el-tooltip>
        </el-button>
      </div>
      <div class="treeClass">
        <el-scrollbar wrap-style="overflow-x:hidden;" style="height: calc(100vh - 165px)">
        <el-tree v-loading="treeLoading" highlight-current :data="treeData" :props="defaultProps"
          :expand-on-click-node="false" @node-click="handleNodeClick">
          <span slot-scope="{ node, data }" class="custom-tree-node">
            <!--可以根据情况，以数据类型来显示不同的图标或图片-->
            <svg-icon icon-class="wjj" style="margin-right: 5px; color: rgb(255, 207, 92)"
              v-if="data.childs && data.childs.length > 0" class="iconfont icon-folder-open-fill" />
            <svg-icon icon-class="wj" style="margin-right: 5px; color: rgb(255, 207, 92)" v-else
              class="iconfont icon-wenjian" />
            <span>{{ node.label }}</span>
          </span>
        </el-tree>
        </el-scrollbar>
      </div>
    </el-card>
    <div class="resize" title="收缩侧边栏">
      ⋮
    </div>
    <el-card class="form-box" ref='cardForm'>
      <div slot="header" class="clearfix">
        <span>采集点设置</span>
      </div>
      <right-form ref="rightFoem" :treeRow='treeRow' :treeNode='treeNode' :showButtons="showButtons"
        @handleNodeClick='handleNodeClick' @getTree='getTree' @cancels='cancels' />
    </el-card>

  </div>
</template>
<script>
  import {
    dragControllerDiv
  } from '@/utils/common';
  import rightForm from "./components/rightForm.vue"
  import {
    mapState
  } from "vuex";
  export default {
    data() {
      return {
        status: false,
        defaultProps: {
          children: 'childs',
          label: 'gathername'
        },
        treeRow: {},
        treeNode: {}
      }
    },
    components: {
      rightForm,
    },
    watch: {
      'status': {
        handler(val, oldVal) {
          const cardTree = this.$refs.cardTree.$el.style
          const cardForm = this.$refs.cardForm.$el.style
          if (val) {
            cardTree.width = '5%'
            cardForm.width = '94%'
          } else {
            cardTree.width = '22%';
            cardForm.width = '77%';
          }
        }
      }
    },
    computed: {
      ...mapState({
        treeData: (state) => state.collectionPoint.treeData,
        showButtons: (state) => state.collectionPoint.showButtons,
      }),
      treeLoading: {
        get() {
          return this.$store.state.collectionPoint.treeLoading;
        },
        set(val) {
          this.$store.state.collectionPoint.treeLoading = val;
        },
      },
    },
    methods: {
      btns() {
        this.status = !this.status
      },
      getTree() {
        this.treeLoading = true
        this.$store.dispatch('collectionPoint/getPointTree', {}).catch(err => {
          this.treeLoading = false
        })
      },
      handleNodeClick(tab, node) {
        this.treeRow = tab
        this.treeNode = node
        //  form: {
        //   fatherName: "",
        //   gathercode: "",
        //   gathername: "",
        //   id: "",
        //   storeseq: "",
        // },
        this.$refs.rightFoem.form.fatherName = node.parent.label
        this.$refs.rightFoem.form.gathercode = tab.gathercode
        this.$refs.rightFoem.form.gathername = tab.gathername
        this.$refs.rightFoem.form.id = tab.id
        this.$refs.rightFoem.form.storeseq = tab.storeseq
      },
      cancels() {
        this.treeRow = {}
        this.treeNode = {}
      }
    },
    mounted() {
      dragControllerDiv()
    },
    created() {
      let btnData = {
        menuid: this.$getMenuId(sessionStorage.getItem("routerIdList"), this.$route.path),
        roleid: sessionStorage.getItem("roleid")
      }
      this.$store.dispatch("collectionPoint/getButtonList", btnData);
      this.getTree()
    }
  }

</script>
<style lang='scss' scoped>
  .box {
    display: flex;
    justify-content: space-around;
    height: 100%;

    .tree-box {
      width: 22%;
      // height: 720px;
      overflow-y: auto;

      .treeClass {
        padding: 0 10px
      }

      .el-tree {
        width: 100%;
        margin-top: 10px;
      }

      .clearfix {
        font-weight: 600;
        font-size: 17px;
        letter-spacing: 3px;
      }

    }

    .form-box {
      width: 77%;
      height: calc(100vh - 95px);
      overflow-y: auto;

      .clearfix {
        font-weight: 600;
        font-size: 17px;
        letter-spacing: 3px;
      }

    }

  }

</style>
