<template>
  <div class="bf-organization">
    <!-- 上级组织 -->
    <div class="left">
      <div class="paren_header">
        <p>{{ title }}</p>
        <template>
          <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>
        </template>
      </div>
      <div class="paren_content">
        <div class="dptBox" v-show="active == 0">
          <el-scrollbar style="height: 100%">
            <el-tree :data="data" :check-strictly="true" :default-expanded-keys="defaultExpandedKeys" show-checkbox @node-click="nodeClick" @check-change="currentChange" :props="defaultProps" :default-checked-keys="checkedKeys" :node-key="nodekey" ref="tree" highlight-current>
              <span v-if="orgType == 'org'" slot-scope="{ data }" :title="data.OrganName" class="org-data-tree-node">
                {{ data.OrganName }}
              </span>
              <span v-else slot-scope="{ data }" :title="data.AuthName" class="org-data-tree-node">
                {{ data.AuthName }}
              </span>
            </el-tree>
          </el-scrollbar>
        </div>
        <!-- <div class="dptBox" v-if="doesJobShow" v-show="active == 1">
          <Rolelist :type="true" @radioChange="radioChange" :radioCheck="radioCheck" :number="8" :active="true" class="lessData" :mainData="mainData" />
        </div> -->
        <div v-show="active == 1" class="dptBox duoBox">
          <el-scrollbar style="height: 100%">
            <el-tree :data="data" show-checkbox @check-change="currentChangeDuo" :default-expanded-keys="defaultExpandedKeys" :props="defaultProps" :default-checked-keys="checkedKeysDuo" :node-key="nodekey" ref="treeDuo" highlight-current>
              <span v-if="orgType == 'org'" slot-scope="{ data }" :title="data.OrganName" class="org-data-tree-node">
                {{ data.OrganName }}
              </span>
              <span v-else slot-scope="{ data }" :title="data.AuthName" class="org-data-tree-node">
                {{ data.AuthName }}
              </span>
            </el-tree>
          </el-scrollbar>
        </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: () => [],
    },
    checkedKeysDuo: {
      //已选中
      type: Array,
      default: () => [],
    },
    lessData: {
      //副岗
      type: Array,
      default: () => [],
    },
    mainData: {
      //主岗
      type: Array,
      default: () => [],
    },
    radioCheck: {
      type: Number,
      default: 0,
    },
    checkBoxList: {
      type: Array,
      default: () => [],
    },
    orgType: {
      type: String,
      default: "org",
    },
    orgList: {
      type: Array,
      default: () => []
    }
  },
  components: { Rolelist },
  data () {
    return {
      active: 0,
      colType: [
        {
          name: "所属组织",
        },
        {
          name: "组织授权",
        }
      ],
      checkedDatas: [],
      checkedId: null,
      checkedList: [], //选中的列表,
      nodekeys: "nodekey",
      defaultExpandedKeys: [-1],
      getExpandedKeysStep: [false, false]
    };
  },
  watch: {
    checkedKeys: {
      handler (val) {
        this.checkedList = val;
        this.$refs.tree.setCheckedKeys(val)
        this.getExpandedKeysStep.splice(0, 1, true)
      },
      deep: true,
    },
    checkedKeysDuo: {
      handler (val) {
        this.$refs.tree.setCheckedKeys(val)
      },
      deep: true,
    },
    orgList: {
      handler (val) {
        this.getExpandedKeysStep.splice(1, 1, true)
      },
      deep: true
    },
    getExpandedKeysStep: {
      handler (val) {
        if (val.every(v => v)) {
          this.defaultExpandedKeys = [-1]
          if (this.orgType === 'org') {
            this.checkedKeys.forEach(key => {
              if (key !== -1) {
                const org = this.orgList.find(item => item.OrganId == key)
                if (org) {
                  const OrganUpid = org.OrganUpid
                  if (!this.defaultExpandedKeys.includes(OrganUpid)) {
                    this.defaultExpandedKeys.push(OrganUpid)
                  }
                }
              }
            })
          } else if (this.orgType === 'auth') {
            this.checkedKeys.forEach(key => {
              if (key !== -1) {
                const auth = this.orgList.find(item => item.AuthId == key)
                if (auth) {
                  const UpAuthId = auth.UpAuthId
                  if (!this.defaultExpandedKeys.includes(UpAuthId)) {
                    this.defaultExpandedKeys.push(UpAuthId)
                  }
                }
              }
            })
          }
        }
      },
      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) {
      if (this.orgType == 'org') {
        const { OrganId } = data
        this.$emit("getTreeData", this.$refs.tree.getCheckedNodes())
        if (isChecked) {
          const checked = [OrganId]
          this.$refs.tree.setCheckedKeys(checked)
        }
      } else if (this.orgType == 'auth') {
        const { AuthId } = data
        this.$emit("getTreeData", this.$refs.tree.getCheckedNodes())
        if (isChecked) {
          const checked = [AuthId]
          this.$refs.tree.setCheckedKeys(checked)
        }
      }
    },
    currentChangeDuo () {
      const arr = this.$refs.treeDuo.getCheckedNodes()
      this.$emit("getTreeDataDuo", arr)
    },
    // 选中后设置初始数据
    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%;
  .org-data-tree-node {
    font-size: 14px;
    max-width: 10vw;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    margin-left: 10px;
  }
}
</style>
