import { getDingTalkAllDeptUser, refashDingTalkAllDeptUser } from '@/api/expenseManagement'
const DingDepartUser = {
  template: `
      <a-tree-select
          :value="defaultVal"
          tree-data-simple-mode
          style="width: 100%"
          :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
          :tree-data="treeData"
          :placeholder="placeholder"
          multiple
          treeCheckable
          :treeCheckStrictly="true"
          treeNodeFilterProp="title"
          @change="handleChange"
        />
    `,
  props: {
    value: {
      type: Array,
      defatut: function(){
        return []
      }
    },
    id: {
      type: String,
      default: ''
    },
    placeholder: {
      type: String,
      default: '请选择人员'
    },
    disabled:{
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      defaultVal: [],
      treeData: []
    };
  },
  mounted() {
    this.genTreeNode()
  },
  watch: {
    value(newValue, oldValue) {
      this.defaultVal = newValue
    },
  },
  methods: {
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    handleChange(value) {
      this.defaultVal = value;
      this.$emit('change', value, this.id);
      this.$emit('input', value);
    },
    // 格式化数据
    formatData(data){
      console.log(data)
      const ret = []
      data.map(item => {
        // 部门
        if(item.deptId){
          const node = {
            title: item.name,
            id:item.deptId,
            value: item.deptId,
            key: item.deptId,
            pId: item.parentId,
            autoAddUser: item.autoAddUser,
            createDeptGroup: item.createDeptGroup,
            disableCheckbox: true,
            checkable: false,
            children: []
          }
          if(item.deptUserList && item.deptList){
            node.children = this.formatData(item.deptList.concat(item.deptUserList?item.deptUserList:[]))
          }else if(item.deptUserList && !item.deptList){
            node.children = this.formatData(item.deptUserList)
          }else if(!item.deptUserList && item.deptList){
            node.children = this.formatData(item.deptList)
          }
          ret.push(node)
        }else{
          ret.push({
            title: item.name,
            id: item.userid,
            value: item.userid,
            key:item.userid,
            pId: item.parentId,
            avatar: item.avatar,
            leader: item.leader,
            boss: item.boss,
            admin: item.admin,
            active: item.active,
            userid: item.userid,
            isLeaf: true
          })
        }
         
      })
      return ret
    },
    // 获取部门人员
    async genTreeNode() {
       let ret = await getDingTalkAllDeptUser().then(res => res.data)
       const res1 = ret.deptList||[]
       const res2 = ret.deptUserList||[]
       this.treeData = this.treeData = this.formatData(res1.concat(res2));
       console.log(this.treeData,'----')
       this.defaultVal = this.value
    },
    // 更新人员数据
    updateDeptUser(){
      const _this = this
      refashDingTalkAllDeptUser().then(res => {
        if(res.status == 200){
          // this.treeData = []
          // this.genTreeNode()
          const h = this.$createElement;
          this.$info({
            title: '提示',
            centered: true,
            content: h('div', {}, [
              h('p', '数据正在刷新中...'),
              h('p', '请10分钟后（按F5刷新页面）再进行操作！'),
            ]),
            onOk() {
              _this.$emit("close")
            },
          });
        }else{
          this.$message.error(res.message)
        }
        this.$emit("loaded")
      })
    }
  },
};

export default DingDepartUser