// shoppingGuid/page/pages/kpi/kpi.js
const app = getApp()
const wxService = require('../../../../utils/wxService')
wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    navBar: [
      { name: '销售任务', type: 1, isActive: true }, 
      { name: '招募任务', type: 2, isActive: false },
      // { name: '内容任务', type: 3, isActive: false },
      // { name: '维护任务', type: 4, isActive: false }
    ],
    current: 1,
    list : [],
    pageSize: 10,
    pageNo: 1,
    totalPages: 0
  },

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

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const {pageSize, pageNo,current} = this.data
    this.getTaskPage(pageSize, pageNo,current)
  },
  getTaskPage(pageSize, pageNo,current) { // 1 销售任务 2 招募任务
    wx.showLoading({
      title: '加载中'
    })
    const query = {
      type: current,
      brandId: app.globalData.brandId,
      // pageSize,
      // pageNo,
    }
    wxService.post(`/marketing/shoppingguide/task/page?pageSize=${pageSize}&pageNo=${pageNo}`,query).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          data.content.forEach(item=>{
            let curStart = new Date(item.task.startTime)
            let curEnd = new Date(item.task.endTime)
            item.task.startTime = `${curStart.getMonth()}月${curStart.getDay()}日`
            item.task.endTime = `${curEnd.getMonth()}月${curEnd.getDay()}日`
          })
          this.setData({
            list: pageNo == 1? [...data.content]: [...this.data.list,...data.content],
            totalPages: data.totalPages
          })
        }
      }
    })
  },
  // 点击事件
  //分类点击
  onTopBarTap(e){
    let index = e.currentTarget.dataset.index,
      item = e.currentTarget.dataset.item;
    this.data.navBar.forEach(nav => {
      nav.isActive = false;
    });

    this.data.navBar[index].isActive = true;
    this.data.current = this.data.navBar[index].type;

    this.setData({
      pageNo: 1,
      totalPages: 0,
      navBar: this.data.navBar,
      current: this.data.current
    },()=>{
      if (wx.pageScrollTo) {
        wx.pageScrollTo({
          scrollTop: 0
        })
      }
      const {pageSize, pageNo,current} = this.data
      this.getTaskPage(pageSize, pageNo,current)
    });
  },
  //销售任务item点击事件
  onTapToSaleTaskInfo(e){
    let item = e.currentTarget.dataset.item.task;
    let id = item.id || 0;
    let type = item.type || 1;

    wx.navigateTo({
      url: '/shoppingGuid/page/pages/saleTaskInfo/saleTaskInfo?id=' + id + '&type=' + type,
    });

  },
  //招募任务item点击事件
  onTapToInviteTaskInfo(e){
    let item = e.currentTarget.dataset.item.task;
    let id = item.id || 0;
    let type = item.type || 2;

    wx.navigateTo({
      url: '/shoppingGuid/page/pages/enlistInfo/enlistInfo?id=' + id + '&type=' + type,
    });
  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('-------------')
    if(pageNo < totalPages) {
      this.setData({
        pageNo: this.data.pageNo + 1,
      },()=>{
        const {pageSize, pageNo,current} = this.data
        this.getTaskPage(pageSize, pageNo,current)
      })
    }
  }
})