var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loveData:{
      love:0,
      ratio:0,
      least:0,
      value:0
    },
    love_nums:"",
    integral:"消费积分",
    loveName:"爱心值"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getyz_basic_info();
    this.getdata();
  },
  loading(message){
    wx.showToast({
      title:message,
      icon:'loading'
    });
  },
  getyz_basic_info(){
    try {
      let basic_info = wx.getStorageSync("yz_basic_info");
      if(basic_info){
        let integral = basic_info.integral;
        this.setData({
          integral,
          loveName:basic_info.love
        });
        wx.setNavigationBarTitle({
          title:integral
        })
      }
    } catch (error) {
      
    }
  },
  getdata(){
    this.loading('获取数据中...');
    let urlStr = app.getNetAddresss("plugin.love.Frontend.Modules.Love.Controllers.change-integral.index");
    app._postNetWork({
      url: urlStr,
      success: ({data:{result,msg,data}}) => {
        wx.hideToast();
        if(result == 1) this.setData({loveData:data});
        else this.this.$toast(msg);
      }
    })
  },
  $toast(message){
    wx.showToast({
      title: message,
      icon: "none"
    })
  },
  setdata(){
    let {love_nums,loveData} = this.data;
    if(love_nums<=0 || love_nums === ""){
      this.$toast("转化的数量不能为0，也不能为空");
      return false
    }else if((love_nums) * 1 > loveData.love * 1){
      this.$toast("转化的数量不能大于总数量");
      return false
    }else if(love_nums * 1 > (loveData.love - loveData.least) * 1){
      this.$toast("最低保留"+ loveData.least+"爱心值")
      return false
    }
    this.loading("正在转化中...");
    let urlStr = app.getNetAddresss("plugin.love.Frontend.Modules.Love.Controllers.change-integral.change");
    app._postNetWork({
      url: urlStr,
      data:{love_nums},
      success: ({data:{result,msg,data}}) => {
        wx.hideToast();
        if(result == 1){
          wx.showToast(data);
          setTimeout(()=>{
            this.getdata();
          },1000)
        }else this.$toast(msg);
      }
    })
  },
  _bindinput({detail:{value}}){
    this.setData({love_nums:value})
  },
  allLove(){
    let {love,least} = this.data.loveData;
    if(love<=least){
      this.$toast("当前没有可转换的" + this.data.loveName);
      return false;
    }
    this.setData({love_nums:love - least});
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

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

  },

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

  },

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

  }
})