<!--
  @description: 标段分类选择弹框
  @Modifier:
  @routerPath:
  @date: 2021/8/8-23:27
-->
<template>
  <el-dialog
    v-dialogDrag
    :visible.sync="dialogData.dialog"
    :close-on-click-modal="false"
    title="选择分类"
    width="700px"
    append-to-body
    custom-class="el-dialog__body-p8 el-dialog_table"
  >
    <div class="tools p0-16 pt16 clearfix">
      <el-form ref="searchForm" :model="searchForm" inline>
        <el-form-item label="选择专业" prop="specialtyName">
          <el-input
            v-model="filterText"
            placeholder="输入关键字进行过滤"
          />
        </el-form-item>
      </el-form>
    </div>

    <div class="main mb10">
      <el-scrollbar style="height:400px">
        <!-- <el-tree
          ref="treeSelect"
          :props="defaultProps"
          :load="treeLoadNode"
          :default-checked-keys="dialogData.selectCodes"
          show-checkbox
          check-strictly
          lazy
          node-key="id"
          @check="check"
          default-expand-all
        /> -->
        <el-tree
          ref="treeSelect"
          v-loading="loading"
          check-on-click-node
          class="filter-tree"
          :data="dataTree"
          show-checkbox
          :default-checked-keys="dialogData.selectCodes"
          check-strictly
          :props="defaultProps"
          node-key="id"
          :filter-node-method="filterNode"
          @check="check"
        />
      </el-scrollbar>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="ensure">确 定</el-button>
      <el-button @click="dialogData.dialog = false">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
/* 接口*/
import { queryExpertSepcialLisByUserId } from '@/api/expert/expertEvaluationInfo' //
import { queryExpertSepcialTree } from '@/api/expert/expertscloud'
/* 混合开发方法-参数字段*/
import { ListMixin } from '@/mixins/ListMixin' // 分页
export default {
  name: 'BidSelectworkunit',
  components: {
  },
  mixins: [ListMixin],
  props: {
    dialogData: {
      type: Object,
      default: function() {
        return {
          dialog: false, // 弹窗状态
          selectCodes: [] // 当前选中的专业Code
        }
      }
    }

  },
  data() {
    return {
      loading: true,
      organizationDialog: false, // 弹框打开状态
      // 搜索信息
      searchForm: {
        specialtyName: ''
      },
      filterText: '',
      currentData: {}, // 当前选择的信息
      data: [],
      dataTree: [],
      specialtyType: '',
      defaultProps: {
        children: 'children',
        label: 'label',
        isLeaf: 'leaf'
      }
    }
  },
  watch: {
    filterText(val) {
      this.$refs.treeSelect.filter(val)
    },
    // 监听-弹框展示状态
    'dialogData.dialog'(val) {
      if (val) {
        // 弹框已打开
        this.initData()
      }
    }
  },
  methods: {
    initData() {
      this.filterText = ''
      queryExpertSepcialTree().then(res => {
        this.loading = false
        this.dataTree = res.data
      })
    },
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    openDialog() {
      this.dialogData.dialog = true
      // this.$refs.treeSelect.setCheckedNodes([])
    },
    // 树节点点击
    check(checkedNodes, checkedKeys, halfCheckedNodes, halfCheckedKeys) {
      if (!checkedNodes.children || !checkedNodes.children.length) {
        this.$refs.treeSelect.setCheckedNodes([checkedNodes])
      } else {
        this.$refs.treeSelect.setCheckedNodes([])
      }
    },
    /*
    resolve 添加树节点数据
    */
    initLoad(node, resolve) {
      let id
      if (node.data) {
        id = node.data.id
      } else {
        id = ''
      }
      queryExpertSepcialLisByUserId(id).then(res => {
        res.data.forEach(item => {
          item.leaf = !item.parent // 指定节点是否为叶子节点
        })
        resolve(res.data) // 添加树节点
      })
    },
    // 树节点加载子集事件
    treeLoadNode(node, resolve) {
      this.initLoad(node, resolve) // 初始化数据
    },
    // 当前选择的信息
    handleCheckChange(data, checked, indeterminate) {

    },
    // 打开弹框specialtyName
    openOrganizationDialog(specialtyName, buyersIndex, type) {
      this.organizationDialog = true // 打开弹框
    },
    // 选择信息-确定
    ensure() {
      const nodes = this.$refs.treeSelect.getCheckedNodes() // 获取已选中信息
      const data = {
        keys: [], // id集合
        labels: [] // 名称集合
      }
      nodes.forEach(item => {
        data.keys.push(item.specialtyId)
        data.labels.push(item.label)
      })
      data.keys = data.keys.join(',')
      data.labels = data.labels.join(',')

      this.$emit('change', data)
      this.dialogData.dialog = false // 关闭弹框
    },
    // 搜索重置
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped lang="less">
.condition {
  padding-top: 10px;
  padding-bottom: 10px;
  width: 80px;
  border-bottom: 2px solid blue;
  text-align: center;
}
.fromclearfix {
  padding-top: 20px;
}
/deep/ .el-tree {
  .el-checkbox {
    display: none;
  }
  .is-leaf + .el-checkbox {
    display: block;
  }
}
</style>
