import { getPayInfo, getWalletInfo, createOrder, getCode } from './common/user';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    pay_list: [],
    total_coin: 0,
    rectHeight: wx.getMenuButtonBoundingClientRect().height,
    appid: '',
    channel_name: "",
    session_key: "",
    source_name: "",
    version: "",
    openid: '',
    backAppid: '',
    backUrl: '',
    video_id: ''
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var obj = wx.getLaunchOptionsSync()
    console.log(this.data, 'data', obj, obj.query.session_key);
    if (obj.query.appid) {
      this.setData({
        channel_name: obj.query.channel_name,
        session_key: obj.query.session_key,
        source_name: obj.query.source_name,
        version: obj.query.version,
        backAppid: obj.query.appid,
        backUrl: obj.query.url,
        video_id: obj.query.video_id ? obj.query.video_id : '',
        appid: wx.getAccountInfoSync().miniProgram.appId
      })
      this.toLogin()
    }
  },
  toLogin () {
    let that = this
    wx.login({
      success: function (res) {
        let _params = {
          appid: that.data.appid,
          channel_name: that.data.channel_name,
          session_key: that.data.session_key,
          source_name: that.data.source_name,
          version: that.data.version,
        }
        let params = {
          code: res.code,
          ..._params
        }
        console.log(params, 'params');
        if (res.code) {
          getCode(params).then(item => {
            console.log(item, 'login', params);
            that.setData({
              openid: item.data.data.open_id
            })
            that.getWalletData()
            that.loadWalletInfo()
            console.log(item.data.data.open_id, 'userinfo');
          })
        } else {
        }
      }
    })
  },

  //加载充值列表
  getWalletData () {
    let params = {
      appid: this.data.appid,
      channel_name: this.data.channel_name,
      session_key: this.data.session_key,
      source_name: this.data.source_name,
      version: this.data.version,
    }
    getPayInfo(params).then(res => {
      this.setData({
        pay_list: res.data.data
      })
    })
  },

  //加载钱包余额
  loadWalletInfo () {
    let params = {
      appid: this.data.appid,
      channel_name: this.data.channel_name,
      session_key: this.data.session_key,
      source_name: this.data.source_name,
      version: this.data.version,
    }
    getWalletInfo(params).then(res => {
      if (res.data.code == 200) {
        this.setData({
          total_coin: res.data.data.total_coin
        })
      }
    });
  },
  back () {
    let that = this
    console.log('1111', that.data.backAppid, that.data.backUrl);
    wx.navigateToMiniProgram({
      appId: that.data.backAppid,
      path: `${that.data.backUrl}?video_id=${that.data.video_id}`,
      extraData: {
        foo: 'bar'
      },
      envVersion: 'release',
      success (res) {
        // 打开成功
      }
    })
    return
  },
  //创建支付订单
  toOrder (e) {
    let item = e.currentTarget.dataset.item
    let params = {
      appid: this.data.appid,
      channel_name: this.data.channel_name,
      session_key: this.data.session_key,
      source_name: this.data.source_name,
      version: this.data.version,
    }
    let that = this
    createOrder({ pay_info_id: item.id, ...params, open_id: this.data.openid }).then(res => {
      console.log(res.data.code, 'creat');
      if (res.data.code == 200) {
        wx.requestPayment({
          timeStamp: res.data.data.timeStamp,
          nonceStr: res.data.data.nonceStr,
          package: res.data.data.package,
          signType: res.data.data.signType,
          paySign: res.data.data.paySign,
          success: function (res) {
            console.log('success');
            that.loadWalletInfo()
            that.back()
          },
          fail: function (res) { }
        })
      }
    })
  },
  // 监听页面隐藏
  onHide () {
  },

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

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

  },
})