// pages/user/walletDetails/walletDetails.js
import Date from '../../../utils/util.js'
import APIClient from '../../../utils/APIClient.js'
import utils from '../../../utils/util'
Page({

  data: {
    buyBtnText: '立即开通',
    price: '29',
    bottomText: '原价99元/年',
    productId: 0,
    modalType: 'image',
    isModalShow: false,
    isVIP: false
  },
  onLoad: function (options) {
    if (options.title) {
      wx.setNavigationBarTitle({
        title: VIP购买
      });
    }
    APIClient.getProductData().success(res => {
      this.setData({
        price: res.data.data.price,
        productId: res.data.data.id
      })
    }).fail(err => {
      console.log(err);
    });
    APIClient.getUserAuth().success(res => {
      if (res.data.data) {
        this.setData({
          isVIP: true,
          bottomText: `有效期：${utils.formatDate(res.data.data.endTime,2)}`
        })
      } else {
        this.setData({
          isVIP: false
        })
      }
    })
  },

  createOrder() {
    APIClient.postOrder(this.data.productId).success(res => {
      this.prePayMap(res.data.data)
    }).fail(err => {
      console.log(err);
    });
  },
  prePayMap(prePayMap) {
    const that = this;
    wx.requestPayment({
      'appId': prePayMap.appId,
      'timeStamp': prePayMap.timeStamp,
      'nonceStr': prePayMap.nonceStr,
      'package': prePayMap.package,
      'signType': 'MD5',
      'paySign': prePayMap.sign,
      'success': function (res) {
        that.setData({
          isModalShow: true,
        })
      },
      'fail': function (res) {
        console.log('支付失败', res)
        
      }
    })
  },
  modalConfirmHandler() {
    this.setData({
      isModalShow: false
    })
    wx.redirectTo({
      url: `/pages/index/index`
    })
  },
})