var app = getApp();
var that;
import moment from 'moment' // 时间库
Page({
  data: {
    tabIndex: 1,//条件选择
    list: [],
    status: 0,
    page: 1,
  },
  onLoad() {
    that = this;
    dd.setNavigationBar({ title: "消息通知" });
  },
  onShow(){
    if (this.data.page == 1) {
      this.doSthForSb();
    }
  },
  tabAction(e) {
    let index = e.target.dataset.index
    this.setData({
      tabIndex: index,
      status: index == 1 ? 0 : 1,
      list: []
    })
    this.doSthForSb()
  },
  openDetail(e) {
    let item = e.target.dataset.item
    let data = {
      id: item.id,
      type: item.type,
      employee_id: item.employee_id
    }
    if(this.data.tabIndex==1){
      dd.navigateTo({
        url: `../performanceDetails/performanceDetails?id=${item.remark.packageEmployee_id}&tel=${item.remark.package_name}&paths='messageInform'&data=${JSON.stringify(data)}`
      })
    }else{
      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.doSthForSb(true);
    }
  },
  doSthForSb(is) {
    // 代办数量
    if (!is) { this.setData({ page: 1 }) };
    this.setData({ list: [] })
    let data = {
      status: this.data.status,
      type: '1,2',
      page: this.data.page,
      page_size: 20,
    }
    app.$get('api/per/package/msg/cc', data).then(res => {
      if (res.data.code == 1) {
        let list = res.data.data.list;
        list.forEach(item => {
          if (item.remark.employee_id && app.globalData.usersList[item.remark.employee_id]) {
            // 被考核人
            item.userInfo = app.globalData.usersList[item.remark.employee_id];
          }
        });
        if (this.data.page !== 1) {
          list = this.data.list.concat(list);
        }
        let data = list;
        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,
          isData: list.length == res.data.data.total
        })
        if (this.data.tabIndex == 1) {
          this.setData({
            total: res.data.data.total,
          })
        }
      }
    })
  },
});
