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

  /**
   * 页面的初始数据
   */
  data: {
    isIPX: app.globalData.isIPX,
    show:false,
    accountList:[],
    approvalItemsOptions:[],
    bookName:''
  },

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

  },
   //跳转支出申请
swichPaymentApply:function(e){
  console.log(e.currentTarget.dataset.current);
  // let cur = e.currentTarget.dataset.current;
  // if (this.data.currentTaB == cur) {
  // return false;
  // }else{
  //   wx.navigateTo({
  //     url: '../inCome/index?id=' + id,
  //     })
  // }
  wx.navigateTo({
    url: '/pages/apply/paymentTemplate/add/add',
    })
  },
  openBox:function(){
    var that = this ;
    that.setData({
      show: true,
    })
  },
  closeBox:function(){
    var that = this ;
    that.setData({
      show: false,
    })
  },
  goTemplate:function(e){
    wx.navigateTo({
      url: 'paymentTemplate/add/add?id='+e.currentTarget.dataset.id+'&type=template&option='+e.currentTarget.dataset.options
    })
  },

  updateMoney:function(e){
    UTIL.showLoadingHaveMask('正在查询');
    UTIL.httpRequest(API.URL_GET_GETBALANCEENQUIRY+e.currentTarget.dataset.id, {method:'GET'}, {
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          this.setData({
            ["accountList["+e.currentTarget.dataset.index+"].balance"]:res.msg
          })
          UTIL.hideLoadingHaveMask();
          wx.showToast({
            title: '查询成功',
            icon: 'success',
            duration: 2000,
          })
        }else{
          UTIL.hideLoadingHaveMask();
          wx.showToast({
            title: '查询失败',
            icon: 'error',
            duration: 2000,
          })
        }
      }
    })
  },

  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.showLoading({
      title: '正在加载账户',
      mask:true
    })
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'bank_type', {method:'GET'}, {
      success: (res) => {
        this.setData({
          bankTypeOptions:res.data
        });
      }
    })
    var sendData = {
      pageNum:'1',
      pageSize:'100',
      accountName:'',
      bankAccountNumber:'',
      status:'N',
      method:'GET',
      accountType:102
    }
    var sendData2 = {
      pageNum:'1',
      pageSize:'100',
      accountName:'',
      bankAccountNumber:'',
      status:'N',
      method:'GET',
      accountType:101
    }
    var sendData3 = {
      pageNum:'1',
      pageSize:'100',
      accountName:'',
      bankAccountNumber:'',
      status:'N',
      method:'GET',
      accountType:100
    }
    UTIL.httpRequest(API.URL_GET_GETINFO, {method:'GET'}, {
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          this.setData({bookName:res.user.bookName})
        }
      }
    })
    UTIL.httpRequest(API.URL_GET_GETTEMPLATELIST, {method:'GET'}, {
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          that.setData({
            approvalItemsOptions : res.rows
          })
        }
      }
    })
    setTimeout(function(){
      UTIL.httpRequest(API.URL_GET_ACCOUNTLIST, sendData,{
        success: (res) => {
          for (let i = 0; i < res.rows.length; i++) {
            if(res.rows[i].bankAccountNumber==null){continue;}
            res.rows[i].bankAccountNumber = res.rows[i].bankAccountNumber.replace(/(\d{4})(?=\d)/g, "$1 ");
            res.rows[i].balance = Number(res.rows[i].balance).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, ($0, $1) => {
              return $1 + ","; }).replace(/\.$/, "");
            res.rows[i].bankTypeText = UTIL.getTransform(res.rows[i].bankType,that.data.bankTypeOptions);
          }
          console.log(res.rows)
          that.setData({
            accountList:res.rows
          })
          wx.hideLoading();
        }
      })
    },1000)
    UTIL.httpRequest(API.URL_GET_ACCOUNTLIST, sendData2,{
      success: (res) => {
        for (let i = 0; i < res.rows.length; i++) {
          // if(res.rows[i].bankAccountNumber==null){continue;}
          // res.rows[i].bankAccountNumber = res.rows[i].bankAccountNumber.replace(/(\d{4})(?=\d)/g, "$1 ");
          res.rows[i].balance = Number(res.rows[i].balance).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, ($0, $1) => {
            return $1 + ","; }).replace(/\.$/, "");;
          console.log(res.rows[i].balance)
        }
        that.setData({
          accountListXJ:res.rows
        })
      }
    })
    UTIL.httpRequest(API.URL_GET_ACCOUNTLIST, sendData3,{
      success: (res) => {
        for (let i = 0; i < res.rows.length; i++) {
          // if(res.rows[i].bankAccountNumber==null){continue;}
          // res.rows[i].bankAccountNumber = res.rows[i].bankAccountNumber.replace(/(\d{4})(?=\d)/g, "$1 ");
          res.rows[i].balance = parseFloat(res.rows[i].balance).toFixed(2);
          console.log(res.rows[i].balance)
        }
        that.setData({
          accountListGWK:res.rows
        })
      }
    })
  },
  delete(e){
    console.log(e);
    var that = this;
    UTIL.httpRequest(API.URL_GET_GETTEMPLATEREMOVE + e.currentTarget.dataset.id, {method:'GET'},{
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          that.data.approvalItemsOptions.splice(e.currentTarget.dataset.index, 1); 
          wx.showToast({
            title: '删除成功！',
            icon: 'success',
            duration: 2000
          })
          that.setData({
            approvalItemsOptions : that.data.approvalItemsOptions
          })
        }
      }
    })
  },
  getMr(e){
    var that = this;
    UTIL.httpRequest(API.URL_GET_GETTEMPLATERESETDEFAULT + e.currentTarget.dataset.id, {method:'GET'},{
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          UTIL.httpRequest(API.URL_GET_GETTEMPLATELIST, {method:'GET'}, {
            success: (res) => {
              if (res.code == API.SUCCESS_CODE) {
                wx.showToast({
                  title: '设置成功！',
                  icon: 'success',
                  duration: 2000
                })
                that.setData({
                  approvalItemsOptions : res.rows
                })
              }
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

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

  },

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

  },

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

  }
})