const app = getApp();

Page({


  data: {
    OccupationArray: [], // 职业-数据
    Occupationindex: 0,  // 职业-下标

    Price:'',   //价格
    Originalprice:'',   //价格
    day:'', //  天
    Priceid: '', //  价格ID
    
    
  },

  onLoad: function (options) {

    var that =this;
    //  职业 ajax 
    let Occupation_url = 'pay/recharge/setmealinfo';
    let Occupation_data = {
      pid: '0'
    };
    app.postRequest(Occupation_url, Occupation_data, function (res) {      //  职业 ajax
      if (res.code == 201) {
        
          //  计算日均价  四舍五入
        var averageprice = Number(res.data[0].discount_peice / res.data[0].day);
        averageprice = averageprice.toFixed(2);   //取小数点后两位
        // console.log(averageprice)

        that.setData({
          OccupationArray: res.data,
          Price: res.data[0].total_price,  //原价
          Originalprice: res.data[0].discount_peice,  //优惠价格
          Priceid: res.data[0].id,  //价格ID  
          day: averageprice,    //  日均价
        })
      }
    
    })

  },

  OccupationEvent: function (e) {   //  职业    单项选择器
  
    var _discount_peice =  this.data.OccupationArray[e.detail.value].discount_peice   //优惠价格
    var _day = this.data.OccupationArray[e.detail.value].day   //天

    //  计算日均价  四舍五入
    var _averageprice = Number(_discount_peice / _day);
    _averageprice = _averageprice.toFixed(2);   //取小数点后两位

    // console.log(this.data.OccupationArray[e.detail.value].id);
    // console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
      Priceid:this.data.OccupationArray[e.detail.value].id, //价格ID
      Price:this.data.OccupationArray[e.detail.value].total_price,  //原价
      Originalprice: this.data.OccupationArray[e.detail.value].discount_peice,  //优惠价格
      Occupationindex: e.detail.value,
      day: _averageprice,
    })

  },


  sfzfmclick(e) {


  },
  pay: function () {
    let _that = this;
    wx.login({
      success: function (res) {
        console.log(res.code)
        if (res.code) {
          // let p_url = 'public/wxlogin';
          // let p_data = {
          //   code: res.code,
          // };
          var _Priceid = _that.data.Priceid
          let payment_url = 'pay/wxpay/index';
          let payment_data = {
            rid: _that.data.Priceid,
            total_price: _that.data.Price,  //原价
            discount_peice: _that.data.Originalprice,  //优惠
            code: res.code
          };
          app.postRequest(payment_url, payment_data, function (res) { 

            console.log(res.data)
            if (res.code == 201) {

              wx.requestPayment({
                timeStamp: res.data.timeStamp,
                nonceStr: res.data.nonceStr,
                package: res.data.package,
                signType: res.data.signType,
                paySign: res.data.paySign,

                success(res) {
                  wx.showToast({
                    title: '支付成功',
                    icon: 'success',
                    duration: 2000
                  })
                },
                fail(res) {
                    getApp().Tips('支付失败');
                }
              })
              return
            }
              getApp().Tips('支付失败');
          })

          // app.postRequest(p_url, p_data, function (res) {
          //   console.log(res);
          //   if (res.code == 201) {


          //   }
          // })
        }
      }
    })
  }


})