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,
    userData: [],//初次显示的数据源
    keyVal: ''
  },
  props: {
    onClose: (data) => console.log(data),
    onConfirm: (data) => console.log(data),
    selectUser: [],//默认选择
    isAllSelect: true,//是否多选
    isLeadership: false,//是否选择领导
    superior_list: [],//上级交叉领导集合
    islead: false,//是否只显示领导
  },
  didMount() {
    that = this;
    this.setData({ selectUserId: this.props.selectUser[0] ? this.props.selectUser : [] })
    var superior_list = this.props.superior_list;//将传进来的上级集合，转成集合里变成对象（原先是数组）【】
    var userData = app.globalData.userData.employee_detail;
    this.setData({
      userData: this.props.isLeadership ? userData.manage_scope : superior_list.length > 0 ? superior_list : userData.superior_list,
      terr: [{ name: '全部', id: 0 }]
    })
    this.getBmData();
    this.getData(0);
  },
  methods: {
    onReset() {
      this.setData({
        isVal: false,
        keyVal: ''
      })
    },
    onConfirm() {
      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.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: [], userList: [], all_user_list: [] });
      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;
    },
    //单选择
    bindtap1(e) {
      this.setData({ selectUserId: [] });
      var index = e.target.dataset.index;
      var checked = `userList[${index}].checked`;
      var 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
          })
        }
      })
      var arr = [];
      arr.push(e.target.dataset.item);
      if (!elm) {
        this.setData({
          [checked]: !elm,
          selectUserId: arr
        })
      } else {
        this.setData({
          [checked]: !elm,
          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 ids = [];
        if (that.props.islead) {//只显示管理者  
          list.map((item) => {
            item.checked = false;
            var isManager=false;
            item.role_list.forEach(roleItem=>{
              if (roleItem.name == 'admin' || roleItem.name == 'creator' || roleItem.name == 'point_manager' || roleItem.name == 'dept_manager') {
                    isManager=true;
              }
            })
            if (isManager) {
              arr.push(item);
              if (selectUserId.length > 0) {
                selectUserId.forEach(item2 => {
                  if (item.id == item2.id) {
                    item.checked = true;
                    ids.push(item);
                  }
                })
              }
            }
          })
        } else {
          list.map((item) => {
            item.checked = false;
            if (selectUserId.length > 0) {
              selectUserId.forEach(item2 => {
                if (item.id == item2.id) {
                  item.checked = true;
                  ids.push(item);
                }
              })
            }
          })
          that.data.userData.map((item) => {
            list.map((item2) => {
              if (item.id == item2.id) {
                arr.push(item2);
              }
            })
          })
        }
        this.setData({
          userList: arr,
          all_user_list: arr,
          selectUserId: ids.length > 0 ? ids : this.data.selectUserId
        })
      })
    },
    //获取部门列表
    getBmData() {
      app.$get("api/department/tree", {}).then((res) => {
        var list = res.data.data.list;
        this.setData({
          rule_tree: list,
          all_rule_list: list
        })
      })
    },
  },
});
