var app = getApp()
var that;
Component({
  mixins: [],
  data: {
    userList: [],//人员集合
    all_user_list: [],//人员集合
    isShow: false,
    showId: 0,//显示的部门ID下员工
    terr: [
      { name: '全部', dept_id: 0 }
    ],
    all_rule_list: [],
    rule_tree: [],
    selectUserId: [],
    isAllChecked: false,
    isAll: true,
    userData: [],
    isCreator: false,
    keyVal: '',
  },
  props: {
    onClose: (data) => console.log(data),
    onConfirm: (data) => console.log(data),
    selectUser: [],
    isAllSelect: true,//是否多选
    isLeadership: false,//数据源是否是自己的管理范围
    isBoss: false,//是否去除创始人
    isUser: false,//是否去除自己
  },
  didMount() {
    that = this;
    that.getData(0);
    that.getBmData();
    that.setData({
      isCreator: app.globalData.isCreator, 
      selectUserId: that.props.selectUser[0] ? that.props.selectUser : [],
      isAll: that.props.isAllSelect,
      // userData: app.globalData.userData.employee_detail.manage_scope,
      terr: [{ name: '全部', dept_id: 0 }]
    })
  },
  didUpdate() { },
  didUnmount() {
  },
  methods: {
    onReset() {
      this.setData({
        isVal: false,
        keyVal: ''
      })
    },
    onConfirm() {
      if (this.data.selectUserId.length == 0) {
        app.globalData.showToast("请选择一名员工")
        return
      }
      this.props.onConfirm(this.data.selectUserId);
      this.onClose();
    },
    //搜索
    bindKeyInput(e) {
      this.setData({
        keyVal: e.detail.value,
        isVal: e.detail.value ? true : false
      })
    },
    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.dept_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: '全部', dept_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.dept_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.dept_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 selectUserId = this.data.selectUserId;
      var isExist = false;
      var userList = this.data.userList;
      if (!this.data.isAll) {//单选择
        let item = e.target.dataset.item;
        let index = e.target.dataset.index;
        let checked = `userList[${index}].checked`;//获取当前控制选中的字段
        let elm = this.data.userList[index].checked;//当前的状态

        //将所有的选中状态改成未中
        var userList = this.data.userList;
        userList.map((item, item_index) => {
          if (item.checked) {
            var item_checked = `userList[${item_index}].checked`;
            this.setData({
              [item_checked]: false
            })
          }
        })
        if (!elm) {
          selectUserId.forEach((element, _this) => {
            if (element.id == item.id) {
              isExist = true;
            }
          });
          if (!isExist) {
            selectUserId = [];
            selectUserId.push(item);
          }
        } else {//取消选中
          selectUserId = [];
        }
        this.setData({
          [checked]: !elm,
        })
      } else {//多选择
        let item = e.target.dataset.item;
        let index = e.target.dataset.index;
        let checked = `userList[${index}].checked`;//获取当前控制选中的字段
        let elm = this.data.userList[index].checked;//当前的状态
        this.setData({
          [checked]: !elm,
        })
        if (e.detail.value) {//当有值时选中
          selectUserId.forEach((element, _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.$get2("api/per/user/employee_list", { dept_id: dept_id,page:0,pageSize:0,official:1,scope:1 }).then((res) => {
        var list = res.data.data.list || [];
        var selectUserId = this.data.selectUserId;
        var arr = [];
        var userList = [];
        list.forEach(item => {
          if (this.props.isBoss && item.is_creator == 1) {
            return null
          } else if (this.props.isUser && item.id == app.globalData.userData.id) {
            return null
          } else {
            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() {
      app.$get("api/per/user/department", {}).then((res) => {
        var list = res.data.data.dept_tree;
        this.setData({
          rule_tree: list,
          all_rule_list: list
        })
      })
    },
  },
});
