<template>
  <a-card title="部门">
    <div slot="extra" class="drawer-bootom-button">
      <a-dropdown :trigger="['click']" placement="topCenter">
        <a-menu slot="overlay">
          <a-menu-item key="5" @click="expandAll">展开所有</a-menu-item>
          <a-menu-item key="6" @click="closeAll">合并所有</a-menu-item>
        </a-menu>
        <a-button>
          树操作
          <a-icon type="up" />
        </a-button>
      </a-dropdown>
    </div>
    <div style="background: #fff;">
      <a-col :md="10" :sm="24">
        <template>
          <a-tree
            :show-line="true"
            :selected-keys="selectedKeys"
            :tree-data="treeData"
            :check-strictly="checkStrictly"
            :expanded-keys="iExpandedKeys"
            :auto-expand-parent="autoExpandParent"
            @select="onSelect"
            @expand="onExpand"
            :replaceFields="{children:'children', title:'label', key:'id', value: 'id' }"
          />
        </template>
      </a-col>
    </div>
  </a-card>
</template>

<script>
import { getAction } from '@/api/request'

export default {
  name: 'LeftTree',
  components: {},
  data() {
    return {
      iExpandedKeys: [100],
      loading: false,
      autoExpandParent: true,
      treeData: [],
      hiding: true,
      selectedKeys: [],
      currSelected: {},
      allTreeKeys: [],
      checkStrictly: true,
      url: {
        tree: '/system/dept/treeSelect'
      },
      orgCategoryDisabled: false
    }
  },
  computed: {},
  created() {
    this.loadTree()
  },
  methods: {
    loadData() {
      this.refresh()
    },
    loadTree() {
      var that = this
      that.treeData = []
      getAction(this.url.tree).then((res) => {
        if (res.code === 200) {
          // 部门全选后，再添加部门，选中数量增多
          this.allTreeKeys = []
          for (let i = 0; i < res.data.length; i++) {
            const temp = res.data[i]
            that.treeData.push(temp)
            that.setThisExpandedKeys(temp)
            that.getAllKeys(temp)
          }
          this.loading = false
        }
      })
    },
    setThisExpandedKeys(node) {
      if (node.children && node.children.length > 0) {
        this.iExpandedKeys.push(node.key)
        for (let a = 0; a < node.children.length; a++) {
          this.setThisExpandedKeys(node.children[a])
        }
      }
    },
    refresh() {
      this.loading = true
      this.loadTree()
    },
    onExpand(expandedKeys) {
      console.log('onExpand', expandedKeys)
      this.iExpandedKeys = expandedKeys
      this.autoExpandParent = false
    },
    onSelect(selectedKeys, e) {
      console.log('selected', selectedKeys, e)
      this.hiding = false
      const record = e.node.dataRef
      console.log('onSelect-record', record)
      this.currSelected = Object.assign({}, record)
      this.selectedKeys = [record.key]
      this.$emit('selected', this.currSelected)
    },
    onClearSelected() {
      this.hiding = true
      this.currSelected = {}
      this.selectedKeys = []
    },
    // ---- author:os_chengtgen -- date:20190827 --  for:切换父子勾选模式 =======------
    expandAll() {
      this.iExpandedKeys = this.allTreeKeys
    },
    closeAll() {
      this.iExpandedKeys = []
    },
    getAllKeys(node) {
      this.allTreeKeys.push(node.id)
      if (node.children && node.children.length > 0) {
        for (let a = 0; a < node.children.length; a++) {
          this.getAllKeys(node.children[a])
        }
      }
    }
    // ---- author:os_chengtgen -- date:20190827 --  for:切换父子勾选模式 =======------

  }
}
</script>

<style lang="less" scoped>
  .ant-card {
    height: 100%;

    /deep/ .ant-card-head {
      padding: 0 15px;

      /deep/ .ant-card-head-title, /deep/ .ant-card-extra {
        padding: 10px 0;
      }
    }

    /deep/ .ant-card-body {
      padding: 10px;
      height: calc(100vh - 260px);
      overflow: auto;
    }
  }
</style>
