// pages/member/balance/supplier_withdrawals/supplier_withdrawals.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    language: '',
    supplier: '',
    member_id: '',
    balance: 0,
    service_type: 0,
    service_money: 0,
    withdrawType: {}, // 提现方法
    // 是否提现到支付宝
    withdrawToAlipay: 0,
    // 是否提现到微信
    withdrawToWechat: 0,
    withdrawToManual: 0,
    poundage: '',
    eupshow: false,
    hxShow: false,
    info_form: {
      balance_money: ''
    },
    action: '',
    msg: '',
    notice: '',
    show2: false, // 判断
    show3: false, // 银行卡列表
    withList: [], // 银行卡列表
    checkObj: {}, // 选中的银行卡
    actual_amount: '', // 实际到账金额
    poundageHj: '', // 手续费
    cost_money: '', // 提现金额
    poundage_name:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.supplier) {
      this.setData({
        supplier: options.supplier
      });
    }
    if (options.member_id) {
      this.setData({
        member_id: options.member_id
      });
    }
    // 获取数据
    if (this.data.supplier === 'supplier') {
      this._getData2();
    } else {
      this._getData();
    }
    this.customizeIncome();
  },
  //自定义提现收入语言
  customizeIncome() {
    try {
      var value = wx.getStorageSync('mailLanguage');
      if (value) {
        let mailLanguage = JSON.parse(value);
        // Do something with return value
        this.setData({
          poundage_name: mailLanguage.income.poundage_name
        });
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  _getData2() {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.supplier.frontend.withdraw.index');
    urlStr += '&member_id=' + this.data.member_id;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            balance: res.data.cost_money,
            service_money: res.data.service_money,
            service_type: res.data.service_type,
            withdrawType: res.data.type
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  _getData() {
    let that = this;
    let urlStr = app.getNetAddresss('finance.balance-withdraw.page');
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            balance: res.data.balance,
            withdrawToAlipay: res.data.alipay,
            withdrawToWechat: res.data.wechat,
            withdrawToManual: res.data.manual,
            poundage: res.data.poundage,
            eupshow: res.data.eup_pay,
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  balanceMoneyinp(e) {
    let val = e.detail.value;
    this.setData({
      'info_form.balance_money': val
    });
  },
  confirm(e) {
    let num = e.target.dataset.num;
    this.setData({
      pay_type: num
    });
    switch (this.data.pay_type) {
    case '1':
      this.confirmPost(this.data.pay_type);
      break;
    case '2':
      this.confirmPost(this.data.pay_type);
      break;
    case '3':
      this.manualVerify(this.data.pay_type);
      break;
    case '4':
      this.checkEup(this.data.pay_type);
      break;
    case '5':
      this.checkHx(this.data.pay_type);
      break;
    default:
      break;
    }
  },
  confirmPost(type) {
    let that = this;
    let urlStr = app.getNetAddresss('finance.balance-withdraw.withdraw');
    if (parseFloat(that.data.info_form.balance_money) > parseFloat(that.data.balance)) {
      wx.showToast({
        icon: 'none',
        title: '提现金额不可大于您的余额',
        duration: 1500
      });
      return;
    }
    if (that.data.info_form.balance_money == undefined || that.data.info_form.balance_money <= 0 || that.data.info_form
      .balance_money == 0) {
      wx.showToast({
        icon: 'none',
        title: '提现金额不可低于0',
        duration: 1500
      });
      return;
    }
    urlStr += '&withdraw_type=' + type;
    urlStr += '&withdraw_money=' + that.data.info_form.balance_money;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500,
            success: function () {
              setTimeout(function () {
                // 要延时执行的代码
                wx.navigateBack({
                  delta: 1
                });
              }, 1500); // 延迟时间
            }
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  // 手动提现验证
  manualVerify(type) {
    let that = this;
    let urlStr = app.getNetAddresss('finance.balance-withdraw.is-can-submit');
    if (parseFloat(this.data.info_form.balance_money) > parseFloat(this.data.balance)) {
      wx.showToast({
        icon: 'none',
        title: '提现金额不可大于您的余额',
        duration: 1500
      });
      return;
    }
    if (
      this.data.info_form.balance_money == undefined ||
      this.data.info_form.balance_money <= 0 ||
      this.data.info_form.balance_money == 0
    ) {
      wx.showToast({
        icon: 'none',
        title: '提现金额不可低于',
        duration: 1500
      });
      return;
    }
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {},
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (response.data.status) {
            that.confirmPost(type); // 提现
          } else {
            that.setManualInfo(type, res.data.manual_type);
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {}
    });
  },
  setManualInfo(type, manual_type) {
    let msg = '';
    let routerPath = '';
    switch (manual_type) {
    case 'bank':
      msg = '请先填写银行卡信息';
      routerPath = '/packageA/member/memberBank/memberBank';
      break;
    case 'wechat':
      msg = '请先填写微信信息';
      routerPath = '/packageA/member/info/info';
      break;
    case 'alipay':
      msg = '请先填写支付宝信息';
      routerPath = '/packageA/member/info/info';
      break;
    default:
      break;
    }
    // let that = this;
    wx.showModal({
      title: '提示',
      content: msg,
      success(res) {
        if (res.confirm) {
          wx.navigateTo({
            url: routerPath
          });
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    });
  },
  checkEup(type) {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.eup-pay.api.account.pi-account');
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {},
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (res.data.bool == 1) {
            that.confirmPost(type); // 提现
          }
        } else if (res.result == 0) {
          if (res.data.bool == 0) {
            let msg = res.msg;
            wx.showModal({
              title: '提示',
              content: msg,
              success(res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/packageA/member/info/info'
                  });
                } else if (res.cancel) {
                  console.log('用户点击取消');
                }
              }
            });
          }
        }
      },
      fail: function (res) {
        wx.showToast({
          icon: 'none',
          title: res,
          duration: 1500
        });
      }
    });
  },
  submitbrank() {
    app._postNetWork({
      url: this.data.action,
      showToastIn: false,
      data: {
        ipsRequest: this.data.msg
      },
      success: function (resdata) {
        wx.navigateBack({
          delta: 1
        });
      },
      fail: function (res) {}
    });
  },
  suChange(e) {
    console.log(e, '事件啊啊');
    this.setData({
      service_money: e.detail.value
    });
  },
  // 汇聚提现前面的
  towithdrawalHj(e) {
    let typeData = e.target.dataset.index;
    this.setData({
      pay_type: typeData
    });
    if (this.data.pay_type == 5) {
      let urlStr = app.getNetAddresss('plugin.converge_pay.frontend.controllers.converge-pay-bank-card.get-bank-card');
      app._getNetWork({
        url: urlStr,
        success: (resdata) => {
          let res = resdata.data;
          if (res.result == 1) {
            this.setData({
              checkObj: res.data
            });
            let name = res.data.bankName + '(' + res.data.receiverAccountNoEnc.substring(res.data.receiverAccountNoEnc.length - 4) + ')';
            this.setData({
              show2: true,
              'checkObj.bankName': name
            });

            this.getwithList();
            this.getCartWith();
            //
            return;
          } else {
            if (res.data.status == -1) {
              wx.navigateTo({
                url: '/packageA/others/HJinfo/bankCardInformation/bankCardInformation'
              });
            }
            wx.showToast({
              title: res.msg,
              icon: 'none'
            });
            return;
          }
        },
        fail: function (res) {
          console.log(res.msg);
        }
      });
      return;
    }
    this.towithdrawal(typeData);
  },
  // 获取银行卡列表
  getwithList() {
    let urlStr = app.getNetAddresss('plugin.converge_pay.frontend.controllers.converge-pay-bank-card.get-bank-card-list');
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          if (res.data.length == 0) {
            wx.showToast({
              title: '请先去添加银行卡',
              icon: 'none'
            });
            wx.navigateTo({
              url: '/packageA/others/HJinfo/bankCardInformation/bankCardInformation'
            });
          }
          res.data.map(item => {
            item.check = false;
            if (item.receiverAccountNoEnc != '' && item.receiverAccountNoEnc != null) {
              item.bankName += '(' + item.receiverAccountNoEnc.substring(item.receiverAccountNoEnc.length - 4) + ')';
            }
          });
          this.setData({
            withList: res.data
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
          setTimeout(() => {
          }, 2000);
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  // 获取手续费
  getCartWith() {
    let urlStr = app.getNetAddresss('plugin.supplier.frontend.withdraw.converge-withdraw');

    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            poundageHj: res.data.poundage,
            actual_amount: res.data.actual_amount,
            cost_money: res.data.cost_money
          });
        } else {
          wx.showModal({
            title: '提示',
            content: res.msg
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  getToTitle() {
    let urlStr = app.getNetAddresss('plugin.converge_pay.frontend.controllers.converge-pay-bank-card.set-bank-card');
    app._postNetWork({
      url: urlStr,
      data: { set: this.data.checkObj },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          // 有资格提现
          this.towithdrawal(this.data.pay_type);
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  suppierGotiem() {
    console.log(this.data.checkObj, '选中的项');
    this.getToTitle();
    return;
  },
  checkOut(e) {
    let item = e.target.dataset.item || e.currentTarget.dataset.item;
    item.check = !item.check;
    console.log(item, '当前对象');

    let data = this.data.withList;
    data.map(items => {
      if (items.id == item.id) {
        items.check = item.check;
      }else {
        items.check = false;
      }
    });
    console.log(data, '数组');

    if (item.check == true) {
      this.setData({
        show3: false,
        show2: true,
        checkObj: item,
        withList: data
      });
    }
  },
  show1CloseBtn() {
    this.setData({
      show2: false,
      show3: false
    });
  },
  backGo() {
    this.setData({
      show2: true,
      show3: false
    });
  },
  backTap() {
    this.setData({
      show2: false,
      show3: false
    });
  },
  gotoList() {
    this.setData({
      show2: false,
      show3: true
    });
  },
  towithdrawal(type) {
    let that = this;
    if (this.data.balance == undefined || this.data.balance <= 0 || this.data.balance == 0) {
      wx.showToast({
        icon: 'none',
        title: '提现金额不可低于0',
        duration: 1500
      });
      return;
    }
    let urlStr = app.getNetAddresss('plugin.supplier.frontend.withdraw.withdraw');
    urlStr += '&apply_type=' + this.data.pay_type;
    urlStr += '&member_id=' + this.data.member_id;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            show2: false
          });
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500,
            success: function () {
              setTimeout(function () {
                // 要延时执行的代码
                wx.navigateBack({
                  delta: 1
                });
              }, 1500); // 延迟时间
            }
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  }
});
