<template>
  <admin-layout>
    <a-row :gutter="10">
      <a-col :md="6" :sm="24">
        <a-card :bordered="false">
          <div>
            <a-input-search
              @search="onSearch"
              style="width: 100%; margin-top: 10px"
              placeholder="请输入部门名称"
              class="mb10"
            />
            <!-- 树-->

            <template>
              <!--组织机构-->
              <a-tree
                showLine
                :selectedKeys="selectedKeys"
                :expandedKeys.sync="iExpandedKeys"
                :checkStrictly="true"
                @select="onSelect"
                :dropdownStyle="{ maxHeight: '200px', overflow: 'auto' }"
                :treeData="departTree"
              />
            </template>
          </div>
        </a-card>
      </a-col>
      <a-col :md="18" :sm="24">
        <a-card :bordered="false">
          <a-tabs defaultActiveKey="2">
            <a-tab-pane tab="基本信息" key="1" forceRender>
              <DeptBaseInfo ref="DeptBaseInfo"></DeptBaseInfo>
            </a-tab-pane>
            <a-tab-pane tab="用户信息" key="2">
              <DeptUserInfo ref="DeptUserInfo"></DeptUserInfo>
            </a-tab-pane>
          </a-tabs>
        </a-card>
      </a-col>
    </a-row>
  </admin-layout>
</template>

<script>
import DeptBaseInfo from './modules/staff/DeptBaseInfo'
import DeptUserInfo from './modules/staff/DeptUserInfo'
import { queryDepartTreeList, searchByKeywords } from './api/services/staff'
import { PageListMixin } from '@/mixins/PageListMixin'

export default {
  name: 'DepartStaffList',
  components: {
    DeptBaseInfo,
    DeptUserInfo
  },
  mixins: [PageListMixin],
  data() {
    return {
      currentDeptId: '',
      iExpandedKeys: [],
      loading: false,
      autoExpandParent: true,
      currFlowId: '',
      currFlowName: '',
      disable: true,
      treeData: [],
      visible: false,
      departTree: [],
      rightClickSelectedKey: '',
      hiding: true,
      model: {},
      dropTrigger: '',
      depart: {},
      disableSubmit: false,
      checkedKeys: [],
      selectedKeys: [],
      autoIncr: 1,
      currSelected: {},
      form: this.$form.createForm(this),
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      graphDatasource: {
        nodes: [],
        edges: []
      }
    }
  },
  created() {
    this.currFlowId = this.$route.params.id
    this.currFlowName = this.$route.params.name
    // this.loadTree()
  },
  methods: {
    loadData() {
      this.refresh()
    },
    loadTree() {
      var that = this
      that.treeData = []
      that.departTree = []
      queryDepartTreeList().then(res => {
        if (res.code === '200') {
          for (let i = 0; i < res.value.length; i++) {
            let temp = res.value[i]
            that.treeData.push(temp)
            that.departTree.push(temp)
            that.setThisExpandedKeys(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.iExpandedKeys.push(node.children[a])
          // this.setThisExpandedKeys(node.children[a])
        }
      }
    },
    refresh() {
      this.loading = true
      this.loadTree()
    },
    onExpand(expandedKeys) {
      // if not set autoExpandParent to false, if children expanded, parent can not collapse.
      // or, you can remove all expanded children keys.
      this.iExpandedKeys = expandedKeys
      this.autoExpandParent = false
    },
    onSearch(value) {
      let that = this
      if (value) {
        searchByKeywords({ keyWord: value }).then(res => {
          if (res.code === '200') {
            that.departTree = []
            for (let i = 0; i < res.value.length; i++) {
              let temp = res.value[i]
              that.departTree.push(temp)
            }
          } else {
            that.$message.warning(res.message)
          }
        })
      } else {
        that.loadTree()
      }
    },
    onCheck(checkedKeys, e) {
      let record = e.node.dataRef
      this.checkedKeys = []
      // if (e.checked === true) {
      this.currentDeptId = record.id
      this.checkedKeys.push(record.id)

      this.$refs.DeptBaseInfo.open(record)
      this.$refs.DeptUserInfo.open(record)
      // }
      // else {
      //   this.checkedKeys = [];
      //   this.$refs.DeptBaseInfo.clearForm();
      //   this.$refs.DeptUserInfo.clearList();
      // }

      this.hiding = false
      // this.checkedKeys = checkedKeys.checked
    },
    onSelect(selectedKeys, e) {
      if (this.selectedKeys[0] !== selectedKeys[0]) {
        this.selectedKeys = [selectedKeys[0]]
      }
      let record = e.node.dataRef
      this.checkedKeys.push(record.id)
      this.$refs.DeptBaseInfo.open(record)
      this.$refs.DeptUserInfo.onClearSelected()
      this.$refs.DeptUserInfo.open(record)
    }
  }
}
</script>
