var app = getApp()
var that;
Component({
  mixins: [],
  data: {
    userList: [],//人员集合
    all_user_list: [],//人员集合
    isShow: false,
    showId: 0,//显示的部门ID下员工
    terr: [
      { name: '全部', id: 0 }
    ],
    all_rule_list: [],
    rule_tree: [],
    selectUserId: [],
    isAllChecked: false,
    isItem: true,
    searchVal: '',
    isAll: true,
    userData:[],
  },
  props: {
    onClose: (data) => console.log(data),
    onConfirm: (data) => console.log(data),
    selectUser: [],
    isAllSelect: true,//是否多选
    isLeadership:false,//是否限制人员选择
  },
  didMount() {
    that = this;
    app.login(app.globalData.corpId, function () {
      that.setData({
        selectUserId: that.props.selectUser,
        isAll: that.props.isAllSelect,
        userData:app.globalData.userData.employee_detail.manage_scope,
        terr:[{name: '全部', id:0}]
      })
      that.getData(0);
      that.getBmData();
    })
  },
  didUpdate() { },
  didUnmount() {
   },
  methods: {
    onConfirm() {
      if(this.data.selectUserId.length==0){
        app.globalData.showToast("请选择一名员工")
        return
      }
      this.props.onConfirm(this.data.selectUserId);
      this.onClose();
    },
    //搜索
    bindKeyInput(e) {
      var userList = this.data.userList;
      var items = userList.filter((item) => {
        return item.name.indexOf(e.detail.value) >= 0
      });
      this.setData({
        searchVal: items,
        isItem: e.detail.value ? false : true
      })

    },
    changeSection(e) {
      var item = e.target.dataset.item;
      var terr = this.data.terr;
      terr.push(item);
      if (item._child.length > 0) {
        that.setData({
          rule_tree: item._child,
          terr: terr,
          isAllChecked: false
        })
      } else {
        that.setData({
          rule_tree: [],
          terr: terr,
          isAllChecked: false
        })
      }
      this.getData(item.id);
    },
    //点击导航栏
    activeItem(e) {
      var item = e.target.dataset.item;
      var index = e.target.dataset.index;
      var terr = this.data.terr;
      if ((index + 1) == terr.length) { return false };
      this.setData({ rule_tree: [] });
      if (index == 0) {
        this.setData({
          terr: [{ name: '全部', id: 0 }],
          rule_tree: this.data.all_rule_list,
          isAllChecked: false
        })
      } else {
        var arr = terr.slice(0, index + 1);
        this.setData({
          rule_tree: item._child,
          terr: arr,
          isAllChecked: false
        })
      }
      this.getData(item.id);
    },
    // 点击下一级
    openDown(e) {
      this.setData({ rule_tree: [] });
      var item = e.target.dataset.item;
      var terr = this.data.terr;
      terr.push(item);
      if (item._child.length > 0) {
        that.setData({
          rule_tree: item._child,
          terr: terr,
          isAllChecked: false
        })
      }
      this.getData(item.id);
    },
    getArrDifference(arr1, arr2) {
      var obj = {};
      arr1.concat(arr2).forEach(function (v, i, arr) {
        obj[v.id] = v;
      });
      return obj;
    },
    //全选择
    ruleActiveAll(e) {
      var userList = this.data.userList;
      var selectUserId = this.data.selectUserId;
      if (e.detail.value) {
        userList.map(item => {
          item.checked = true;
        })
        if (selectUserId.length > 0) {
          var obj = this.getArrDifference(selectUserId, userList);
          this.setData({
            selectUserId: Object.values(obj),
            userList: userList,
            isAllChecked: true
          })
        } else {
          this.setData({
            selectUserId: userList,
            userList: userList,
            isAllChecked: true
          })
        }

      } else {
        userList.map(item => {
          item.checked = false;
        })
        this.setData({
          userList: userList,
          selectUserId: [],
          isAllChecked: false
        })
      }
    },
    //单选择
    radioChange: function (e) {
      var item = e.target.dataset.item;
      var selectUserId = this.data.selectUserId;
      var isExist = false;
      if (e.detail.value) {
        if (!this.data.isAll) {//当设置单选择时
          if (selectUserId.length > 0) {
            app.globalData.showToast("只能选择一名人员");
            return;
          }
        }
        selectUserId.forEach((element, index, _this) => {
          if (element.id == item.id) {
            isExist = true;
          }
        });
        if (!isExist) {
          selectUserId.push(item);
        }
      } else {
        selectUserId.forEach((element, index, _this) => {
          if (element.id == item.id) {
            _this.splice(index, 1);
          }
        });
      }
      this.setData({
        selectUserId: selectUserId
      })
    },
    //关闭
    onClose(e) {
      this.props.onClose();
    },
    //获取人员列表
    getData(dept_id) {
      this.setData({
          userList:[],
          all_user_list:[]
      })
      app.$get("api/employee/list", { dept_id: dept_id }).then((res) => {
        var list = res.data.data.list||[];
        var selectUserId = this.data.selectUserId;
        var arr=[];
        var userList=[];
        list.forEach(item=>{
          if(item.is_official==1){
              userList.push(item)
          }
        })
        userList.map((item) => {
          item.checked = false;
          if (selectUserId.length > 0) {
            selectUserId.forEach(item2 => {
              if (item.id == item2.id) {
                item.checked = true;
              }
            })
          }
        })
        that.data.userData.map((item)=>{
              userList.map((item2)=>{
                  if(item.id==item2.id){
                      arr.push(item2);
                  }
            })
        })
        this.setData({
          userList:that.props.isLeadership? arr:userList,
          all_user_list:that.props.isLeadership? arr:userList,
        })
      })
    },
    //获取部门列表
    getBmData(dept_id) {
      app.$get("api/department/tree", {}).then((res) => {
        var list = res.data.data.list;
        this.setData({
          rule_tree: list,
          all_rule_list: list
        })
      })
    },
  },
});
