// shoppingGuid/page/pages/saleTaskInfo/saleTaskInfo.js
const wxService = require('../../../../utils/wxService')
wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    navBar: [{ name: '线上订单', type: 1, isActive: true }, { name: '线下订单', type: 2, isActive: false }],
    current: 1,
    saleDetail: null
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let taskId = this.options && this.options.id || 0
    let orderType = this.options && this.options.type || 1
    this.getTaskDetail(orderType, taskId)
  },
  //获取任务详情
  getTaskDetail(orderType,taskId) {
    wx.showLoading({
      title: '加载中'
    })
    wxService.post(`/marketing/shoppingguide/task/detail?orderType=${orderType}&taskId=${taskId}`).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          if (data.task.startTime) {
            data.task.startTime = data.task.startTime.replace(/\-/g, '/');
          }
          if (data.task.endTime) {
            data.task.endTime = data.task.endTime.replace(/\-/g, '/');
          }

          let curStart = new Date(data.task.startTime)
          let curEnd = new Date(data.task.endTime)
          data.task.startTime = `${curStart.getMonth() + 1}月${curStart.getDate()}日`
          data.task.endTime = `${curEnd.getMonth() + 1}月${curEnd.getDate()}日`
          data.task.target = parseFloat(data.task.target / 100).toFixed(2);

          this.setData({
            saleDetail: data || {}
          })
        }
      }
    })
  },
  // 点击事件
  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({
      navBar: this.data.navBar,
      current: this.data.current
    });
  },
  //跳转到活动说明界面
  onTapToNotice(){
    let taskId = this.options && this.options.id || 0
    let orderType = this.options && this.options.type || 1;

    wx.navigateTo({
      url: `/shoppingGuid/page/pages/taskNotice/taskNotice?id=${taskId}&type=${orderType}`,
    });
  }
});