// pages/duty/index/index.js
const app = getApp();

import {
  formatTime
} from "../../../../utils/util"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    topBarH: app.globalData.CustomBar,
    currentIndex: 0,
    tabs: [{
      lab: '今日工作'
    }, {
      lab: '历史工作'
    }],

    page: 1,
    pageSize: 10,
    list: [],
    dateFilter: formatTime(new Date(), '-')
  },

  /**
   * 选择菜单
   */
  tabSelect: function (params) {
    this.data.currentIndex = params.currentTarget.dataset.index;
    this.data.freshIng = true;
    this.setData(this.data)
  },

  /**
   * 时间筛选
   */
  bindDateChange: function (params) {
    this.data.dateFilter = params.detail.value;
    this.data.freshIng = true;
    this.setData(this.data);
  },

  /**
   * 刷新
   */
  refresh: function (params) {
    this.data.page = 1;
    this.getList();
  },

  /**
   * 获取日志列表
   */
  getList: function () {
    const time = this.data.currentIndex == 0 ? formatTime(new Date(), '-') : this.data.dateFilter
    app.axios("GET", "app", "/work/wkDynamicsPage", {
      page: this.data.page,
      limit: this.data.pageSize,
      startTime: time,
      endTime: time,
      userId: app.globalData.userInfo.userId
    }, false).then(res => {
      this.data.freshIng = false;
      if (res.code == 1) {
        let page = Number.parseInt(res.data.pageNum);
        if (this.data.page == 1) {
          this.data.list = res.data.list;
        } else {
          var list = this.data.list;
          if (this.data.page == page) this.data.list = [...list, ...res.data.list]
        }
        if (res.data.list?.length > 0) this.data.page = page + 1
      }
      this.setData(this.data)
    })
  },

  /**
   * 详情
   */
  toDetail: function (params) {
    const item = params.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/statistic/g-z-r-z/detail/index',
      success: res => {
        res.eventChannel.emit('getItem', item)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.data.page = 1;
    this.getList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})