<template>
  <div class="bf-organization">
    <!-- 上级组织 -->
    <div class="left">
      <div class="paren_header">
        <p>{{ title }}</p>
        <div class="column" v-show="vice">
          <div :class="active == index ? 'column_childs' : 'column_child'" v-for="(item, index) in colType" :key="index" @click="upStart(index)">
            {{ item.name }}
          </div>
        </div>
      </div>
      <div class="paren_content">
        <div class="dptBox" v-show="active == 0">
          <el-scrollbar style="height: 100%">
            <el-tree :data="data" :check-strictly="true" show-checkbox @node-click="nodeClick" @check-change="currentChange" :props="defaultProps" default-expand-all :default-checked-keys="checkedKeys" :expand-on-click-node="false" :node-key="nodekey" ref="tree" highlight-current>
            </el-tree>
          </el-scrollbar>
        </div>
        <div class="dptBox" v-show="active == 1">
          <Rolelist :type="true" @radioChange="radioChange" :radioCheck="radioCheck" :number="8" :active="true" class="lessData" :mainData="mainData" />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import Rolelist from '@/components/rolelist'
export default {
  props: {
    title: {
      type: String,
      default: "",
    },
    vice: {
      type: Boolean,
      default: false,
    },
    data: {
      type: Array,
      default: () => [],
    },
    nodekey: {
      type: String,
      default: "",
    },
    defaultProps: {
      type: Object,
      default: () => { },
    },
    checkedKeys: {
      //已选中
      type: Array,
      default: () => [],
    },
    lessData: {
      //副岗
      type: Array,
      default: () => [],
    },
    mainData: {
      //主岗
      type: Array,
      default: () => [],
    },
    radioCheck: {
      type: Number,
      default: 0,
    },
    checkBoxList: {
      type: Array,
      default: () => [],
    },
  },
  components: { Rolelist },
  data () {
    return {
      active: 0,
      colType: [
        {
          name: "组织",
        },
        {
          name: "岗位",
        }
      ],
      checkedDatas: [],
      checkedId: null,
      checkedList: [], //选中的列表,
      nodekeys: "nodekey",
    };
  },
  watch: {
    checkedKeys: {
      handler (val) {
        this.checkedList = val;
        this.$refs.tree.setCheckedKeys(val)
      },
      deep: true,
    },
  },
  mounted () {
    if(this.data!=null){
      this.data = this.formatData(this.data)
    }
  },
  methods: {
    //点击选择
    upStart (index) {
      this.active = index;
      this.$emit("getTreeindex", index)
    },
    formatData (params) {
      let data = params;
      data.map((item) => {
        if (item.hasOwnProperty('children')) {
          item.disabled = true;
          this.formatData(item.children)
        }
      });
      return data;
    },
    currentChange (data, isChecked) {
      const { OrganId } = data
      this.$emit("getTreeData", this.$refs.tree.getCheckedNodes())
      if (isChecked) {
        const checked = [OrganId]
        this.$refs.tree.setCheckedKeys(checked)
      }
    },
    // 选中后设置初始数据
    setDataObj (arr) {
      arr.forEach((item) => {
        if (!item.children) {
          item.children = {
            OrganId: item.OrganId,
            ValidBegin: "",
            ValidEnd: "",
            Action: "",
            QueryRow: "",
            QueryCol: "",
            NewCol: "",
            EditRow: "",
            EditCol: "",
            DeleteRow: "",
          };
        }
      });
      this.$emit("getTreeData", arr)
    },
    nodeClick (data) {
      this.$emit("getTreeDatas", data)
      this.checkedId = data.nodekey
    },
    radioChange (val) {
      this.$emit('radioChange', val)
    },
    //多选框
    checkChange (arr) {
      this.$emit("checkChange", arr)
    },
    checkClick (item) {
      this.$emit("checkClick", item)
    }
  },
};
</script>
<style lang="scss">
@import "./organization.scss";
.DBox {
  cursor: not-allowed;
}
.dptBox {
  height: 100%;
}
</style>
