// pages/regular/index.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,
    option1: [],
    option2: [],
    option3: [],
    value1: '',
    value2: '',
    value3: '',
    showPopup:false,
    result1:'',
    result2:'',
    result3:'',
    beginApplyDate:'' , //申请开始时间
    endApplyDate: '' ,  //申请结束时间
    txnamtMin: '' , //金额范围 最小
    xnamtMax: '' , //金额范围最大
    showDialog:false,
    beforeClose(action) {
      return new Promise((resolve) => {
        setTimeout(() => {
          if (action === 'confirm') {
            // 拦截确认操作
             resolve(false);
          } else {
            resolve(true);
          }
        }, 0);
      });
    },
    projectId:'',
    projectValue:'',
    showTransactionDialog:'',
    form:{
      datescopeFrom:'',
      datescopeTo:'',
      amountscopeFrom:'',
      amountscopeTo:'',
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    // 所属银行 字典查询
    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 + 'sys_normal_disable', {method:'GET'}, {
      success: (res) => {
        that.setData({
          statusOptions:res.data,
        })
      }
    })
  },
  goSearch(e){
    var that = this ;
    var sendData = {
      pageNum:'1',
      pageSize:'100',
      bankType:'',
      townAccountType:'',
      payee:e.detail,
      method:'GET',
      accountType:''
    }
    //转账列表    
    UTIL.httpRequest(API.URL_GET_GETTACCOUNTLIST , sendData, {
      success: (res) => {
        res.rows.map(rrr=>{
          rrr.bankTypeText = UTIL.getTransform(rrr.bankType,that.data.bankTypeOptions);
          rrr.townAccountTypeText = UTIL.getTransform(rrr.townAccountType,that.data.townAccountTypeOptions);
          rrr.statusText = UTIL.getTransform(rrr.status,that.data.statusOptions);
          rrr.balance = rrr.balance == null ? '0.00' : parseFloat(rrr.balance).toFixed(2);
        })
        that.setData({
          list:res.rows
        })
      }
    })
  },
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  onClose() {
    this.setData({ showPopup: false });
  },
  minInput(e){
    var that = this ;
    that.setData({
      ["form.amountscopeFrom"]: e.detail, //金额范围 最小
    })
  },
  maxInput(e){
    var that = this ;
    that.setData({
      ["form.amountscopeTo"]: e.detail, //金额范围最大
    })
  },
  toPayDetail(e){
    let options = JSON.stringify(e.currentTarget.dataset.options);
    wx.navigateTo({
      url: 'detail/detail?options='+options,
    })
  },
  goEdit(e){
    let options = JSON.stringify(e.currentTarget.dataset.options);
    wx.navigateTo({
      url: 'edit/edit?options='+options,
    })
  },
  goAdd(e){
    wx.navigateTo({
      url: 'add/add',
    })
  },
  goRemove(e){
    let id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '是否删除？',
      success (res) {
        if (res.confirm) {
          //转账列表
          UTIL.httpRequest(API.URL_GET_GETTACCOUNTREMOVE + id , {} , {
            success: (res) => {
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 2000
              })
              this.onLoad();
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
     
  },
  showTransactionDialog(e){
    var that = this ;
    console.log(e);
    that.setData({
      form:{
        datescopeFrom:'',
        datescopeTo:'',
        amountscopeFrom:'',
        amountscopeTo:'',
        accountId:e.currentTarget.dataset.id
      },
    })
    that.setData({
      showTransactionDialog:true,
    })
  },
  selectAccount(){
    var that = this ;
    console.log(that.data.form);
    if (that.data.form.datescopeFrom == '') {
      UTIL.showToastNoneIcon('请选择开始日期！');
      return;
    }
    if (that.data.form.datescopeTo == '') {
      UTIL.showToastNoneIcon('请选择截止日期！');
      return;
    }
    that.data.form.method = 'POST';
    //转账列表
    UTIL.httpRequest(API.URL_GET_GETTACCOUNTADDRECORD , that.data.form, {
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          wx.showToast({
            title: '查询成功！',
            icon: 'success',
            duration: 2000
          })
          that.setData({
            showTransactionDialog : false
          })
        }else{
          UTIL.showToastNoneIcon(res.msg);
          that.setData({
            showTransactionDialog : false
          })
        }
      }
    })
  },
  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,
    })
  },
  goBalance(e){
    let id = e.currentTarget.dataset.id;
    UTIL.httpRequest(API.URL_GET_GETTACCOUNTENQUIRY+id, {method:'GET'}, {
      success: (res2) => {
        if (res2.code == 200) {
          wx.navigateTo({
            url: 'balance/balance?id='+e.currentTarget.dataset.id,
          })
        }else{
          UTIL.showToastNoneIcon(res2.msg);
        }
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    var sendData = {
      pageNum:'1',
      pageSize:'100',
      bankType:'',
      townAccountType:'',
      payee:'',
      method:'GET',
      accountType:''
    }
    setTimeout(function(){
      //转账列表
      UTIL.httpRequest(API.URL_GET_GETTACCOUNTLIST , sendData, {
        success: (res) => {
          res.rows.map(rrr=>{
            rrr.bankTypeText = UTIL.getTransform(rrr.bankType,that.data.bankTypeOptions);
            rrr.townAccountTypeText = UTIL.getTransform(rrr.townAccountType,that.data.townAccountTypeOptions);
            rrr.statusText = UTIL.getTransform(rrr.status,that.data.statusOptions);
            rrr.balance = rrr.balance == null ? '0.00' : parseFloat(rrr.balance).toFixed(2);
          })
          that.setData({
            list:res.rows
          })
        }
      })
    },1000)
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})