// pages/handOutBonus/handOutBonus.js
const getReq = require('../../config.js').getReq;
const app = getApp();
import recordSource from '../../utils/recordSource';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bonusType: 'pin',
    canShare: false,
    btnFlag: false,
    datas: {},
    total: 0,
    count: 0,
    money: 0,
    tempCount: '',
    tempMoney: '',
    sendFlag: false,
    calcFlag: true,
    path: '',
    img_url: '',    
    title: '',
    tempBless: '',
    bless: '',
    userInfo: app.globalData.userInfo
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.userInfo) {
      this.getDatas()      
      this.setData({
        userInfo: app.globalData.userInfo
      })
    }
  },
  getAuth(e) {
    let { userInfo } = e.detail
    if (userInfo) {
      this.getDatas()      
      this.setData({
        userInfo: app.globalData.userInfo
      })
    }
  },

  getDatas() {
    let params = {
      act: 'member_bonus',
      op: 'predepositex'
    }
    getReq(app,params, res => {
      if (res.code == 200) {
        let { datas } = res  
        let tempBless = datas['send_bless']
        this.setData({
          datas,
          tempBless
        })
      }
    })
  },
  bindKeyInput(e) {
    let { type } = e.currentTarget.dataset
    let value = e.detail.value
    this.checkBindInput(type, value)
  },
  checkBindInput(type, value) {
    let currentType = type
    let currentValue = this.trim(value)
    if (type == 'count') {
      if (currentValue[0] == 0) {
        this.setData({
          tempCount: ''
        })
      }

      if (currentValue > 500) {
        currentValue = value.substring(0, currentValue.length - 1) || 0
        wx.showToast({
          icon: 'none',
          title: '一次最多发送红包500个',
          duration: 2000
        })
      }
      this.checkBindTotal(parseFloat(currentValue) || 0, this.data.money)
      this.setData({
        tempCount: parseFloat(currentValue),
        count: parseFloat(currentValue) || 0
      })
    }
    else if (type == 'money') {
      let index = currentValue.indexOf('.')
      if (index != -1) {
        if (index == 0) {
          this.setData({
            tempMoney: ''
          })
          return
        }
        let secondIndex = currentValue.substring(index + 1).indexOf('.')
        if (secondIndex != -1) {
          currentValue = currentValue.substring(0, index + 1)
        }

        if (currentValue.length - index - 1 > 2) {
          currentValue = currentValue.substring(0, index + 1 + 2)
        }
      }
      if (currentValue[0] == 0 && currentValue[1] != '.') {
        currentValue = currentValue.substring(1) || 0
      }
      if (currentValue > this.data.datas['share_bonus_rate'][0]['total']) {
        currentValue = value.substring(0, value.length - 1)
        wx.showToast({
          icon: 'none',
          title: '不能大于可分享的最大金额!',
          duration: 2000
        })
      }
      this.checkBindTotal(this.data.count, parseFloat(currentValue) || 0)
      this.setData({
        tempMoney: currentValue,
        money: parseFloat(currentValue) || 0
      })
    }
  },
  checkBindTotal(count, money) {
    let flag = true
    if (this.data.bonusType == 'pin') {
      flag = this.checkTotal(money)
      if (!count) {
        this.setData({
          total: 0
        })
      }
      else {
        this.setData({
          total: money
        })
      }
      
    }
    else {
      let bonus = count * money
      let total = parseInt(bonus * 100 + 0.5) / 100
      flag = this.checkTotal(total)
      this.setData({
        total
      })
    }
    if (count && money && this.data.bonusType == 'pin') {
      let single = money / count
      if (single < 0.01) {
        wx.showToast({
          icon: 'none',
          title: '单个红包金额小于0.01!',
          duration: 2000
        })
        flag = false
      }
    }
   
    if (!count || !money) {
      flag = false
    }
    this.setData({
      btnFlag: flag
    })
    return flag
  },
  checkTotal(money) {
    let flag = money < this.data.datas['share_bonus_rate'][0]['total']
    if (!flag) {
      wx.showToast({
        icon: 'none',
        title: '不能大于可分享的最大金额!',
        duration: 2000
      })
    }
    return flag
  },
  secBonusType(e) {
    let { type } = e.currentTarget.dataset
    this.setData({
      bonusType: type,
    })
    this.checkBindTotal(this.data.count || 0, this.data.money || 0)
  },
  checkBonus() {
    let flag = this.checkBindTotal(this.data.count || 0, this.data.money || 0)
    if (!flag){
      return
    }
    this.shareBonus()
  },
  shareBonus() {
    let params = {
      act: 'member_bonus',
      op: 'make',
      bonus_rate: this.data.datas['share_bonus_rate'][0]['rate'],
      total_num: this.data.count,
      type_bless: this.data.bless || this.data.datas['send_bless']
    }

    if (this.data.bonusType == "pin") {
      let data = {
        total_amount: this.data.money,
        send_type: 1
      }
      params = Object.assign({}, params, data)
    }
    else {
      let data = {
        fixed_money: this.data.money,
        send_type: 2
      }
      params = Object.assign({}, params, data)
    }
    getReq(app,params, res => {
      if (res.code == 200) {
        let { title, path } = res.datas
        this.setData({
          canShare: true,
          title,
          path
        })
      }
      else {
        wx.showToast({
          icon: 'none',
          title: res.message,
          duration: 2000
        })
        return false
      }
    })
  },
  close() {
    this.setData({
      canShare: false
    })
  },
  blessFocus(e) {
    let { value } = e.detail
    if (value == this.data.datas['send_bless']) {
       this.setData({
        tempBless: ''
       })
    }
  },
  blessBlur(e) {
    let { value } = e.detail
    if(!this.trim(value)) {
      this.setData({
        tempBless: this.data.datas['send_bless']
       })
    }
  },                        
  blessInput(e) {
    let { value } = e.detail
    this.setData({
      bless: value
     })
  },
  trim(str) {
    str = str.replace(/\s+/g, "")
    return str
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    let self = this
    if (res.from == "button") {
      this.getDatas()
      this.setData({
        canShare: false
      })
      let imageUrl = '../../image/share_bonus.png'
      let { title, path } = this.data
      return {
        title,
        imageUrl: imageUrl,
        path: `/${path}`
      }
    }
  }
})