// 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) {
    const { pageSize, pageNo, current } = this.data
    this.getTaskPage(pageSize, pageNo, current)
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  getTaskPage(pageSize, pageNo,current,refresh) { // 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=>{
            //兼容ios 
            if(item.task.startTime){
              item.task.startTime = item.task.startTime.replace(/\-/g, '/');
            }
            if (item.task.endTime) {
              item.task.endTime = item.task.endTime.replace(/\-/g, '/');
            }

            let curStart = new Date(item.task.startTime)
            let curEnd = new Date(item.task.endTime)
            
            item.task.startStr = `${curStart.getMonth() + 1}月${curStart.getDate()}日`
            item.task.endStr = `${curEnd.getMonth() + 1}月${curEnd.getDate()}日`
            if(current == 1){ //销售任务时 目标是金额
              item.task.target = parseFloat(item.task.target/100).toFixed(2);
            }
          })
          this.setData({
            list: pageNo == 1 ? [...data.content]: [...this.data.list,...data.content],
            totalPages: data.totalPages
          })
        }
      }

      if(refresh){
        wx.showToast({
          title: '刷新成功',
        });

        wx.stopPullDownRefresh();
      }
    })
  },
  // 点击事件
  //分类点击
  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,
      list : [],
    },()=>{
      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 () {
    this.data.pageSize = 10;
    this.data.pageNo = 1;
    this.data.list = [];
    const { pageSize, pageNo, current } = this.data
    this.getTaskPage(pageSize, pageNo, current,true)
  },

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