// 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:'2',
      payeeType:'1',
      payeeTypeText:'收款方',
      payeePaymentLines:'',
      status:'0',
    },
    showBankType:false,
    showAccountType:false,
    bankTypeOptions:[],
    bankTypeText:'',
    accountTypeOptions:[],
    payeeTypeOptions:[],
    accountTypeText:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this ;
    that.setData({
      projectId:options.id
    })
    var that = this;
    // 所属银行字典查询 
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'bank_type_all', {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', {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,
        })
      }
    })
    if(options.id){
      UTIL.httpRequest(API.URL_GET_PAYEEGET + options.id, {method:'GET'}, {
        success: (res) => {
          UTIL.httpRequest(API.URL_GET_DEPOSITLIST, {method:'GET',bankType:res.data.bankType,status:'0'}, {
            success: (res2) => {
              that.setData({
                depositOptions:res2.rows,
              })
              console.log(res.data.bankType)
              console.log(res2.rows)
              res.data.payeeTypeText = UTIL.getTransform(res.data.payeeType,that.data.payeeTypeOptions);
              res.data.bankTypeText = UTIL.getTransform(res.data.bankType,that.data.bankTypeOptions);
              that.setData({
                form:res.data,
              })
            }
          })
          
        }
      })
    }
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
    
  },
  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
    })
  },
  onConfirm(even){
    console.log(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){
    console.log(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.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;
    wx.showLoading({
      title: '正在保存',
      mask:true
    })
    if(that.data.form.payee == ''){
      wx.hideLoading();
      UTIL.showToastNoneIcon('请填写收款方！');
      return;
    }
    if(that.data.form.payeeAccount == ''){
      wx.hideLoading();
      UTIL.showToastNoneIcon('请填写收款账号！');
      return;
    }
    if(that.data.form.payeeType == ''){
      wx.hideLoading();
      UTIL.showToastNoneIcon('请选择收款人类型！');
      return;
    }
    if(that.data.form.accountType == ''){
      wx.hideLoading();
      UTIL.showToastNoneIcon('请选择账户类型！');
      return;
    }
    if(that.data.form.bankType == ''){
      wx.hideLoading();
      UTIL.showToastNoneIcon('请选择所属银行！');
      return;
    }
    if(that.data.form.bankDeposit == ''){
      wx.hideLoading();
      UTIL.showToastNoneIcon('请选择开户行！');
      return;
    }
    if(that.data.form.payeePaymentLines == ''){
      wx.hideLoading();
      UTIL.showToastNoneIcon('请输入联行号！');
      return;
    }

    that.setData({
      ['form.method']:'POST'
    })

    let url = '';

    if(this.data.projectId){
      url = API.URL_GET_PAYEEEDIT;
      console.log("修改");
    }else{
      url = API.URL_GET_PAYEEADD;
      console.log("新增");
    }
    UTIL.httpRequest(url, that.data.form, {
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          wx.showToast({
            title: "提交成功！",
            duration: 2000,
            icon:"success",
            mask:true
          })
          let pages = getCurrentPages();
          let currentPage = null;   //当前页面
          let prevPage = null;  //上一个页面
          currentPage = pages[pages.length - 1]; //获取当前页面，将其赋值
          prevPage = pages[pages.length - 2]; //获取上一个页面，将其赋值
          prevPage.onLoad(prevPage.options)
          setTimeout(function(){
            wx.navigateBack({
              delta:1
            })
            wx.hideLoading();
          },2000)
        }else{
          wx.hideLoading();
          wx.showToast({
            title: "提交失败！",
            duration: 2000,
            icon:"error"
          })
        }
      }
    })
  },
  onChange(even){
    console.log(even);
    this.setData({
      [even.currentTarget.dataset.formname]:even.detail
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

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

  },

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

  },

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

  },

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

  }
})