const app = getApp();

Page({


  data: {
    OccupationArray: [], // 职业-数据

    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 arrData = res.data;
                          // console.log(res.data)
                          var averageprice ='';

                          for ( var i in res.data){

                            if (arrData[i].day && arrData[i].day  != 0) {
                          
                            //  计算日均价  四舍五入
                              averageprice = Number(arrData[i].total_price / arrData[i].day);    
                              averageprice = averageprice.toFixed(2);   //取小数点后两位
                              arrData[i].average_price = averageprice;

                              // console.log(averageprice);

                              }

                          }

                          that.setData({
                            OccupationArray: arrData,
                          })
              }
    
    })

  },

  // 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) {


  // },




  // 勾选
  checkboxChange: function (e) {
    console.log(e.detail.value)
    var that =this;

   var Price= ''  //价格
   var  Originalprice=''   //原价
   var  day='' //  天




    var dataid = e.detail.value;
    var data = that.data.OccupationArray;
    // console.log(data)

    for (let i = 0; i < data.length; i++) {    //已选中的数据处理
      if (data[i].id == dataid) {
        Price = data[i].discount_peice
        Originalprice = data[i].total_price
        day  =  data[i].time

      }
    }

    this.setData({
      Priceid: dataid, //价格ID
      Price: Price,  //原价
      Originalprice: Originalprice,  //优惠价格
      day: day,
    })
    
    console.log(day);
    console.log(Originalprice);
    console.log(Price);

    console.log('checkbox发生change事件，携带value值为：', e.detail.value);
  },



  pay: function () {
    let _that = this;
    wx.login({
      success: function (res) {
        console.log(res.code)
        if (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) {


          //   }
          // })
        }
      }
    })
  },



  
})