var app = getApp();
var that;
import moment from 'moment' // 时间库
Page({
  data: {
    minuteIndex: 0,//条件选择
    minuteArray: [
      { label: '全部待办', value: 0 },
      { label: '目标制定', value: 1 },
      { label: '目标确认', value: 2 },
      { label: '结果录入', value: 4 },
      { label: '评分', value: 5 },
      { label: '审批', value: 9 }
    ],
    list: [],
    page: 1,
    pendingList: [] //提供给考核详情上下切换人员列表
  },
  onLoad() {
    that = this;
    dd.setNavigationBar({ title: "待办事项" });
  },
  onShow() {
    if (this.data.page == 1) {
      this.getList();
    }
  },
  openDetail(e) {
    let item = e.target.dataset.item
    dd.navigateTo({
      url: `../performanceDetails/performanceDetails?id=${item.remark.packageEmployee_id}&tel=${item.remark.package_name}`
    })
  },
  //上啦加载
  onScrollToLower() {
    if (!that.data.isData) {
      this.setData({
        page: ++this.data.page
      })
      this.getList(true);
    }
  },
  activeItem(e) {
    this.setData({ minuteIndex: e.detail.value, })
    this.getList();
  },
  getList(is) {
    if (!is) { this.setData({ page: 1 }) };
    this.setData({ list: [] })
    let data = {
      status: 0,
      node_type: this.data.minuteArray[this.data.minuteIndex].value,
      page: this.data.page,
      page_size: 20,
    }
    app.$get('api/per/package/msg/agency', data).then((res) => {
      var list = res.data.data.list;
      list.forEach(item => {
        if (item.remark.employee_id && app.globalData.usersList[item.remark.employee_id]) {
          // 被考核人
          let userInfo = app.globalData.usersList[item.remark.employee_id];
          item.userInfo = userInfo;
        }
        if (item.employee_id && app.globalData.usersList[item.employee_id]) {
          // 当是导入导出时，显示登录者
          item.userInfo2 = app.globalData.usersList[item.employee_id];
        }
      });
      if (this.data.page !== 1) {
        list = this.data.list.concat(list);
      }
      let data = list;
      let pendingList = [];
      data.forEach(item => {
        if (item.remark.employee_id&&app.globalData.usersList[item.remark.employee_id]) {
          // 被考核人
          let userInfo = app.globalData.usersList[item.remark.employee_id];
          pendingList.push({ name: userInfo.name, employeeID: item.remark.packageEmployee_id, package_name: item.remark.package_name });
        }
      });
      data.forEach(item => {
        // 转换展示的时间格式
        item.update_time = moment(item.update_time).format('YYYY-MM-DD HH:mm')
        if (/\d{4}/g.exec(item.update_time)[0] == moment().format('YYYY')) {
          item.tineKind = moment(new Date(item.update_time.replace(/-/g, '/')).getTime()).format('MM月DD日');
        } else {
          item.tineKind = moment(new Date(item.update_time.replace(/-/g, '/')).getTime()).format('YYYY年MM月DD日');
        }
      });
      // 转换成可用格式
      let bbt = [];
      data.forEach(item => {
        if (bbt[item.tineKind]) {
          bbt[item.tineKind].list.push(item);
        } else {
          bbt[item.tineKind] = true;
          bbt[item.tineKind] = {
            time: item.tineKind,
            list: []
          };
          bbt[item.tineKind].list.push(item);
        }
      });
      let listdata = [];
      for (let i in bbt) {
        listdata.push(bbt[i]);
      }
      this.setData({
        list: listdata,
        pendingList: pendingList,
        total: res.data.data.total,
        isData: list.length == res.data.data.total
      })
    })
  },
});
