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:[],
    receiptOptions:[],
    bookName:'',
    accountListXJ:[],
    showDialog:false,
    projectId:'',
    bankName:'',
    beforeClose(action) {
      return new Promise((resolve) => {
        setTimeout(() => {
          if (action === 'confirm') {
            // 拦截确认操作
             resolve(false);
          } else {
            resolve(true);
          }
        }, 0);
      });
    },
    index: 0,
    showTransactionDialog:false,
    showReceiptDialog:false,
    showSelectType:false,
    form:{
      selectTypeText:'',
      selectType:'',
      directionText:'',
      direction:'',
      datescopeFrom:'',
      datescopeTo:'',
    }
  },

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

  },
  bindSelectTypePickerChange: function(e) {
    var that = this;
    var dictValue = that.data.selectTypeOptions[e.detail.value].dictValue;
    var text = UTIL.getTransform(dictValue,that.data.selectTypeOptions);
    this.setData({
      ["form.selectTypeText"]: text,
      ["form.selectType"]:dictValue
    })
  },
  bindDirectionPickerChange: function(e) {
    var that = this;
    var dictValue = that.data.directionOptions[e.detail.value].dictValue;
    var text = UTIL.getTransform(dictValue,that.data.directionOptions);
    this.setData({
      ["form.directionText"]: text,
      ["form.direction"]:dictValue
    })
  },
  bindReceiptPickerChange: function(e) {
    var that = this;
    var dictValue = that.data.receiptOptions[e.detail.value].dictValue;
    var text = UTIL.getTransform(dictValue,that.data.receiptOptions);
    this.setData({
      ["form.directionText"]: text,
      ["form.direction"]:dictValue
    })
  },
  bindDatescopeFromPickerChange: function(e) {
    console.log(e);
    var that = this;
    that.setData({
      ["form.datescopeFrom"]: e.detail.value,
    })
  },
  bindDatescopeToPickerChange: function(e) {
    console.log(e);
    var that = this;
    that.setData({
      ["form.datescopeTo"]: e.detail.value,
    })
  },
  onChange(e){
    var that = this;
    console.log(e);
    that.setData({
      [e.currentTarget.dataset.name]: e.detail,
    })
  },
  selectAccount(){
    console.log(this.data.form)
    var that = this ;
    const form = that.data.form;
    if(form.selectType == ''){
      UTIL.showToastNoneIcon('请选择查询类型！');
      return;
    }else if(form.direction == ''){
      UTIL.showToastNoneIcon('请选择来往账标识！');
      return;
    }else if(form.datescopeFrom == ''){
      UTIL.showToastNoneIcon('请选择开始日期！');
      return;
    }else if(form.datescopeTo == ''){
      UTIL.showToastNoneIcon('请选择截止日期！');
      return;
    }
    that.data.form.method = "POST"
    UTIL.showLoadingHaveMask('正在查询');
    UTIL.httpRequest(API.URL_GET_SELECTRECORD, that.data.form, {
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          UTIL.hideLoadingHaveMask();
          wx.showToast({
            title: '查询成功',
            icon: 'success',
            duration: 2000,
          })
        }else{
          UTIL.hideLoadingHaveMask();
          UTIL.showToastNoneIcon(res.msg)
        }
      }
    })

  },
  bankReceipt(){
    console.log(this.data.form)
    var that = this ;
    const form = that.data.form;
    if(form.selectType == ''){
      UTIL.showToastNoneIcon('请选择查询类型！');
      return;
    }else if(form.direction == ''){
      UTIL.showToastNoneIcon('请选择来往账标识！');
      return;
    }else if(form.datescopeFrom == ''){
      UTIL.showToastNoneIcon('请选择开始日期！');
      return;
    }else if(form.datescopeTo == ''){
      UTIL.showToastNoneIcon('请选择截止日期！');
      return;
    }

    that.data.form.method = "GET"
    UTIL.showLoadingHaveMask('正在查询');
    UTIL.httpRequest(API.URL_GET_SELECTBANKRECEIPT, that.data.form, {
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          UTIL.hideLoadingHaveMask();
          wx.showToast({
            title: '查询成功',
            icon: 'success',
            duration: 2000,
          })
        }else{
          UTIL.hideLoadingHaveMask();
          UTIL.showToastNoneIcon(res.msg)
        }
      }
    })

  },
  showTransactionDialog(e){
    var that = this;
    that.setData({
      form:{
        selectTypeText:'',
        selectType:'',
        directionText:'',
        direction:'',
        datescopeFrom:'',
        datescopeTo:'',
      }
    })
    that.setData({
      showTransactionDialog:true,
      ["form.accountId"]:e.currentTarget.dataset.id
    })
  },
  showReceiptDialog(e){
    var that = this;
    that.setData({
      form:{
        selectTypeText:'',
        selectType:'',
        directionText:'',
        direction:'',
        datescopeFrom:'',
        datescopeTo:'',
      }
    })
    that.setData({
      showReceiptDialog:true,
      ["form.accountId"]:e.currentTarget.dataset.id
    })
  },
  goRegular(e){
    wx.navigateTo({
      url: '/pages/regular/regular?id='+e.currentTarget.dataset.id,
    })
  },
   //跳转支出申请
  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(even){
    console.log(even.currentTarget.dataset.name);
    this.setData({
      [even.currentTarget.dataset.name]: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'
    })
  },

  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();
          UTIL.showToastNoneIcon(res.msg)
        }
      }
    })
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.showLoading({
      title: '正在加载账户',
      mask:true
    })
    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',
      payeeType:'4',
      status:'0',
      method:'GET',
    }
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'bank_type', {method:'GET'}, {
      success: (res) => {
        this.setData({
          bankTypeOptions:res.data
        });
      }
    })
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'select_type', {method:'GET'}, {
      success: (res) => {
        this.setData({
          selectTypeOptions:res.data
        });
      }
    })
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'direction', {method:'GET'}, {
      success: (res) => {
        this.setData({
          directionOptions:res.data
        });
      }
    })
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'receipt_trans_type', {method:'GET'}, {
      success: (res) => {
        this.setData({
          receiptOptions:res.data
        });
      }
    })
    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_APPROVALITEMSLIST, {method:'GET',dataType:'1'}, {
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          that.setData({
            approvalItemsOptions : res.rows
          })
        }
      }
    })
    setTimeout(function(){
      UTIL.httpRequest(API.URL_GET_ACCOUNTLIST, sendData,{
        success: (res) => {
          if(res.rows){
            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();
          }else{
            wx.hideLoading();
          }
        }
      })
  
      UTIL.httpRequest(API.URL_GET_ACCOUNTLIST, sendData2,{
        success: (res) => {

          if(res.rows){
            for (let i = 0; i < res.rows.length; i++) {
              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)
            that.setData({
              accountListXJ:res.rows
            })
          }
          

        }
      })

      UTIL.httpRequest(API.URL_GET_SELECTLIST, sendData3,{
        success: (res) => {
          for (let i = 0; i < res.rows.length; i++) {
            res.rows[i].payeeAccount = res.rows[i].payeeAccount.replace(/(\d{4})(?=\d)/g, "$1 ");
            res.rows[i].payeePaymentLines = Number(res.rows[i].payeePaymentLines).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, ($0, $1) => {
              return $1 + ","; }).replace(/\.$/, "");
          }
          that.setData({
            accountListGWK:res.rows
          })
        }
      })
      
    },1000)
    
  },
  delete(e){
    console.log(e);
    var that = this;
    UTIL.httpRequest(API.URL_GET_REMOVEAPPROVAL + 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_GETSETDEFAULTVALUES + e.currentTarget.dataset.id, {method:'GET'},{
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          UTIL.httpRequest(API.URL_GET_APPROVALITEMSLIST, {method:'GET',dataType:'1'}, {
            success: (res) => {
              if (res.code == API.SUCCESS_CODE) {
                wx.showToast({
                  title: '设置成功！',
                  icon: 'success',
                  duration: 2000
                })
                that.setData({
                  approvalItemsOptions : res.rows
                })
              }
            }
          })
        }
      }
    })
  },
  showDialog(e){
    var that = this ;
    that.setData({
      showDialog:true,
      projectId:e.currentTarget.dataset.id,
      bankName:e.currentTarget.dataset.name,
      id: '' ,
      password: '',})
  },
  //支付口令修改
  edit(e){ 
    var that = this ;
    var zz = /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[~!@#$%^&*])[\da-zA-Z~!@#$%^&*]{6,}$/;
    console.log(zz.test(that.data.password));
    if(!(zz.test(that.data.password))){
      wx.showToast({
        title: '请根据提示修改',
        icon: 'error',
        duration: 2000,
      })
      return;
    }
    let data = {
      id: that.data.projectId ,
      accountPassword: that.data.password,
      method:'POST'
    }
    UTIL.httpRequest(API.URL_GET_PASSWORDEDIT, data,{
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          wx.showToast({
            title: '设置成功！',
            icon: 'success',
            duration: 2000
          })
          that.setData({
            showDialog : false,
          })
        }else if(res.code == "403"){
          wx.showToast({
            title: "无修改权限",
            icon: 'error',
            duration: 2000
          })
          that.setData({
            showDialog : false,
          })
        }else{
          wx.showToast({
            title: "系统错误",
            icon: 'error',
            duration: 2000
          })
        }
      }
    })
  },
  bindNameInput(e){
    var that = this ;
    console.log(e);
    that.setData({password:e.detail.value});
  },
  onClose(e){
    this.setData({
      [e.currentTarget.dataset.name]:false
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

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

  },

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

  },
  navigate(even){
    wx.navigateTo({
      url: even.currentTarget.dataset.url,
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})