// pages/gift-detail/gift-detail.js
import {
  getMpCompaignsRewardDetail,
  getMpCompaignsActivityDetail,
  changeMpCompaignsRewards,
  changeMpCompaignsActivity
} from '../../../../services/index.js';
import {
  isPhone
} from '../../../../utils/base.js'
let phone_intTime = null;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasShare: true,
    title_arr: {
      gift: '好礼详情',
      activity: '活动详情'
    },
    message: "",
    phone: '',
    cover: true,
    input: false,
    title: true,
    hasloading: false
  },
  gift(id) {
    getMpCompaignsRewardDetail({
      user_id: wx.getStorageSync('user_id'),
      user_token: wx.getStorageSync('user_token'),
      member_id: wx.getStorageSync('member_id'),
      mpCompaignsRewardSettingId: id,
    }).then(res => {
      if (res.errcode === 0) {
        this.setData(res.data.mpCompaignsRewardSettingAPI);
        this.setData({
          hasloading: true
        })
      } else {
        this.setData({
          hasloading: false
        })
      }
    })
  },
  activity(id) {
    getMpCompaignsActivityDetail({
      user_id: wx.getStorageSync('user_id'),
      user_token: wx.getStorageSync('user_token'),
      member_id: wx.getStorageSync('member_id'),
      mpCompaignsActivitySettingId: id,

    }).then(res => {
      if (res.errcode === 0) {
        this.setData(res.data.mpCompaignsActivitySettingAPI)
        this.setData({
          hasloading: true
        })
      } else {
        this.setData({
          hasloading: false
        })
      }
    })
  },
  // 兑换
  exchange() {
    const phone = this.data.phone;
    if (isPhone(phone)) {
      const money = this.data.money;
      const type = this.data.type;
      const id = this.data.id;
      const exchangeType = this.data.exchangeType;
      const _tmp = Object.assign({
        user_id: wx.getStorageSync('user_id'),
        user_token: wx.getStorageSync('user_token'),
        member_id: wx.getStorageSync('member_id')
      }, {
        money: '',
        phone
      })
      if (exchangeType) {
        wx.navigateTo({
          url: `/pages/integral/pages/integral-pay/integral-pay?type=${this.data.type}&&phone=${phone}&&money=${money}&&id=${id}&&points=${this.data.points}`,
          success: () => {
            this.showCover()
          }
        })
        return
      }
      // 礼物兑换
      if (type === 'gift') {
        const _params = Object.assign(_tmp, {
          mpCompaignsRewardSettingId: id,
          withdrawType: 0
        })
        changeMpCompaignsRewards(_params).then(res => {
          if (res.errcode === 0) {
            this.showCover();
            this.showMessage(res, type)
          }
        }).catch(res => {
          this.showCover();
          setTimeout(_ => {
            wx.navigateBack({
              delta: 1
            })
          }, 1500)
        })
      }
      // 活动报名
      if (type === 'activity') {
        const _params = Object.assign(_tmp, {
          mpCompaignsActivitySettingId: id,
          withdrawType: 0
        })
        changeMpCompaignsActivity(_params).then(res => {
          if (res.errcode === 0) {
            this.showCover();
            this.showMessage(res, type)
          }
        }).catch(res => {
          this.showCover();
          setTimeout(_ => {
            wx.navigateBack({
              delta: 1
            })
          }, 1500)
        })
      }
      // 积分兑换订阅上报
      wx.aldPushSubscribeMessage({
        eventId: '5ea141ee7739104342928e94'
      });
    } else {
      wx.showToast({
        icon: 'none',
        title: '电话格式错误',
      })
    }
  },
  // 提示信息
  showMessage(res, type) {
    wx.showModal({
      title: res.errmsg,
      content: '您可以在兑换记录中查看详情',
      confirmText: '好的',
      showCancel: false,
      success: res => {
        if (res.confirm) {
          const _pageList = getCurrentPages();
          const _page = _pageList[_pageList.length - 2]
          _page.setData({
            integral: _page.data.integral - this.data.points
          }, _ => {
            wx.navigateBack({
              delta: 1,
            })
          })
        }
      }
    })
    // if (res.data.success) {

    //   // this.setData({
    //   //   message: "客服稍后会联系您，您也可以在兑换记录中查看详情。",
    //   //   input: true,
    //   //   title: true
    //   // })
    // } else {
    //   this.showCover();
    //   wx.showModal({
    //     title: type === 'gift' ? '兑换失败' : '报名失败',
    //     content: res.data.message,
    //     confirmText: '好的',
    //     showCancel: false,
    //     success: res => {
    //       wx.navigateBack({
    //         delta: 1,
    //       })
    //     }
    //   })
    //   // this.setData({
    //   //   message: res.data.message,
    //   //   input: true,
    //   //   title: false
    //   // })
    // }
  },
  // 保存电话
  savePhone(e) {
    const phone = e.detail.value;
    clearTimeout(phone_intTime);
    phone_intTime = setTimeout(_ => {
      this.setData({
        phone
      })
    }, 50)
  },
  // 展示蒙层
  showCover() {
    const cover = !this.data.cover;
    // const pointsBalance = wx.getStorageSync('mpMemberAccount').pointsBalance;
    // if (!cover && this.data.points > pointsBalance) {
    //   wx.showToast({
    //     icon: 'none',
    //     title: '积分不足',
    //   })
    //   return
    // }
    this.setData({
      cover,
      input: cover
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.type) {
      wx.setNavigationBarTitle({
        title: this.data.title_arr[options.type],
      })
      this.setData({
        // button: this.data.button_message[options.type],
        type: options.type,
        page_title: this.data.title_arr[options.type],
        id: options.id
      })
      // this[options.type](options.id);
    }
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (!this.data.hasloading) {
      this[this.data.type](this.data.id)
    }
  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      path: `${this.route}?type=${this.data.type}&&id=${this.data.id}`,
      imageUrl: this.data.rewardPic || this.data.activityPic,
      title: this.data.rewardName || this.data.activityName
    }
  },
  returnCover() {

  }
})