// packageF/debt/vipAddindex/vipAddindex.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId: "",
    total: '',
    coupon: false,
    vid: '',
    usetInfo: null,
    cvid: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    if (options.vid) {
      this.setData({
        userId: options.vid
      })
      this.getVipUser()
    }
    if (options.cvid) {
      this.setData({
        cvid: options.cvid
      })
    }
    if (options.coupon) {
      this.setData({
        coupon: options.coupon
      })
      wx.setNavigationBarTitle({
        title: '提货券联名',
      })
    } else {
      wx.setNavigationBarTitle({
        title: '添加会员',
      })
    }
    if (options.debtId) {
      this.setData({
        debtId: options.debtId
      })
    }
    if (options.credit) {
      this.setData({
        credit: options.credit
      })
    }

  },
  tapId() {
    this.getVipUser();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  submitTap() {
    if (this.data.coupon) {
      // 提货券联名：：：：转让
      this.makeOver();
    } else {
      // 申请vip
      this.vipApply();
    }
  },
  vipApply() {
    let urlStr = app.getNetAddresss('plugin.debt-shop.frontend.debt-member.vipBind');
    app._getNetWork({
      url: urlStr,
      data: {
        vip_uid: this.data.userId,
        debtor_id: this.data.debtId,
        amount: this.data.total
      },
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1000,
            success: function (res) {
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1,
                })
              }, 1000);
            }
          })

        }else{
          wx.showToast({
            title: res.msg,
            icon:'none',
            duration:1000
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  makeOver() {
    let urlStr = app.getNetAddresss('plugin.debt-shop.frontend.debt-member.exchangeCommonCredit');
    app._getNetWork({
      url: urlStr,
      data: {
        connection_id: this.data.cvid,
        amount: this.data.total
      },
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1000,
            success: function (res) {
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1,
                })
              }, 1000);
            }
          })

        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getVipUser() {
    let urlStr = app.getNetAddresss('member.member.memberInfo');
    app._getNetWork({
      url: urlStr,
      data: {
        uid: this.data.userId,
      },
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            usetInfo: res.data
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  }
})