var app = getApp();
var that;
Page({
  data: {
    id: '',
    minuteIndex: 0,//条件选择
    minuteArray: [
      { value: 0, text: '全部' },
      { value: 1, text: '目标制定' },
      { value: 2, text: '目标确认' },
      { value: 3, text: '执行中' },
      { value: 4, text: '结果值录入' },
      { value: 5, text: '评分' },
      { value: 9, text: '审批' },
      { value: 11, text: '结束' }
    ],
    popupCx: '',

    showStaff: false,//显示员工选择
    staffVal: '全部员工',

    showTissue: false,//显示部门组件
    TissueVal: '全公司',

    employee_list: [{ id: 0, name: '全部员工' }],
    employee_index: 0,
  },
  onLoad(e) {
    that = this;
    this.setData({
      id: e.id,
      e:e,
    })
    dd.setNavigationBar({ title: e.tel });
    this.getInitData();
    this.getList();
  },
  getStr(type) {
    let is = '';
    switch (type) {
      case 1:
        is = '目标制定';
        break;
      case 2:
        is = '目标确认';
        break;
      case 3:
        is = '执行中';
        break;
      case 4:
        is = '结果值录入';
        break;
      case 5:
        is = '评分';
        break;
      case 9:
        is = '审批';
        break;
      case 11:
        is = '结束';
        break;
    }
    return is;
  },
  getInitData() {
    app.$get('api/per/package/info_v3_aid', { package_id: this.data.id }).then(res => {
      this.setData({
        employee_list:[{ id: 0, name: '全部员工' }]
      })
      let employee_list = this.data.employee_list;
      employee_list.push(...res.data.data.employees);
      this.setData({
        employee_list: employee_list
      })
    })
  },
  getList(is) {
    if (!is) { this.setData({ page: 1 }) };
    this.setData({ list: [] })
    let data = {
      package_id: this.data.id,
      doing_id: this.data.minuteArray[this.data.minuteIndex].value,
      page: this.data.page,
      page_size: 20,
    }
    if (this.data.dept_id) {
      data.dept_ids = JSON.stringify([this.data.dept_id]);
    }
    if (this.data.employee_index != 0) {
      data.employee_ids = JSON.stringify([this.data.employee_list[this.data.employee_index].id])
    }
    app.$get('api/per/package/info_v3', data).then((res) => {
      var list = res.data.data.list;
      list.forEach(item => {
        item.str=this.getStr(item.doing_id);
        if (item.employee_id&&app.globalData.usersList[item.employee_id]) {
          //当是导入导出时，显示登录者
          item.userInfo = app.globalData.usersList[item.employee_id];
          item.dept_list = app.globalData.usersList[item.employee_id].dept_list
        }
      });
      if (this.data.page !== 1) {
        list = this.data.list.concat(list);
      }
      this.setData({
        list: list,
        isData: list.length == res.data.data.total
      })
    })
  },
  openDetail(e){
      let item=e.target.dataset.item
      let str=`../performanceDetails/performanceDetails?assId=${this.data.id}&staffId=${item.employee_id}&tel=${this.data.e.tel}`
      dd.navigateTo({
        url: str
      })
  },
  activeItem(e) {
    console.log(e.detail.value)
    this.setData({ employee_index: e.detail.value, })
    this.getList();
  },
  activeItem2(e) {
    this.setData({ minuteIndex: e.detail.value, })
    this.getList();
  },
  //上啦加载
  onScrollToLower() {
    if (!that.data.isData) {
      this.setData({
        page: ++this.data.page
      })
      this.getList(true);
    }
  },
  //显示部门选择组件
  selectTissue() {
    this.setData({ showTissue: !this.data.showTissue, popupCx: 'container__mask', })
  },
  //选择部门
  onConfirmTissue(data) {
    console.log(data);
    this.setData({ dept_id: data.dept_id, TissueVal: data.name })
    this.getList();
  },
  //显示选择员工
  selectStaff() {
    this.setData({
      popupCx: 'container__mask',
      showStaff: true
    })
  },
  //关闭弹窗
  onClose() {
    this.setData({
      showTissue: false,
      showStaff: false,
      popupCx: '',
      rule_item_id: ''
    })
  },
  //选择员工
  onConfirmStaff(data) {
    if (!data) {
      this.setData({ staffVal: '全部员工', employee_ids: '', page: 1, rule_item_id: '' })
      this.getData();
      return false;
    }
    if (data.length > 0) {
      if (data.length > 1) {
        var str = "";
        var arr = [];
        data.forEach((item) => {
          str += item.name + ','
          arr.push(item.id);
        })
        var employee_ids = arr.join(',');
        this.setData({ staffVal: str.substring(0, str.length - 1), employee_ids: employee_ids, page: 1, rule_item_id: '' })
      } else {
        this.setData({ staffVal: data[0].name, employee_ids: [data[0].id], page: 1, rule_item_id: '' })
      }
    } else {
      this.setData({ staffVal: '全部员工', employee_ids: [], page: 1, rule_item_id: '' })
    }
    this.getData();
  },
});
