const config = require('../../config/config.js')
const Http = require('../../utils/HttpBasics.js')
const format = require('../../utils/util.js')
const util = require('../../utils/util.js')
const qrCodeJS = require('../../utils/qrcode.js')
var app = getApp()

// pages/cardPay/cardPay.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    reachBottom: false,
    phone: '',
    list: []
  },

  getList(pageNum) {
    const that = this;
    let data = {
      userPhone: that.data.phone,
      pageNum,
      pageSize: 10,
    }
    wx.showLoading()
    Http.get({
      url: config.api.cardInfoList,
      data
    })
      .then(res => {
        console.log(res, 'res');
        if (res.data.list) {
          res.data.list.forEach(item => {
            item.createDate = format.formatTime(item.createDate, 'yyyy-MM-dddd hh:mm:ss')
            item.updateDate = format.formatTime(item.updateDate, 'yyyy-MM-dddd hh:mm:ss')
            item.remainingAmount = (item.remainingAmount / 100) || ''

            if (item.wxCardInfo && item.wxCardInfo.id) {
              item.cardIdHide = item.wxCardInfo.id.slice(0, 4) + `******` + item.wxCardInfo.id.slice(14)
            }
          })

          if (data.pageNum == 1) {
            that.setData({
              list: res.data.list
            })
          } else {
            const tempList = that.data.list
            res.data.list.forEach(item => {
              tempList.push(item)
            })
            that.setData({
              list: tempList
            })
          }
        }
        wx.hideLoading()
      })
      .catch(err => {
        console.log(err, 'err');
        wx.showToast({
          title: err.message,
          icon: 'none'
        })
      })
  },

  goToPay(e) {
    const id = e.currentTarget.dataset.id * 1
    wx.navigateTo({
      url: `/pages/cardPay/cardPay?codeInfo=${e.currentTarget.dataset.id}&cardPayType="3"`,
    })
  },

  checkNum() {
    const phoneReg = /^(?:(?:\+|00)86)?1\d{10}$/
    if (!phoneReg.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号！',
        icon: 'none'
      })
      return
    }
    this.setData({
      pageNum: 1,
      reachBottom: true
    });
    this.getList(1)
    console.log(this.data.pageNum, 'pageNum');
  },

  getCardNum(e) {
    this.setData({
      phone: e.detail.value
    })
  },

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

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

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

  },

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

  },

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

  },

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (!this.data.reachBottom) return
    let pageNum = this.data.pageNum + 1
    this.setData({
      pageNum: pageNum
    });
    this.getList(this.data.pageNum);
  },

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

  }
})