// 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:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    // 对账状态字典查询
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'check_status', {method:'GET'}, {
      success: (res) => {
        that.setData({
          checkStatusOptions:res.data,
        })
      }
    })
    // 业务类型字典查询
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'trans_type', {method:'GET'}, {
      success: (res) => {
        let option2 = [{
          text: '业务类型', value: ''
        }];
        res.data.map(rr=>{
          option2.push({
            text: rr.dictLabel, value: rr.dictValue
          })
        })
        that.setData({
          option2:option2,
          transTypeOptions:res.data,
        })
      }
    })
    // 来往账标识字典查询
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'direction', {method:'GET'}, {
      success: (res) => {
        let option3 = [];
        res.data.map(rr=>{
          option3.push({
            text: rr.dictLabel, value: rr.dictValue
          })
        })
        option3[0].text = '来往账标识';
        that.setData({
          option3:option3,
          value3:res.data[0].dictValue,
          result3:res.data[0].dictValue,
          directionOptions:res.data,
        })
      }
    })
    var sendData = {
      pageNum:'1',
      pageSize:'100',
      accountName:'',
      bankAccountNumber:'',
      status:'N',
      method:'GET',
      accountType:102
    }

    UTIL.httpRequest(API.URL_GET_ACCOUNTLIST, sendData,{
      success: (res) => {
          console.log(res.rows)

          let option1 = [];
          res.rows.map(rr=>{
            console.log(rr);
            option1.push({
              text: rr.accountName, value: rr.bankAccountNumber
            })
          })

          that.setData({
            option1:option1,
            value1:res.rows[0].bankAccountNumber,
            result1:res.rows[0].bankAccountNumber,
            accountList:res.rows
          })
          let perames = {method:'POST',actacn:res.rows[0].bankAccountNumber,pageSize:'10'}
          //交易明细列表
          UTIL.httpRequest(API.URL_GET_GETRECORDETAILLIST , perames, {
            success: (res) => {
              res.rows.map(rrr=>{
                rrr.directionText = UTIL.getTransform(rrr.direction,that.data.directionOptions);
              })
              that.setData({
                list:res.rows
              })
            }
          })
      }
    })
  },
  goSubmit(){
    var that = this ;
    let form = {
      actacn:that.data.result1 , //付款账户
      payeeActacn: '' , //收款账号
      payeeToname:'' ,  //收款人
      transtype:that.data.result2 ,//  业务类型 数据字典：trans_type  
      direction:that.data.result3 ,// 来往账标识 数据字典：direction  
      params:{
        beginTxnDate:that.data.beginApplyDate,
        endTxnDate:that.data.endApplyDate,
        txnamtMin:that.data.txnamtMin,
        txnamtMax:that.data.xnamtMax,
      },
      pageSize:'10',
      method:'POST',
    }
    console.log(form);
    UTIL.httpRequest(API.URL_GET_GETRECORDETAILLIST , form, {
      success: (res) => {
        res.rows.map(rrr=>{
          rrr.directionText = UTIL.getTransform(rrr.direction,that.data.directionOptions);
        })
        let actacnName = (that.data.option1).filter(function (e) { return e.value == that.data.result1; });
        that.setData({
          value1:that.data.result1,
          value2:that.data.result2,
          value3:that.data.result3,
          actacnName:actacnName[0].text,
          list:res.rows,
          showPopup:false
        })
      }
    })
  },
  bindDateBeginChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      beginApplyDate: e.detail.value
    })
  },
  bindDateEndChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      endApplyDate: e.detail.value
    })
  },
  reset(){
    this.setData({
      result2:'',
      txnamtMin:'',
      xnamtMax:'',
      beginApplyDate:'',
      endApplyDate:''
    })
    this.onLoad();
  },
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  showPopup(){
    var that = this;
    that.setData({
      showPopup:true
    })
  },
  onClose() {
    this.setData({ showPopup: false });
  },
  onChangeZH(event) {
    console.log(event.detail);
    this.setData({
      result1: event.detail,
    });
  },
  onChangeLX(event) {
    console.log(event.detail);
    this.setData({
      result2: event.detail,
    });
  },
  onChangeBS(event) {
    console.log(event.detail);
    this.setData({
      result3: event.detail,
    });
  },
  changeTab1(event){
    var that = this ;
    let actacnName = (that.data.option1).filter(function (e) { return e.value == event.detail; });
    console.log(actacnName);
    this.setData({
      result1: event.detail,
      value1: event.detail,
      actacnName:actacnName[0].text
    });
    let form = {
      actacn:that.data.result1 , //付款账户
      transtype:that.data.result2 ,//  业务类型 数据字典：trans_type  
      direction:that.data.result3 ,// 来往账标识 数据字典：direction  
      pageNum:'1',
      pageSize:'10',
      method:'POST',
    }
    console.log(form);
    UTIL.httpRequest(API.URL_GET_GETRECORDETAILLIST , form, {
      success: (res) => {
        res.rows.map(rrr=>{
          rrr.directionText = UTIL.getTransform(rrr.direction,that.data.directionOptions);
        })
        that.setData({
          list:res.rows,
          showPopup:false
        })
      }
    })
    // UTIL.httpRequest(API.URL_GET_GETRECONCILIATIONMOBILE , form, {
    //   success: (res) => {
    //     res.rows.map(rrr=>{
    //       rrr.checkStatusText = UTIL.getTransform(rrr.checkStatus,that.data.checkStatusOptions);
    //     })
    //     that.setData({
    //       list:res.rows,
    //       showPopup:false
    //     })
    //   }
    // })
  },
  changeTab2(event){
    this.setData({
      result2: event.detail,
      value2: event.detail
    });
    var that = this ;
    let form = {
      actacn:that.data.result1 , //付款账户
      transtype:that.data.result2 ,//  业务类型 数据字典：trans_type  
      direction:that.data.result3 ,// 来往账标识 数据字典：direction  
      pageSize:'10',
      method:'POST',
    }
    console.log(form);
    UTIL.httpRequest(API.URL_GET_GETRECORDETAILLIST , form, {
      success: (res) => {
        res.rows.map(rrr=>{
          rrr.directionText = UTIL.getTransform(rrr.direction,that.data.directionOptions);
        })
        that.setData({
          list:res.rows,
          showPopup:false
        })
      }
    })
  },
  changeTab3(event){
    this.setData({
      result3: event.detail,
      value3: event.detail
    });
    var that = this ;
    let form = {
      actacn:that.data.result1 , //付款账户
      transtype:that.data.result2 ,//  业务类型 数据字典：trans_type  
      direction:that.data.result3 ,// 来往账标识 数据字典：direction  
      pageSize:'10',
      method:'POST',
    }
    console.log(form);
    UTIL.httpRequest(API.URL_GET_GETRECORDETAILLIST , form, {
      success: (res) => {
        res.rows.map(rrr=>{
          rrr.directionText = UTIL.getTransform(rrr.direction,that.data.directionOptions);
        })
        that.setData({
          list:res.rows,
          showPopup:false
        })
      }
    })
  },
  minInput(e){
    var that = this ;
    that.setData({
      txnamtMin: e.detail.value, //金额范围 最小
    })
  },
  maxInput(e){
    var that = this ;
    that.setData({
      xnamtMax: e.detail.value, //金额范围最大
    })
  },
  openDZ(e){
    var that = this;
    that.setData({
      projectId:'',
      projectValue:''
    })
    var id = e.currentTarget.dataset.id;
    that.setData({
      showDialog:true,
      projectId:id
    })
  },
  goDZ(e){
    var that = this ;
    var form = {
      manualRemark:that.data.projectValue,
      id:that.data.projectId,
      method:'POST'
    }
    console.log(form);
    UTIL.httpRequest(API.URL_GET_GETTRANSFEREDIT , form, {
      success: (res) => {
        that.setData({
          showDialog:false
        })
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000,
        })
      }
    })
  },
  bindNameInput(e){
    var that = this;
    var value = e.detail.value;
    that.setData({
      projectValue:value
    })
  },
  toPayDetail(e){
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: 'transactionDetail/transactionDetail?id='+id,
    })
  },
  goHD(e){
    let url = e.currentTarget.dataset.url;
    let alternateField1 = e.currentTarget.dataset.field1;
    let alternateField4 = e.currentTarget.dataset.field4;
    let alternateField9 = e.currentTarget.dataset.field9;
    let id = e.currentTarget.dataset.id;
    console.log(alternateField1);
    console.log(alternateField4);
    console.log(alternateField9);
    if (alternateField1=='3') {
      if (url==null) {
        UTIL.httpRequest(API.URL_GET_GETDIANZIHUIDAN1 + id , {method:'GET'}, {
          success: (res) => {
            console.log(res);
            url = res.data.alternateField2;
            wx.navigateTo({
              url: 'receipt/receipt?url='+url,
            })
          }
        })
      }else if(url.indexOf('失败')>-1){
        UTIL.showToastNoneIcon(url);
      }else{
        wx.navigateTo({
          url: 'receipt/receipt?url='+url,
        })
      }
    }
    if (alternateField4!=null) {
      console.log('aaa');
      if (url==null) {
        UTIL.httpRequest(API.URL_GET_GETDIANZIHUIDAN2 + id , {method:'GET'}, {
          success: (res) => {
            console.log(res);
            url = res.data.alternateField2;
            wx.navigateTo({
              url: 'receipt/receipt?url='+url,
            })
          }
        })
      }else if(url.indexOf('失败')>-1){
        UTIL.showToastNoneIcon(url);
      }else{
        wx.navigateTo({
          url: 'receipt/receipt?url='+url,
        })
      }
    }
    if (alternateField1=='2' && alternateField9!='' && alternateField9!=null) {
      if (url==null) {
        UTIL.httpRequest(API.URL_GET_GETDIANZIHUIDAN3 + id , {method:'GET'}, {
          success: (res) => {
            console.log(res);
            url = res.data.alternateField2;
            wx.navigateTo({
              url: 'receipt/receipt?url='+url,
            })
          }
        })
      }else if(url.indexOf('失败')>-1){
        UTIL.showToastNoneIcon(url);
      }else{
        wx.navigateTo({
          url: 'receipt/receipt?url='+url,
        })
      }
    }

    
  },
  showTransactionDialog(){
    wx.navigateTo({
      url: 'receipt/receipt',
    })
  },
  goSearch(e){
    console.log(e);
    var that = this;
    let form = {
      actacn:that.data.result1 , //付款账户
      payeeActacn: '' , //收款账号
      payeeToname:e.detail ,  //收款人
      transtype:that.data.result2 ,//  业务类型 数据字典：trans_type  
      direction:that.data.result3 ,// 来往账标识 数据字典：direction  
      params:{
        beginTxnDate:that.data.beginApplyDate,
        endTxnDate:that.data.endApplyDate,
        txnamtMin:that.data.txnamtMin,
        txnamtMax:that.data.xnamtMax,
      },
      pageSize:'10',
      method:'POST',
    }
    UTIL.httpRequest(API.URL_GET_GETRECORDETAILLIST , form, {
      success: (res) => {
        res.rows.map(rrr=>{
          rrr.directionText = UTIL.getTransform(rrr.direction,that.data.directionOptions);
        })
        that.setData({
          list:res.rows,
          showPopup:false
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})