<template>
  <bc-check :ref="treeId+'ForDict'" :show.sync="checkModalShow" :treeData="treeData" @checkOk="checkedDictOk"
            :treeId="treeId" :multi="multi"  :types="types" :ifOnlyLeaf="ifOnlyLeaf" ></bc-check>
</template>
<script>
 import bcDictUtil from "../bc-dict/index.js"

  let $this;
  export default {
    name: "bcCheckDict",
    props: {
      show: {
        type: Boolean,
        default: false
      },
      treeId: {
        type: String,
        default: 'bc-check-dict'
      },
      groupCode: {//字典类型编码
        type: String,
        default: null
      },
      parentValue: {//父字典的值
        type: String,
        default: null
      },
      params:{//其他的查询条件或排序参数

      },
      /*resultData: {
        type: Array,
        default: function () {
          return []
        }
      },*/
      multi: {
        type: Boolean,
        default: false
      },
      types: {
        type: Array,
        default: function () {
          return []
        }
      },
      ifOnlyLeaf: {//是否开启只选叶子结点模式
        type: Boolean,
        default: false
      },
    },
    data() {
      return {
        checkModalShow: false,
        treeData: [],
        columns: [],
      }
    },
    methods: {
      reloadTreeData() {
        if (!this.groupCode){return false}
        if (this.parentValue){
          this.$bcDictUtil.getDictChildren(this.groupCode, this.parentValue, (data) =>{
            let treeObj = [];
            this.handleDataToTree(data,treeObj)
            this.treeData = data
            return;
          }/*, this.params*/);
        }else {
          this.$bcDictUtil.getDict(this.groupCode, (data) =>{
            let treeObj = [];
            this.handleDataToTree(data,treeObj)
            this.treeData = treeObj
            return;
          }/*, this.params*/);
        }
      },
      //将普通的数据转换成bc-check需要的参数
      handleDataToTree(data,treeObj){
        data.forEach(n => {
          n.pId = n.parentId
          if (n.children && n.children.length>0){
            this.handleDataToTree(n.children,treeObj)
          }
          n.children = []
          treeObj.push(n)
        })
      },
      checkedDictOk(columns){
        this.columns = columns
        this.$emit('checkOk', this.columns);
        this.$refs[this.treeId+'ForDict'].clearCache();
        this.columns = []
      },
    },
    mounted: function () {
      $this = this;
    },
    watch: {
      show: {
        immediate: true,
        handler: function () {
          this.checkModalShow = this.show;
        }
      },
      checkModalShow: {
        immediate: true,
        handler: function () {
          //赋值
          if (!this.checkModalShow) {
            this.$emit('update:show', false)
          }
        }
      },
      groupCode: {
        immediate: true,
        handler: function () {
          //if (!this.parentValue){
            this.reloadTreeData();
          //}
        }
      },
      parentValue: {
        immediate: true,
        handler: function () {
          this.reloadTreeData();
        }
      },
      /*resultData: {
        immediate: true,
        handler: function () {
          this.columns = JSON.parse(JSON.stringify(this.resultData));
        }
      }*/
    }
  }
</script>
