// pages/pointList/pointList.js
const app = getApp()
const wxService = require('../../utils/wxService')
const utils = require('../../utils/util')
import { Integer } from '../../utils/integerDigitalConvertion'
const { memberId, member } = wx.getStorageSync('_baseUserInfo')

wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    pointList: [],
    currentStatus: 2, // 活动状态  1 未生效 2 生效中 3 已失效 0 已删除
    currentTimeStatus: 1, // 1 进行中 2 已结束 3 未开始
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) { 
    let memberId = options.memberId;
    if (memberId) {
      let tentacleInfo = {
        content: '集点列表页面',
        title: '集点列表',
        tentacleId: memberId,
        contentType: app.globalData.contants.SHARE_TYPE.SET_POINT_ACTIVITY,
        type: 3// 1:门店,2:员工(暂时不做),3:会员,4:第三方外部渠道,5:智能营销
      };
      this.generatePointObject(tentacleInfo);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const { memberId, member } = wx.getStorageSync('_baseUserInfo')
    this.getPointList()
  },
  getPointList() {
    wx.showLoading({
      title: '加载中'
    })
    wxService.get(`/sale/touchpoints/pointConsumptionActivity/findAllValidActivityList`).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          data.forEach(item => {
            // 是否在有效期
            let timeFlag = 1
            let endT = item.endTime
            let startT = item.startTime
            // ios 兼容
            let currentEndTime = (new Date(endT.replace(/-/g, '/'))).getTime();
            let currentstartTime = (new Date(startT.replace(/-/g, '/'))).getTime();
            let currentTime = (new Date()).getTime();
            
            // 开始时间 小于 现在  && 小于 结束时间 进行中
            if (currentstartTime < currentTime && currentTime < currentEndTime) {
              timeFlag = 1
            } else if (currentEndTime < currentTime) {
              // 结束时间 小于 现在时间 已结束
              timeFlag = 2
            } else if(currentTime < currentstartTime) {
              // 未开始
              timeFlag = 3
            }

            let distancetime = currentEndTime - currentTime
            let result = {
              day: '00',
              hour: '00',
              min: '00',
              sec: '00',
              ms: '00',
              isOver: true,
            };

            if (distancetime > 0) {
              //如果大于0.说明尚未到达截止时间
              result.ms = Math.floor((distancetime % 1000) / 100);
              result.sec = Math.floor((distancetime / 1000) % 60);
              result.min = Math.floor((distancetime / 1000 / 60) % 60);
              result.hour = Math.floor((distancetime / 1000 / 60 / 60) % 24);
              result.day = Math.floor((distancetime / 1000 / 60 / 60) / 24);
              result.isOver = false;

              // if (result.ms < 10) result.ms = result.ms;
              // if (result.sec < 10) result.sec = '0' + result.sec;
              // if (result.min < 10) result.min = '0' + result.min;
              // if (result.hour < 10) result.hour = '0' + result.hour;
              // if (result.day < 10) result.day = '0' + result.day;
            }
            item.countdown = result
            this.setData({
              pointList: data,
              currentStatus: item.status,
              currentTimeStatus: timeFlag
            }, () => {
              wx.hideLoading()
              this.getPointConsumptionCount(item.id)
            })
          })
        }
      }
    })
  },
  getPointConsumptionCount(activityId) {
    const { memberId } = wx.getStorageSync('_baseUserInfo')
    // 会员集点活动统计信息
    wxService.post(`/sale/touchpoints/pointConsumptionCount/getPointConsumptionCount?activityId=${activityId}&memberId=${memberId}`).then(res => {
      if (res) {
        const { result, data } = res.data
        const { pointList } = this.data
        if (result == 0) {
          pointList.forEach(item => {
            if (item.id == data.activityId) {
              item.surplusPoints = data.surplusPoints
              item.totalPoints = data.totalPoints
              item.surplusExchangeCount = data.surplusExchangeCount
              this.setData({
                pointList
              })
            }
          })
        }
      }
    })
  },
  handleDetail(e) {
    const { id } = e.currentTarget.dataset
    wxService.router(`/pages/pointDetail/pointDetail`).search({ id })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    //修改为参数memberId
    const { memberId, member } = wx.getStorageSync('_baseUserInfo')
    var path = `pages/pointList/pointList`;
    if (memberId) {
      let m = Integer.digit(memberId, 10, 64);
      path = `pages/pointList/pointList?m=${m}`;
    }
    return {
      path: path
    }
  }
})