// packageI/newMedia/newMediaRechange/newMediaRechange.js
var yz_pay = require("../../../mycomponent/yz_pay/yz_pay"); //引入支付方法
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [yz_pay],
  data: {
    details:{},
    quantity:'',
    price:0,
    buttons:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getStream();
  },

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

  },

  setPrice(){
    let price = this.data.quantity * this.data.details.stream_eq_money;
    this.setData({price});
  },

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

  },

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

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {  
    let pages = getCurrentPages();
    let page = pages[pages.length-2];
    page.getBehavior(true);
  },

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

  },

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

  },

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

  },

  getStream(){
    let urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.stream.recharge-stream");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);

        let buttons = res.data.buttons;
        buttons.forEach((item)=>{
          if(item.value==3){
            item.class="icon-balance_yue";
          }else if(item.value==55){
            item.class="icon-balance_i";
          }
        });

        this.setData({
          details:res.data,
          buttons
        });
      }
    });
  },


  async getPaySn(evt){
    let item = evt.currentTarget.dataset.item;
    let pay_type = item.value;
    let quantity = this.data.quantity;
    if(quantity<=0) return app.tips("充值数量需大于1");
    let urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.stream.get-pay-sn");
    if(item.need_password==true){
      let pass = await this.getPayKeyboardPassword();
      urlStr += "&password="+pass;
    }
   
    app._getNetWork({
      url: urlStr,
      data:{pay_type,stream:quantity},
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) {
          if(pay_type==3){
            this.verifyPasswordError(res);
            return;
          }else return app.tips(res.msg);
        }
        if(pay_type==3){
          this.rechargePay(pay_type,{});
        }else {
          this.alipay(res.data.order_sn);
        }
      }
    });
  },

  alipay(order_sn){
    let urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.stream.alipay");
    app._getNetWork({
      url: urlStr,
      data:{order_sn},
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        this.rechargePay(1,res);
      }
    });
  }




});