// packageB/member/Integral_invest/Integral_invest.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    certificates: "",
    explain_that: "",
    plugin_name: "",
    integral: ""
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    this.getData();
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  // 选择图片
  onRead() {
    let that = this;
    let urlStr = app.getNetAddresss('upload.uploadPic');
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        wx.compressImage({
          src: tempFilePaths[0], // 图片路径
          quality: 60, // 压缩质量
          success(e) {
            let photourl = e.tempFilePath;
            wx.uploadFile({
              url: urlStr,
              filePath: photourl,
              name: 'file',
              success(resdata) {
                var res = JSON.parse(resdata.data);
                if (res.result == 1) {
                  that.setData({
                    certificates: res.data.img_url
                  });
                } else {
                  wx.showToast({
                    icon: 'none',
                    title: res.msg,
                    duration: 1500
                  });
                }
              }
            });
          },
          fail(e) {},
          complete(e) {}
        });
      }
    });
  },
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.integral.Frontend.Controllers.recharge.display");
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            explain_that: res.data.explain_that,
            plugin_name: res.data.plugin_name
          });
          if (res.data.plugin_name) {
            wx.setNavigationBarTitle({
              title: res.data.plugin_name
            });
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  confirm() {
    if (!this.data.certificates) {
      wx.showToast({
        icon: 'none',
        title: '请上传充值凭证',
        duration: 1500
      });
      return;
    }
    if (!this.data.integral) {
      wx.showToast({
        icon: 'none',
        title: '请填写充值积分',
        duration: 1500
      });
      return;
    }
    if (this.data.integral <= 0) {
      wx.showToast({
        icon: 'none',
        title: '充值积分必须大于0',
        duration: 1500
      });
      return;
    }
    let json = {
      certificates: this.data.certificates,
      integral: this.data.integral
    };
    
    let urlStr = app.getNetAddresss("plugin.integral.Frontend.Controllers.recharge.recharge");
    app._getNetWork({
      url: urlStr,
      data:json,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          wx.navigateBack({
            delta: 1,
            complete() {
              wx.showToast({
                icon: 'none',
                title: "充值成功",
                duration: 1500
              });
            }
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  integralChange ({ detail }) {
    console.log(detail);
    this.setData({
      integral: detail.value
    });
  }
});
