// downline.js
const app = getApp(),
  { hostname } = app.globalData,
  userInfo = wx.getStorageSync('userInfo')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    firstActive: false,
    secondActive: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ userInfo })
    options.withdraw && this.setData({ withdrawFlag: true})
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: '加载中...',
    })
    this.getUserBanlance()
    this.getDownline()
  },

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

  },

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

  },

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

  },

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

  },
  // 分享
  shareApp() {
    wx.showShareMenu({
      withShareTicket: false,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {

    return {
      title: app.globalData.fenxiaoTitle,
      path: '/pages/index/index?share_id=' + wx.getStorageSync('openid'),
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: '分享成功',
        })
      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '分享失败',
          image: '../../../images/warning.png'
        })
      }
    }
  },

  getDownline() {
    let that = this
    wx.request({
      url: hostname + 'share_friend',
      data: {
        user_id: wx.getStorageSync('openid')
      },
      success: (res) => {
        let downline = res.data.data
        if (res.data.code == 200) {
          let downline = res.data.data
          that.setData({ downline })
        }
        wx.hideLoading()
      }
    })
  },

  getUserBanlance() {
    let warnTips = setTimeout(() => {
      wx.showToast({
        title: '请求超时',
        image: '../../../images/warning.png'
      })
    }, 10000),
      that = this
    wx.request({
      url: hostname + 'userinfo',
      data: {
        user_id: wx.getStorageSync('openid')
      },
      success: function (res) {
        if (res.data.code == 200) {
          that.setData({ money: res.data.data.share_money })
        }
      },
      fail: function (res) { },
      complete: function (res) {
        clearTimeout(warnTips)
        wx.hideLoading()
      },
    })
    //获取用户余额
  },
  // 显示一级下线 
  showFirstDownline() {
    let firstActive = this.data.firstActive
    this.setData({ firstActive: !firstActive })
  },
  // 显示二级下线
  showSecondDownline() {
    let secondActive = this.data.secondActive
    this.setData({ secondActive: !secondActive })
  },
  // 显示提现窗口
  showWithdraw(e) {
    let { attr } = e.target.dataset
    if (attr) {
      if (this.data.money > 0) {
        this.setData({ withdrawFlag: true })
      }else{
        wx.showModal({
          title: '提示',
          content: '可用佣金不足',
          showCancel: false,
        })
      }
    } else {
      this.setData({ withdrawFlag: false })
    }
  },
  // 提现
  getWithdraw() {
    let { withdrawMoney, withdrawName, withdrawCard, withdrawBank } = this.data,
      that = this
    if (!withdrawMoney) {
      wx.showModal({
        title: '提示',
        content: '请输入要提现的金额',
        showCancel: false
      })
    } else {
      wx.showLoading({
        title: '请稍后..',
      })
      wx.request({
        url: hostname + 'shop/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)
        },
      })
    }
  },
  // 同步输入
  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;
    }
  }
})