// 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:[],
    pageNum:1,
    scrollHeight:'',
    searchName:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    // 可优化：数据请求放到  onload中   新增后在子页面修改父页面数据
    var that = this;
    that.setData({
      index:options.index,
      scrollHeight:wx.getSystemInfoSync().windowHeight
    })
    var that = this;
      // 获取收款账号列表
      let prames = {
        pageNum:1,
        pageSize:10,
        orderByColumn:'id',
        isAsc:'desc',
        status:'0',
        method:'GET'
      }
      console.log(prames);
      UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'bank_type_all', {method:'GET'}, {
        success: (res2) => {
          that.setData({
            bankTypeOptions:res2.data,
          })
          UTIL.httpRequest(API.URL_GET_SELECTLIST , prames, {
            success: (res) => {
              let array = res.rows ; 
              var list = [];
              res.rows.map(res3=>{
                // .replace(/^(.{6})(?:\d+)(.{4})$/,"\$1****\$2")
                res3.payeeAccountText = res3.payeeAccount;
                res3.bankTypeText = UTIL.getTransform(res3.bankType,that.data.bankTypeOptions);
                
                list.push(res3)
              })
              console.log(list);
              that.setData({
                list:list,
              })
            }
          })
        }
      })
      
  },
  goAdd(){
    wx.navigateTo({
      url: '../add/add',
    })
  },
  goSearch(e){
    var that = this;
    that.setData({
      searchName:e.detail,
    })
    // 获取收款账号列表
    let prames = {
      pageNum:1,
      pageSize:10,
      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=>{
          // .replace(/^(.{6})(?:\d+)(.{4})$/,"\$1****\$2")
          res.payeeAccountText = res.payeeAccount;
          res.bankTypeText = (that.data.bankTypeOptions.filter(function (e) { return e.dictValue == res.bankType; }))[0].dictLabel;
          list.push(res)
        })
        that.setData({
          list:list,
        })
      }
    })
  },
  paging(){
    console.log('aaa')
    var that = this;
    that.setData({
      pageNum:that.data.pageNum+1,
    })
    // 获取收款账号列表
    let prames = {
      pageNum:that.data.pageNum,
      pageSize:10,
      orderByColumn:'id',
      isAsc:'desc',
      status:'0',
      method:'GET',
      name:that.data.searchName,
    }
    UTIL.httpRequest(API.URL_GET_SELECTLIST , prames, {
      success: (res) => {
        let array = res.rows ; 
        var payeeType = that.data.payeeType;
        var list = that.data.list;
        var pagecount = res.total/10
        if(that.data.pageNum>pagecount){
          return;
        }
        array.map(res=>{
          // .replace(/^(.{6})(?:\d+)(.{4})$/,"\$1****\$2")
          res.payeeAccountText = res.payeeAccount;
          console.log(that.data.bankTypeOptions);
          res.bankTypeText = (that.data.bankTypeOptions.filter(function (e) { return e.dictValue == res.bankType; }))[0].dictLabel;

          console.log(res);
          list.push(res)
        })
        that.setData({
          list:list,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  delete(e){
    var that = this;

    wx.showModal({
      title: '提示',
      content: '确认删除？',
      success (res) {
        if (res.confirm) {
          UTIL.httpRequest(API.URL_GET_PAYEEREMOVE + e.currentTarget.dataset.id, {method:'GET'},{
            success: (res) => {
              if (res.code == API.SUCCESS_CODE) {
                that.data.list.splice(e.currentTarget.dataset.index, 1); 
                wx.showToast({
                  title: '删除成功！',
                  icon: 'success',
                  duration: 2000
                })
                that.setData({
                  list : that.data.list,
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

    
  },
  goEdit(e){
    wx.navigateTo({
      url: '/pages/payee/add/add?id='+e.currentTarget.dataset.id,
    })
  },

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

  toggle(event) {
    const { index } = event.currentTarget.dataset;
    const checkbox = this.selectComponent(`.checkboxes-${index}`);
    checkbox.toggle();
  },
  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;
    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中的值
      })
      that.back()
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

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

  },

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

  },

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

  },

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

  }
})