// pages/transaction/transactionDetail/transactionDetail.js
import * as UTIL from '../../../utils/util.js';
import * as API from '../../../utils/API.js';
let EVN_CONFIG = require('../../../env/env');

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIPX: app.globalData.isIPX,
    bankTypeOptions:[],
    form:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    var array = JSON.parse(options.options);
    console.log(array);
    UTIL.showLoadingHaveMask();
    // 所属银行 字典查询
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'bank_type', {method:'GET'}, {
      success: (res) => {
        that.setData({
          bankTypeOptions:res.data,
        })
      }
    })
    // 账户分类 字典查询
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'town_account_type', {method:'GET'}, {
      success: (res) => {
        that.setData({
          townAccountTypeOptions:res.data,
        })
      }
    })
    // 管控类型 字典查询
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'cashier_account_management_control_type', {method:'GET'}, {
      success: (res) => {
        that.setData({
          managementControlTypeOptions:res.data,
        })
        array.managementControlTypeText = UTIL.getTransform(array.managementControlType,res.data);
      }
    })
    //开户行查询
    UTIL.httpRequest(API.URL_GET_DEPOSITLIST, {method:'GET',bankType:array.bankType,status:array.status}, {
      success: (res2) => {
        that.setData({
          depositOptions:res2.rows,
        })
      }
    })
    if (array.townAccountType == 1 && array.managementControlType == 2) {
      //中心账套查询
      UTIL.httpRequest(API.URL_GET_GETTACCOUNTCENTRAL, {method:'GET'}, {
        success: (res2) => {
          array.centralBookIdText = res2.data.filter(function (e) { return e.id == array.centralBookId; })[0].bookName;
          that.setData({
            centralBookIdOptions:res2.data,
          })
        }
      })
    }
    if (array.managementControlType == 2) {
      //会计科目查询
      UTIL.httpRequest(API.URL_GET_GETACCOUNTCENTRAL, {method:'GET',bookId:array.centralBookId}, {
        success: (res2) => {
          array.centralSubjectIdText = res2.data.filter(function (e) { return e.subjectId == array.centralSubjectId; })[0].subjectName;
          that.setData({
            centralSubjectIdOptions:res2.data,
          })
        }
      })
    }

    setTimeout(function(){
      that.setData({
        form:array
      })
      wx.hideLoading();
    },3000)
    
    
  },
  /**
   * 显示弹窗
   */
  showPopup(e){
    var that = this ;
    that.setData({
      [e.currentTarget.dataset.name]:true
    })
  },
  /**
   * 隐藏弹窗
   */
  closePopup(e){
    var that = this ;
    that.setData({
      [e.currentTarget.dataset.name]:false
    })
  },
  /**
   * 弹窗选择
   */
  onConfirm(e){
    console.log(e);
    var that = this ;
    that.setData({
      [e.currentTarget.dataset.formname]:e.detail.value.dictValue,
      [e.currentTarget.dataset.formname+'Text']:e.detail.value.dictLabel,
      [e.currentTarget.dataset.name]:false
    })
  },
  onConfirmBank(e){
    console.log(e);
    var that = this ;
    that.setData({
      [e.currentTarget.dataset.formname]:e.detail.value.dictValue,
      [e.currentTarget.dataset.formname+'Text']:e.detail.value.dictLabel,
      [e.currentTarget.dataset.name]:false,
      ["form.bankDeposit"]:'',
      ["form.payeePaymentLines"]:'',
    })
    //开户行查询
    UTIL.httpRequest(API.URL_GET_DEPOSITLIST, {method:'GET',bankType:e.detail.value.dictValue,status:'0'}, {
      success: (res2) => {
        that.setData({
          depositOptions:res2.rows,
        })
      }
    })
  },
  onConfirmaDeposit(e){
    console.log(e);
    var that = this ;
    that.setData({
      [e.currentTarget.dataset.formname]:e.detail.value.bankDeposit,
      [e.currentTarget.dataset.name]:false,
      ["form.payeePaymentLines"]:e.detail.value.payeePaymentLines
    })
  },
  /**
   * 中心账套弹窗选择
   */
  onConfirmBook(e){
    console.log(e);
    var that = this ;
    that.setData({
      [e.currentTarget.dataset.formname]:e.detail.value.id,
      [e.currentTarget.dataset.formname+'Text']:e.detail.value.bookName,
      [e.currentTarget.dataset.name]:false
    })
    //会计科目查询
    UTIL.httpRequest(API.URL_GET_GETACCOUNTCENTRAL, {method:'GET',bookId:e.detail.value.id}, {
      success: (res2) => {
        that.setData({
          centralSubjectIdOptions:res2.data,
        })
      }
    })
  },
  /**
   * 会计科目弹窗选择
   */
  onConfirmSubjectId(e){
    console.log(e);
    var that = this ;
    that.setData({
      [e.currentTarget.dataset.formname]:e.detail.value.subjectId,
      [e.currentTarget.dataset.formname+'Text']:e.detail.value.subjectName,
      [e.currentTarget.dataset.name]:false
    })
  },
  /**
   * 输入框change
   */
  inputChange(e){
    var that = this ;
    that.setData({
      [e.currentTarget.dataset.formname]:e.detail
    })
  },
  /**
   * 提交修改
   */
  goSubmit(e){
    var that = this ;
    console.log(that.data.form);
    that.data.form.method = 'POST';
    //中心账套查询
    UTIL.httpRequest(API.URL_GET_GETTACCOUNTEDIT, that.data.form, {
      success: (res) => {
        console.log(res);
        if (res.code == API.SUCCESS_CODE) {
          wx.showToast({
            title: "修改成功！",
            duration: 2000,
            icon:"success",
            mask:true
          })
          setTimeout(function(){
            that.back()
          },2000)
        }
      }
    })
  },
  onChange(even){
    console.log(even);
    this.setData({
      [even.currentTarget.dataset.formname]:even.detail
    })
  },
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})