// pages/donationDetail/donationDetail.js
import Notify from '../../dist/notify/notify';

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    donationId:0,
    donationDat:null,
    userDonationList:[],
    money:'',
    comment:'',
    showDilog:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (options != null) {
      that.setData({
        donationId: options.donationId,
      })
    }
    
    var config = wx.getStorageSync('config');
    //获取详细信息
    wx.request({
      url: app.url + 'ajax_get_donation_event_detail.php',
      data: {
        unionId: config.unionId,
        donationId: that.data.donationId
      },
      header: { 'content-type': 'application/json' },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        console.log(res)
        if (res.statusCode == 200) {
          that.setData({
            donationDat: res.data.result.donationDat,
            userDonationList: res.data.result.userDonationList
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

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

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

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

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

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

  },

  onInputMoney(event){
    this.setData({
      money: event.detail.value
    })
  },
  onInputComment(event) {
    this.setData({
      comment: event.detail.value
    })
  },

  //取消捐款
  cancelDonation(){
    this.setData({
      showDilog:0
    })
  },

  //显示捐款对话框
  donationAction(){
    this.setData({
      showDilog: 1
    })
  },

  //捐款支付
  donationPay(){
    var that=this;
    //金额必须输入
    if (that.data.money == null || that.data.money.trim() == "") {
      Notify('请输入捐款金额')
      return
    }
    //金额必须是数字
    var isMoney = this.isPriceNumber(that.data.money)
    if (!isMoney) {
      Notify('捐款金额格式错误')
      return
    }
    //赠言必须输入
    if (that.data.comment == null || that.data.comment.trim() == "") {
      Notify('请输入捐赠留言')
      return
    }
    //调用支付接口
    var config = wx.getStorageSync('config');
    wx.request({
      url: app.url + 'ajax_set_user_donation_dat.php',
      data: {
        unionId: config.unionId,
        donationId: that.data.donationId,
        comment: that.data.comment,
        money: that.data.money
      },
      success: function (resp) {
        console.log(resp)
        if (resp.data.status == 'OK') {
          var result = JSON.parse(resp.data.result);
          wx.requestPayment({
            timeStamp: result.timeStamp,
            nonceStr: result.nonceStr,
            package: result.package,
            signType: 'MD5',
            paySign: result.paySign,
            success(res) {
              //刷新本页面
              Notify('捐赠成功!')
              that.setData({
                money: '',
                comment: '',
                showDilog: 0,
              })
              that.onLoad();
            },
            fail(res) {
              Notify('支付失败!')
            }
          })
        }
      }
    })
  },

  //判断金额
  isPriceNumber(_keyword){
    if(_keyword == "0" || _keyword == "0." || _keyword == "0.0" || _keyword == "0.00"){
      _keyword = "0"; return true;
    }else {
      var index = _keyword.indexOf("0");
      var length = _keyword.length;
      if (index == 0 && length > 1) {/*0开头的数字串*/
        var reg = /^[0]{1}[.]{1}[0-9]{1,2}$/;
        if (!reg.test(_keyword)) {
          return false;
        } else {
          return true;
        }
      } else {/*非0开头的数字*/
        var reg = /^[1-9]{1}[0-9]{0,10}[.]{0,1}[0-9]{0,2}$/;
        if (!reg.test(_keyword)) {
          return false;
        } else {
          return true;
        }
      }
      return false;
    }  
    }
})