// pages/member/rankingListSecond/rankingListSecond.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 转化受让人ID
    recipientId: "",
    // 转化消费积分
    changeIntegral: "",
    // 实际到账爱心值
    actualIntegral: 0,
    // 当前消费积分
    integral: 0,
    // 转化点击后网络提交加载
    loading: false,
    integral_name: '',
    love_name: '',
    checkRadio:'1',
    shiftPoint:false,
    nickname:'',
    timerId:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.type == 'shift'){
      this.setData({
        shiftPoint:true
      })
    }
    this._getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
		
  },
  onChange(e){
    this.setData({
      checkRadio: e.detail,
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.isclick = true;
  },

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

  },

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

  },

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

  },

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

  },

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

  },
  // id输入
  idChange({ detail }) {
    this.setData({
      recipientId: detail
    });
    if(this.data.timerId){clearTimeout(this.data.timerId)}
    this.data.timerId = setTimeout(() => {
      this.getNickName()
    }, 800);
  },
  getNickName(){
    if(this.data.recipientId){
      let url = app.getNetAddresss("member.member.memberInfo");
      app._getNetWork({
        url,
        data:{uid:this.data.recipientId},
        success: res => {
          if (res.data.result === 1) {
            this.setData({
              nickname:res.data.data.nickname
            });
          } else {
     
            this.setData({
              nickname:''
            });
          }
        }, fail: err => {
          console.log(err);
        }
      });

    }
  },
  // 转化消费积分输入
  integralChange({ detail }){
    var reg = /^[0-9]+.?[0-9]*$/;
    if (!reg.test(detail)) {
      this.setData({
        changeIntegral: ''
      });
      wx.showToast({
        title: '只能输入数字',
        icon: 'none'
      });
    } else {
      let info = this.data.info;
      let actualIntegral
      if(this.data.shiftPoint){
        actualIntegral =  Number(detail) / info.point_transformation_amount*info.point_transformation_ratio;
      }else{
        actualIntegral = Number(detail) / info.transformation_amount*info.transformation_ratio;
      }
      this.setData({
        changeIntegral: detail,
        actualIntegral:actualIntegral?actualIntegral:0
      });
    }
  },

  // 获取数据，获取当前消费积分
  _getData() {
    let url = app.getNetAddresss("plugin.integral.Frontend.Controllers.Page.transfer-love");
    app._getNetWork({
      url,
      success: res => {
        if (res.data.result === 1) {
          this.setData({
            integral: res.data.data.member_integral,
            integral_name: res.data.data.integral_name,
            love_name: res.data.data.love_name,
            info:res.data.data
          });
          wx.setNavigationBarTitle({
            title: this.data.shiftPoint?`转化${res.data.data.point_name}`:`转化${res.data.data.love_name}`
          });
        } else {
          console.log(res.data.msg);
        }
      }, fail: err => {
        console.log(err);
      }
    });
  },

  // 确认转化
  sureChange() {
    this.setData({
      loading: true
    });
    if(this.data.nickname == ''){
      wx.showToast({
        title: '该会员不存在',
        icon: 'none',
        duration: 1500
      });
      this.setData({
        loading: false
      });
      return;
    }
    // 判断输入转化数额
    if (!this.data.changeIntegral || this.data.changeIntegral.trim() == '') {
      wx.showToast({
        title: '请输入转化数额',
        icon: 'none',
        duration: 1500
      });
      this.setData({
        loading: false
      });
      return;
    }
    // 判断输入受让人ID转化
    if ((!this.data.recipientId || this.data.recipientId.trim() == '') && this.data.checkRadio == '2') {
      wx.showToast({
        title: '请输入受让人ID',
        icon: 'none',
        duration: 1500
      });
      this.setData({
        loading: false
      });
      return;
    }
    // 判断转化是否大于当前消费积分
    if (Number(this.data.changeIntegral) > Number(this.data.integral)) {
      wx.showToast({
        title: `输入的${this.data.changeIntegral}数额超过已有的${this.data.integral}`,
        icon: 'none'
      });
      this.setData({
        loading: false
      });
      return;
    }
    let json = {
      change_value: this.data.changeIntegral,
      recipient_id: this.data.recipientId,
      give_type:this.data.checkRadio
    }
    let url;
    if(this.data.shiftPoint){
      url = app.getNetAddresss('plugin.integral.Frontend.Modules.Integral.Controllers.Transfer.point-transformation');
    }else{
      url = app.getNetAddresss('plugin.integral.Frontend.Modules.Integral.Controllers.Transfer.love-transformation');
    }
    app._getNetWork({
      url,
      data: json,
      success: res => {
        this.setData({
          loading: false
        });
        if (res.data.result === 1) {
          wx.showToast({
            title: '转化成功',
            icon: 'none',
            duration:1500,
            complete: () => {
              setTimeout(() => {
                wx.navigateTo({
                  url: '/packageB/member/Integral_love_detail/Integral_love_detail',
                })
              }, 1500);
            }
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          });
        }
      }
    });
  }
});