// pages/gift/gift.js
import {
  getMpCompaignsReward,
  getMpCompaignsActivity
} from '../../../../services/index.js';
import { imgServerUrl } from "../../../../config/config"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: {
      gift: '兑好礼',
      activity: '兑活动'
    },
    button_message: {
      gift: '立即兑换',
      activity: '立即报名'
    },
    button: '',
    list: [],
    pageNo: 1,
    type: '',
    isLast: false,
    integral: '0'
  },
  // 好礼列表
  gift(pageNo = this.data.pageNo) {
    let params = {
      user_id: wx.getStorageSync('user_id'),
      user_token: wx.getStorageSync('user_token'),
      member_id: wx.getStorageSync('member_id'),
      pageNo
    }
    getMpCompaignsReward(params).then(res => {
      if (res.errcode === 0) {
        pageNo++;
        this.setData({
          list: [...this.data.list, ...res.data.mpCompaignsRewardList],
          isLast: res.data.isLast,
          pageNo
        })
      }
    })
  },
  // 活动列表
  activity(pageNo = this.data.pageNo) {
    let params = {
      user_id: wx.getStorageSync('user_id'),
      user_token: wx.getStorageSync('user_token'),
      member_id: wx.getStorageSync('member_id'),
      pageNo
    }
    getMpCompaignsActivity(params).then(res => {
      if (res.errcode === 0) {
        pageNo++;
        this.setData({
          list: [...this.data.list, ...res.data.mpCompaignsActivityList],
          isLast: res.data.isLast,
          pageNo
        })
      }
    })
  },
  // toExchangeRecord(e) {
  //   wx.navigateTo({
  //     url: e.currentTarget.dataset.url
  //   })
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    /* 
      获取积分页 路由type字段
      @gift  礼物
      @activity  活动
     */
    if (options.type) {
      wx.setNavigationBarTitle({
        title: this.data.title[options.type],
      })
      this.setData({
        button: this.data.button_message[options.type], // 设置按钮内容
        type: options.type, // 设置type
        integral: options.integral // 设置按钮内容
      })
      this.setData({
        pageNo: 1,
        list: []
      }, _ => {
        this[options.type]();
      })
    }
  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      pageNo: 1,
      list: []
    }, _ => {
      this[this.data.type]();
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const isLast = this.data.isLast;
    if (isLast) {

    } else {
      this[this.data.type]();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: `/${this.route}?type=${this.data.type}&integral=${this.data.integral}`,
      // imageUrl: imgServerUrl + '/images/happyjob/detail_share.jpg',
      title: this.data.title[this.data.type]
    }
  }
})