var app = getApp()
var that;
Component({
  mixins: [],
  data: {
    all_user_list: {},
    isShow: false,
    keyVal: '',
    isAll: true,
  },
  props: {
    onConfirm: (data) => console.log(data),
    onClose: (data) => console.log(data),
    defaultUser: '',
    isAllSelect: true,//是否多选
  },
  didMount() {
    that = this;
    this.setData({ isShow: this.props.isShow, isAll: that.props.isAllSelect, })
    this.getData();
  },
  didUpdate() { },
  didUnmount() { },
  methods: {
    onReset() {
      this.setData({
        isVal: false,
        keyVal: ''
      })
    },
    //搜索
    bindKeyInput(e) {
      this.setData({
        keyVal: e.detail.value,
        isVal: e.detail.value ? true : false
      })
    },
    onConfirm() {
      this.props.onConfirm(this.data.selectUserId);
      this.props.onClose();
    },
    radioChange: function (e) {
      var selectUserId = this.data.selectUserId;
      var isExist = false;
      let item = e.target.dataset.item;
      let index = e.target.dataset.index;
      let checked = `userList[${index}].checked`; //获取当前控制选中的字段
      let elm = this.data.userList[index].checked;//当前的状态
      if (this.data.isAll) {//当可多选时
        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);
            }
          });
        }
      } else {//单选择
        let userList=this.data.userList;
        userList.map(item => {
          item.checked = false;
        })
        selectUserId = [];
        if (e.detail.value) {//当有值时选中
            selectUserId.push(item);
            this.setData({
              userList: userList
            })
        } else {//取消选中
          // selectUserId = [];
        }
      }
      this.setData({
        [checked]: !elm,
      })
      this.setData({
        selectUserId: selectUserId
      })
    },
    getData() {
      app.$get2("api/per/user/employee_list", { page:0,pageSize:0 }).then((res) => {
        var list = res.data.data.list;
        var arr = this.props.defaultUser;
        if (typeof (arr) == 'string') {
          arr = arr.split(',');
        }
        var ids = [], userList = list;
        userList.map(item => {
          item.checked = false;
          if (arr.length > 0) {
            arr.forEach(item2 => {
              if (item.id == item2) {
                item.checked = true;
                ids.push(item);
              }
            })
          }
        })
        this.setData({
          userList: userList,
          all_user_list: userList,
          selectUserId: ids.length > 0 ? ids : []
        })
      })
    },
    //关闭
    onClose(e) {
      this.props.onClose();
    }
  },
});
