// pages/payee/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,
    list: {},
    result: "",
    bankTypeOptions:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    // 可优化：数据请求放到  onload中   新增后在子页面修改父页面数据
    var that = this;
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'bank_type', {method:'GET'}, {
      success: (res) => {
        that.setData({
          bankTypeOptions:res.data,
        })
      }
    })
    that.setData({
      // isPeers:options.isPeers,//是否同行
      // bankType:options.bankType,//所属银行
      // accountType:options.accountType,//账户类型
      payeeType:options.transferType,//申请转帐类型
      money:options.money,//申请转帐类型
      index:options.index
    })
    var that = this;
      // 获取收款账号列表
      let prames = {
        pageNum:1,
        pageSize:999,
        orderByColumn:'id',
        isAsc:'desc',
        // isPeers:that.data.isPeers,//是否同行
        // bankType:that.data.bankType,//所属银行
        // accountType:that.data.accountType,//账户类型
        // payeeType:that.data.payeeType,//申请转帐类型
        status:'0',
        method:'GET'
      }
      UTIL.httpRequest(API.URL_GET_SELECTLIST , prames, {
        success: (res) => {
          let array = res.rows ; 
          var payeeType = that.data.payeeType;
          var list = [];
          array.map(res=>{
            res.payeeAccountText = res.payeeAccount.replace(/^(.{6})(?:\d+)(.{4})$/,"\$1****\$2");
            res.bankTypeText = (that.data.bankTypeOptions.filter(function (e) { return e.dictValue == res.bankType; }))[0].dictLabel;
            if(payeeType == 2 && array.length>0){
              if(res.payeeType == 4 || res.payeeType == 3 ){
                list.push(res)
              }
            }
            if(payeeType == 1 && array.length>0){
              if(res.payeeType == 1){
                list.push(res)
              }
            }
          })
          that.setData({
            list:list,
          })
        }
      })
  },
  goAdd(){
    wx.navigateTo({
      url: 'add/add',
    })
  },
  goSearch(e){
    var that = this;
    // 获取收款账号列表
    let prames = {
      pageNum:1,
      pageSize:999,
      orderByColumn:'id',
      isAsc:'asc',
      // isPeers:that.data.isPeers,//是否同行
      // bankType:that.data.bankType,//所属银行
      // accountType:that.data.accountType,//账户类型
      // payeeType:that.data.payeeType,//申请转帐类型
      status:'0',
      name:e.detail,
      method:'GET'
    }
    console.log(prames);
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'bank_type', {method:'GET'}, {
      success: (res) => {
        that.setData({
          bankTypeOptions:res.data,
        })
      }
    })
    UTIL.httpRequest(API.URL_GET_PAYEESELECTLIST , prames, {
      success: (res) => {
        let array = res.rows ; 
        var payeeType = that.data.payeeType;
        var list = [];
        array.map(res=>{
          res.payeeAccountText = res.payeeAccount.replace(/^(.{6})(?:\d+)(.{4})$/,"\$1****\$2");
          res.bankTypeText = (that.data.bankTypeOptions.filter(function (e) { return e.dictValue == res.bankType; }))[0].dictLabel;
          if(payeeType == 2 && array.length>0){
            if(res.payeeType == 4 || res.payeeType == 3 ){
              list.push(res)
            }
          }
          if(payeeType == 1 && array.length>0){
            if(res.payeeType == 1){
              list.push(res)
            }
          }
        })
        that.setData({
          list:list,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },
  onChange(event) {
    console.log(event);
    this.setData({
      result: event.detail,
    });
  },

  toggle(event) {
    const { index } = event.currentTarget.dataset;
    this.setData({
      result: index,
    });
    console.log(index);
  },
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  noop() {},
  goSubmit(){
    var that = this;
    if(that.data.result == ''){
      UTIL.showToastNoneIcon('请选择一个收款人！');
      return;
    }
    let array = that.data.list.filter(function (e) { return e.id == that.data.result; });
    array[0].incomeAmount = that.data.money;
    console.log(array);
    let pages = getCurrentPages();
    let currentPage = null;   //当前页面
    let prevPage = null;  //上一个页面
    currentPage = pages[pages.length - 1]; //获取当前页面，将其赋值
    prevPage = pages[pages.length - 2]; //获取上一个页面，将其赋值
    if (prevPage) {
      prevPage.setData({         
        ["form.transfers["+that.data.index+"].payeeList[0]"]: array[0],//将想要传的信息赋值给上一个页面data中的值
        ["form.transfers["+that.data.index+"].accountType"]:array[0].accountType
      })
      that.back()
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

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

  },

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

  },

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

  },

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

  }
})