// pages/more/downLine/distribution/distribution.js
const app = getApp(),
  { hostname } = app.globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let { user_id } = options
    // 获取佣金数据
    wx.request({
      url: hostname + 'share_money',
      data: {
        user_id
      },
      success: res => {
        console.log(res)
        this.setData({
          userData: res.data.data,
          user_id
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },
  // 显示提现窗口
  showWithdraw(e) {
    let { attr } = e.target.dataset
    if (attr) {
      // if (this.data.money > 1) {
      if (true) {
        this.setData({ withdrawFlag: true })
      }
    } else {
      this.setData({ withdrawFlag: false })
    }
  },
  // 提现
  getWithdraw() {
    let { withdrawMoney, withdrawName, withdrawCard, withdrawBank } = this.data,
      that = this
    if (!withdrawMoney) {
      wx.showModal({
        title: '提示',
        content: '请输入要提现的金额',
        showCancel: false
      })
    } else {
      if (Number(withdrawMoney) <= Number(this.data.userData.share_finish) && Number(withdrawMoney) != 0) {
        wx.showLoading({
          title: '请稍后..',
        })
        wx.request({
          url: hostname + 'cash_apply',
          data: {
            user_id: wx.getStorageSync('openid'),
            money: withdrawMoney,
            card_number: withdrawCard,
            name: withdrawName,
            bank: withdrawBank
          },
          success: function (res) {
            if (res.data == 10000) {
              wx.showToast({
                title: '提现成功',
                duration: 2500,
                success: () => {
                  that.setData({
                    withdrawName: '',
                    withdrawBank: '',
                    withdrawCard: '',
                    withdrawMoney: '',
                    withdrawFlag: false
                  })
                }
              })
            }
          },
          fail: function (res) { },
          complete: function (res) {
            setTimeout(() => {
              wx.hideLoading()
            }, 1500)
          },
        })
      } else {
        wx.showToast({
          title: '佣金不足',
          image: '/images/warning.png'
        })
      }
    }
  },
  // 同步输入
  syncInfo(e) {
    let value = e.detail.value,
      { info } = e.currentTarget.dataset
    switch (info) {
      case 'name':
        this.setData({ withdrawName: value })
        break;
      case 'card_num':
        this.setData({ withdrawCard: value })
        break;
      case 'bank':
        this.setData({ withdrawBank: value })
        break;
      case 'money':
        this.setData({ withdrawMoney: value })
        break;
    }
  },
  // 佣金明细
  showDetail(){
    wx.navigateTo({
      url: '../detail/detail'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '邀请好友成为您的伙伴',
      path: '/pages/more/downLine/downLine?share_id=' + wx.getStorageSync('openid'),
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: '分享成功',
        })
      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '分享失败',
          image: '/images/warning.png'
        })
      }
    }
  }
})