// pages/bank/add/add.js
import * as UTIL from '../../../utils/util.js';
import * as API from '../../../utils/API.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIPX: app.globalData.isIPX,
    showBankType:false,
    form:{
      sheng: "", //省 必填
      shi: "",  //市  必填
      bankType: "", //所属银行 必填
      bankDeposit: "", //开户行 必填
      payeePaymentLines: "", //联行号 //必填
      
    },
    status:0,
    id:"",
    bankTypeOptions:[],
    bankTypeindex:0
  },
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  onConfirmbankType (e) {
    let obj =  e.detail.value;
    this.setData({
      'form.bankType':this.data.bankTypeOptions[obj].dictValue,
      'bankTypeindex':obj
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id!=null&&options.id!=""){
      let that = this
      this.setData({id:options.id})
      UTIL.httpRequest(API.URL_GET_DEPOSITDETAIL + this.data.id, {method:'GET'}, {
        success: (res) => {
          this.setData({'form':res.data})
          UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'bank_type', {method:'GET'}, {
            success: (r) => {
              if(r.data.length>0){
                that.setData({
                  bankTypeOptions:r.data,
                })
                r.data.map((rr,ind) => {
                     if(rr.dictValue == res.data.bankType){
                       that.setData({'bankTypeindex':ind})
                     }
                 })
              }
            }
          })
        }
      })
    }else{
      let that = this
      UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'bank_type', {method:'GET'}, {
        success: (r) => {
          if(r.data.length>0){
            that.setData({
              bankTypeOptions:r.data,
              'form.bankType':r.data[0].dictValue
            })
          }
        }
      })
    }

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    //所属银行
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'bank_type', {method:'GET'}, {
      success: (res) => {
        this.setData({
          bankTypeOptions:res.data
        });
      }
    })
  },
  openBox(even){
    this.setData({
      [even.currentTarget.dataset.name]:true
    })
  },
  closeBox(even){
    this.setData({
      [even.currentTarget.dataset.name]:false
    })
  },
  onConfirm(even){
    var that = this ;
    if(even.currentTarget.dataset.name == "showBankType"){
      // 付款方式类型字典查询
      UTIL.httpRequest(API.URL_GET_DEPOSITLIST, {method:'GET',bankType:even.detail.value.dictValue,status:'0'}, {
        success: (res) => {
          that.setData({
            depositOptions:res.rows,
          })
        }
      })
    }
    this.setData({
      [even.currentTarget.dataset.value]:even.detail.value.dictValue,
      [even.currentTarget.dataset.value+"Text"]:even.detail.value.dictLabel,
      [even.currentTarget.dataset.name]:false,
    })
  },
  onConfirmDeposit(even){
    this.setData({
      [even.currentTarget.dataset.value]:even.detail.value.bankDeposit,
      [even.currentTarget.dataset.value+"Text"]:even.detail.value.bankDeposit,
      [even.currentTarget.dataset.name]:false,
      ["form.bankDeposit"]:even.detail.value.bankDeposit
    })
  },
  onChange(even){
    console.log(even);
    this.setData({
      [even.currentTarget.dataset.formname]:even.detail
    })
  },
  goSubmit(){

    if(this.data.form.sheng===''||this.data.form.sheng==null){ //省
      UTIL.showToastNoneIcon('请填写省！');
      return false;
    }else if(this.data.form.shi===''||this.data.form.shi==null){  //市
      UTIL.showToastNoneIcon('请输入市！');
      return false;
    }else if(this.data.form.bankType===''||this.data.form.bankType==null){  //所属银行
      UTIL.showToastNoneIcon('请输入所属银行！');
      return false;
    }else if(this.data.form.bankDeposit===''||this.data.form.bankDeposit==null){  //开户行
      UTIL.showToastNoneIcon('请输入开户行！');
      return false;
    }else if(this.data.form.payeePaymentLines === ''||this.data.form.payeePaymentLines==null){//联行号
      UTIL.showToastNoneIcon('请输入联行号！');
      return false;
    }else if(this.data.status=='0'){
      this.setData({'status':1})
      var that = this;
      that.data.form.method = 'POST';
      if(that.data.form.id==""||that.data.form.id==null){
        UTIL.httpRequest(API.URL_GET_DEPOSITADD, that.data.form, {
          success: (res) => {
            console.log(res);
            that.setData({'status':0})
            if(res.code == 200){
              UTIL.showToastNoneIcon('新增成功');
              setTimeout(function(){
                wx.navigateBack({
                  delta:1
                })
              },2000)
            }else{
              UTIL.showToastNoneIcon('新增失败：'+res.msg);
            }
          },
          fail: function (response) {
            if (typeof fail === FUNCTION_TEXT) {
              fail(handleFail(response));
            } else {
              showToastNoneIcon(API.MSG_FAIL_HTTP);
            }
            this.setData({'status':0})
          },
          complete: function (response) {
            wx.hideNavigationBarLoading();
          }
        })
      }else{
        UTIL.httpRequest(API.URL_POST_DEPOSITUPDATE, that.data.form, {
          success: (res) => {
            this.setData({'status':0})
            if(res.code == 200){
              UTIL.showToastNoneIcon('修改成功');
              setTimeout(function(){
                wx.navigateBack({
                  delta:1
                })
              },2000)
            }else{
              UTIL.showToastNoneIcon('修改失败：'+res.msg);
            }
          },
          fail: function (response) {
            if (typeof fail === FUNCTION_TEXT) {
              fail(handleFail(response));
            } else {
              showToastNoneIcon(API.MSG_FAIL_HTTP);
            }
            this.setData({'status':0})
          },
          complete: function (response) {
            wx.hideNavigationBarLoading();
          }
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

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

  },

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

  },

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

  },

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

  }
})