// packageH/superior_payment/superiorPaymentPay/superiorPaymentPay.js
var yz_pay = require("../../../mycomponent/yz_pay/yz_pay");
var app = getApp();
let plugin_name="上级代付";
Page({
  behaviors: [yz_pay],
  /**
   * 页面的初始数据
   */
  data: {
    goodList: [],
    totalNum: 0,
    totalPrice: '',

    order_pay_id: '',
    payerImg: '',
    payerName: '',
    pid: '',
    mid: '',

    //支付键盘
    payKeyboardShow:false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options:', options);
    this.setData({
      order_id: options.order_id,
      mid: options.mid,
      pid: options.pid
    });

    this.getPayerInfo();
    this.getOrderDetail();
  },

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

  },

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

  },

  //获取代付会员信息和支付信息
  getPayerInfo: function() {
    var _url = app.getNetAddresss("order.merge-pay.anotherPayOrder");
    _url += "&order_ids=" + this.data.order_id + '&mid=' + this.data.mid;
    _url += '&pid=' + this.data.pid;
    _url += "&plugin=parent_payment";
    app._getNetWork({
      url: _url,
      success: (response) => {
        //var _data = res.data.data;
        if (response.data.result == 1) {
          var rs = response.data.data;
          this.setData({
            payerImg: rs.member.avatar,
            payerName: rs.member.nickname,
            order_pay_id: rs.order_pay.id,
            totalPrice:rs.order_pay.amount
          });
          let btns = [],buttons=rs.buttons;
          for(let i in buttons){
            if(buttons[i].value==1 || buttons[i].value==3){
              btns.push(buttons[i]);
            }
          }
          this.setData({buttons:btns});
          wx.setNavigationBarTitle({
            title: response.data.data.plugin_name || plugin_name,
          });
        } else {
          wx.showToast({
            title: response.data.msg,
            icon: 'none',
            duration: 2000,
            success: function() {
              wx.reLaunch({
                url: '/packageG/index/index'
              });
            },
            fail: function() {}
          });
          console.log("错误", response.data.msg);
        }
      },
      fail: function(res) {}
    });
  },
  getOrderDetail: function() {
    var _url = app.getNetAddresss("order.another-pay-detail");
    _url += "&order_id=" + this.data.order_id + '&mid=' + this.data.mid;
    _url += '&pid=' + this.data.pid;
    app._getNetWork({
      url: _url,
      success: (response) => {
        //var _data = res.data.data;
        console.log("ressss::::", response);
        if (response.data.result == 1) {
          this.formatGoods(response.data.data);
        } else {
          app.tips(response.data.msg);
          console.log("错误", response.data.msg);
        }
      },
      fail: function(res) {}
    });
  },

  formatGoods(list){
    let numSum = 0;
    let goods=[];
    list.forEach(val => {
      goods.push(...val.has_many_order_goods);
      return (numSum += val.goods_total);
    });
    this.setData({
      totalNum:numSum,
      goodList:goods
    });
  },

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

  },

  confirm: function(e) {
    let type = e.currentTarget.dataset.value;
    let btn = e.currentTarget.dataset.btn;
    this.handlePay(btn, type);
  },

});
