var app = getApp();
var that;
import { _debounce, getCache } from '../../../utils/util'
Page({
  data: {
    smdsins: 0,
    page: 1,
    smdsHead: [
      { id: 0, name: '参与人数' },
      { id: 1, name: '目标制定' },
      { id: 2, name: '目标确认' },
      { id: 3, name: '执行中' },
      { id: 4, name: '结果值录入' },
      { id: 5, name: '评分' },
      { id: 9, name: '审批' },
      { id: 11, name: '考核结束' }
    ],
    employee_list: [],
    list: [],
    selectUser: []
  },
  onLoad() {
    that = this;
    let obj = getCache('statementDetails')
    if (obj.paths == 'statdeEcharts') {
      dd.setNavigationBar({ title: '考核人数列表' });
      this.setData({
        id: obj.asslist.id,
        moduleshow:false,
        itemparams:{
          name:obj.dept_o.name,
          val:obj.dept_o.tak
        },
       dept_ids:JSON.stringify(obj.dept_o.id),
       nowAssList:obj.asslist,
      })
    } else if (obj.paths == 'personnelDetail') {
      dd.setNavigationBar({ title: '人员明细' });
      this.setData({
         id: obj.asslist.id,
         moduleshow:true,
         smdsins:obj.index||0,
         nowAssList:obj.asslist,
      })
    }   
  },
  onShow(){
    this.getInitData();
    this.getList();
  },
  bindKeyInput: _debounce(function (e) {   //防抖
    let value = e.detail.value
    let arr = []
    if (value == '') {
      arr = []
    } else {
      this.data.employee_list.forEach(item => {
        if (item.name.indexOf(value) != -1) {
          arr.push(item.id)
        }
      })
      this.setData({
        page: ++this.data.page
      })
      arr = arr.length == 0 ? [0] : arr
    }
    this.setData({
      selectUser: arr
    })
    this.getList()
  }, 1000),
  openDetail(e){
      let item=e.target.dataset.item
      let str=`../performanceDetails/performanceDetails?assId=${this.data.nowAssList.id}&staffId=${item.employee_id}&tel=${this.data.nowAssList.name}`
      dd.navigateTo({
        url: str
      })
  },
  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;
  },
  //上啦加载
  onScrollToLower() {
    if (!that.data.isData) {
      this.setData({
        page: ++this.data.page
      })
      this.getList(true);
    }
  },
  activeItem(e) {
    this.setData({
      smdsins: e.target.dataset.id
    })
    this.getList();
  },
  getList(is) {
    if (!is) { this.setData({ page: 1 }) };
    this.setData({list: []})
    let data = {
      package_id: this.data.id,
      doing_id: this.data.smdsins,
      page: this.data.page,
      page_size: 2000,
    }
    if(this.data.dept_ids){
      data.dept_ids=this.data.dept_ids
    }
    if (this.data.selectUser.length > 0) {
      data.employee_ids = JSON.stringify(this.data.selectUser)
    }
    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
      })
    })
  },
  getInitData() {
    app.$get('api/per/package/info_v3_aid', { package_id: this.data.id }).then(res => {
      this.setData({
        employee_list: res.data.data.employees
      })
    })
  },
});
