// pages/payee/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,
    form:{
      payee:'',
      payeeAccount:'',
      bankType:'',
      bankDeposit:'',
      accountType:'',
      payeeType:'1',
      payeeTypeText:'收款方',
      payeePaymentLines:'',
      status:'0',
      villageAccountType: "2",
    },
    showBankType:false,
    showAccountType:false,
    bankTypeOptions:[],
    bankTypeText:'',
    accountTypeOptions:[],
    payeeTypeOptions:[],
    accountTypeText:'',
  },

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

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    // 所属银行字典查询 
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'bank_type', {method:'GET'}, {
      success: (res) => {
        // let array = res.data.filter(function (e) { return e.dictValue == event.detail.value.bankType; });
        this.setData({
          bankTypeOptions:res.data
        });
      }
    })
    // 付款方式类型字典查询 
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'account_type_cashier', {method:'GET'}, {
      success: (res) => {
        that.setData({
          accountTypeOptions:res.data,
        })
      }
    })
    // 付款方式类型字典查询 
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'payee_type', {method:'GET'}, {
      success: (res) => {
        that.setData({
          payeeTypeOptions:res.data,
        })
      }
    })
    
  },
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  openBox(even){
    console.log(even);
    var that = this ;
    if(even.currentTarget.dataset.name == "showDeposit"){
      if(that.data.form.bankType == ''){
        UTIL.showToastNoneIcon('请先选择所属银行！');
        that.setData({
          [even.currentTarget.dataset.name]:false,
        })
        return;
      }
    }
    this.setData({
      [even.currentTarget.dataset.name]:true
    })
  },
  accountTypeChange(even){
    console.log(even);
    var that = this ;
    if (even.detail.value.dictValue == 102) {
      that.setData({bankNameState : false});
    } else {
      that.setData({bankNameState : true});
    }
    this.setData({
      [even.currentTarget.dataset.value]:even.detail.value.dictValue,
      [even.currentTarget.dataset.value+"Text"]:even.detail.value.dictLabel,
      [even.currentTarget.dataset.name]:false,
    })
  },
  //银行类型选择
  bankTypeChange(even) {
    var that = this ;
    if(even.detail.value.dictValue == '1' || even.detail.value.dictValue == '2'){
      that.setData({nongshanghangShow : true});
      that.villageAccountTypeChange(that.data.form.villageAccountType);
    }else{
      that.setData({
        nongshanghangShow : true,
        nongshanghangShow1 : true,
        nongshanghangaccountNoShow : true,
      });
    }
    if(even.detail.value.dictValue == '3'){
      that.setData({
        nonghangShow : true,
      });
    }else{
      that.setData({
        nonghangShow : false,
      });
    }
    if(even.detail.value.dictValue == '4'){
      that.setData({
        jianhangShow : true,
      });
    }else{
      that.setData({
        jianhangShow : false,
      });
    }
    that.setData({
      isManagementControlTypeVisible : even.detail.value.dictValue == '2' && that.data.form.villageAccountType == '3' ? 1 : 0,// 农商行(山东省) && 多级账簿
      [even.currentTarget.dataset.value]:even.detail.value.dictValue,
      [even.currentTarget.dataset.value+"Text"]:even.detail.value.dictLabel,
      [even.currentTarget.dataset.name]:false,
    });
  },
  //账户分类选择
  villageAccountTypeChange(val) {
    var that = this;
    this.data.form.accountNo = "";
    if(val == '1'){
      that.setData({
        nongshanghangaccountNoShow : false,
        nongshanghangShow1 : true,
      });
    }else if(val == '2') {
      that.setData({
        nongshanghangaccountNoShow : false,
        nongshanghangShow1 : false,
      });
    }else{
      that.setData({
        nongshanghangaccountNoShow : true,
        nongshanghangShow1 : true,
      });
    }
    that.setData({
      isManagementControlTypeVisible : that.data.form.bankType == '2' && val == '3' ? 1 : 0,
    });
  },
  onConfirm(even){
    console.log(even);
    var that = this ;
    
    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){
    console.log(even);
    this.setData({
      [even.currentTarget.dataset.value]:even.detail.value.id,
      [even.currentTarget.dataset.value+"Text"]:even.detail.value.bankDeposit,
      [even.currentTarget.dataset.name]:false,
      ["form.payeePaymentLines"]:even.detail.value.payeePaymentLines
    })
  },
  closeBox(even){
    console.log(even.currentTarget.dataset.name);
    this.setData({
      [even.currentTarget.dataset.name]:false
    })
  },
  goSubmit(){
    console.log(this.data.form);
    var that = this;

    if(that.data.form.bankType == ''){
      UTIL.showToastNoneIcon('请选择所属银行！');
      return;
    }
    if(that.data.form.payee == ''){
      UTIL.showToastNoneIcon('请填写户名！');
      return;
    }
    if(that.data.form.payeeAccount == ''){
      UTIL.showToastNoneIcon('请填写收款账号！');
      return;
    }
    if(that.data.form.payeeType == ''){
      UTIL.showToastNoneIcon('请选择收款人类型！');
      return;
    }
    if(that.data.form.accountType == ''){
      UTIL.showToastNoneIcon('请选择账户类型！');
      return;
    }
    if(that.data.form.bankDeposit == ''){
      UTIL.showToastNoneIcon('请选择开户行！');
      return;
    }
    if(that.data.form.payeePaymentLines == ''){
      UTIL.showToastNoneIcon('请输入联行号！');
      return;
    }


    that.setData({
      ['form.method']:'POST'
    })
    UTIL.httpRequest(API.URL_GET_PAYEEADD, that.data.form, {
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {

          let pages = getCurrentPages();
          let currentPage = null;   //当前页面
          let prevPage = null;  //上一个页面
          currentPage = pages[pages.length - 1]; //获取当前页面，将其赋值
          prevPage = pages[pages.length - 2]; //获取上一个页面，将其赋值
          prevPage.onLoad(prevPage.options)
          wx.navigateBack({
            delta:1
          })
        }
      }
    })
  },
  onChange(even){
    console.log(even);
    this.setData({
      [even.currentTarget.dataset.formname]:even.detail
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

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

  },

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

  },

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

  },

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

  }
})