// pages/integral/integral.js
import {
  myPoint,
  todaySign,
  todaySupSign,
  getReward
} from '../../../services/index.js';
const params = {
  user_id: wx.getStorageSync('user_id'),
  user_token: wx.getStorageSync('user_token'),
  member_id: wx.getStorageSync('member_id'),
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sign_list: [{}, {}, {}, {}, {}, {}, {}, ]
  },

  /* 
    查看积分规则
    @rule 签到说明
    @dec  积分规则
   */
  checkRule(e) {
    const type = e.currentTarget.dataset.type;
    const content = type === 'rule' ? this.data.mpPointRule.ruleDescription : this.data.mpSigninDescription.description;
    wx.setStorage({
      key: type,
      data: content,
      success: _ => {
        wx.navigateTo({
          url: `/pages/integral/pages/integral-rule/integral-rule?type=${type}`
        })
      }
    })
  },

  /* 
   兑换列表
   @gift 礼物页面
   @activity  活动页面
  */
  toPage(e) {
    const type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: `/pages/integral/pages/gift/gift?type=${type}&&integral=${this.data.mpMemberAccount.pointsBalance}`,
    })
  },
  // 签到
  sign() {
    const isSign = this.data.isSign;
    const isSupSign = this.data.isSupSign;
    const supplementarySignin = this.data.supplementarySignin;
    const noSign = this.data.noSign;
    // 如果签到跟补签状态都是false  先签到
    if (!isSign && !isSupSign) {
      todaySign({
        user_id: wx.getStorageSync('user_id'),
        user_token: wx.getStorageSync('user_token'),
        member_id: wx.getStorageSync('member_id')
      }).then(res => {
        if (res.errcode === 0) {
          wx.showToast({
            mask:true,
            icon: 'none',
            title: `签到成功 积分+${res.data.signPoint}`,
          })
          this.getSign();
        }
      })
    }
    // 如果签到状态是true  补签状态是false  能否补签状态是true  漏签数量大于0 补签
    if (isSign && !isSupSign && supplementarySignin && noSign) {
      todaySupSign({
        user_id: wx.getStorageSync('user_id'),
        user_token: wx.getStorageSync('user_token'),
        member_id: wx.getStorageSync('member_id')
      }).then(res => {
        if (res.errcode === 0) {
          wx.showToast({
            mask:true,
            icon: 'none',
            title: `补签成功 积分+${res.data.signPoint}`,
            success: res => {
              setTimeout(() => {
                this.getSign();
              }, 1500)
            }
          })
        }
      })
    }
    // 如果签到状态是true  补签状态是false  能否补签状态是false  漏签数量大于0 提示完成任务补签
    if (isSign && !isSupSign && !supplementarySignin && noSign) {
      wx.showModal({
        title: '补签说明',
        content: '完成今天全部的每日任务可以获得一次补签机会~',
        confirmText: '知道了',
        showCancel: false,
        success: res => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/integral/pages/task/task',
            })
          }
        }
      })
    }

    // 如果签到跟补签状态都是true 提示次日补签
    if (isSign && isSupSign) {
      wx.showModal({
        title: '补签说明',
        content: '您今天已经补签过了，明天继续完成任务还能获得补签机会哦！',
        confirmText: '知道了',
        showCancel: false
      })
    }
  },
  /* 
    领取奖励
    @taskId  任务ID
    @taskType 任务类型（0：新手任务 1：日常任务）
   */
  reward(e) {
    const {
      id,
      index,
      points
    } = e.currentTarget.dataset;
    const _params = Object.assign({
      user_id: wx.getStorageSync('user_id'),
      user_token: wx.getStorageSync('user_token'),
      member_id: wx.getStorageSync('member_id')
    }, {
      taskId: id,
      taskType: 1
    });
    getReward(_params).then(res => {
      if (res.errcode === 0) {
        // const arr = this.data.day_task;
        // const mpMemberAccount = this.data.mpMemberAccount;
        // arr[index].is_get = true;
        // mpMemberAccount.pointsBalance = mpMemberAccount.pointsBalance + points;
        wx.showToast({
          icon: 'none',
          title: `领取成功 积分+${points}`,
        })
        this.setData({
          [`day_task[${index}].is_get`]: true,
          'mpMemberAccount.pointsBalance': this.data.mpMemberAccount.pointsBalance + points
        })
      }
    })
  },
  // 去完成
  toDetail() {
    wx.navigateTo({
      url: '/pages/fulltime/index',
    })
  },
  // 获取签到签到状态
  getSign() {
    myPoint({
      user_id: wx.getStorageSync('user_id'),
      user_token: wx.getStorageSync('user_token'),
      member_id: wx.getStorageSync('member_id')
    }).then(res => {
      if (res.errcode === 0) {
        wx.setStorageSync('mpMemberAccount', res.data.mpMemberAccount)
        this.setData(res.data)
      }
    })
  },

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

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getSign()
  },

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

  },

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

  },

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

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

  },

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

  }
})