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,
    phone: '',
    codeNum: '',
    inputNum: '',
    cardType: '',
    reachBottom: false
  },

  scanCode() {
    const that = this
    wx.scanCode({
      success: (res) => {
        // 清空输入框中的号码
        that.setData({
          inputNum: '',
          phone: ''
        })
        console.log(res.result, 'res');
        // 是否为电子卡
        const isECard = that.isJSON(res.result)

        // 电子卡
        if (isECard) {
          const cardInfo = JSON.parse(res.result)
          this.getDetail(cardInfo.ID)
          // 实体卡
        } else {
          const cardID = res.result
          this.getDetail(cardID, 1)
        }
      },
      fail: (res) => {
        wx.showToast({
          title: "未识别到二维码",
          icon: "none",
          mask: false
        })
      }
    })
  },

  getList(pageNum) {
    // 获取当前的时间
    const myDate = new Date();
    const startDate = myDate.getFullYear() + '-' + (myDate.getMonth() + 1) + '-01' + ' 00:00:00';
    const endDate = myDate.getFullYear() + '-' + (myDate.getMonth() + 1) + '-' + myDate.getDate() + ' 29:59:59';
    const that = this;
    let data = {
      ouPhone: that.data.phone,
      cardId: that.data.codeNum,
      startdate: startDate,
      enddate: endDate,
      pageNum,
      pageSize: 10,
      payType: 0,

    }
    if (that.data.cardType) {
      data.cardType = that.data.cardType
    }

    wx.showLoading()
    Http.get({
      url: config.api.cardPayList,
      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,
              reachBottom: true // 允许下拉刷新
            })
          } else {
            const tempList = that.data.list
            res.data.list.forEach(item => {
              tempList.push(item)
            })
            that.setData({
              list: tempList
            })
          }
        } else {
          if (this.data.pageNum == 1) {
            that.setData({
              list: []
            })
          }
        }
        wx.hideLoading()
      })
      .catch(err => {
        console.log(err, 'err');
        wx.showToast({
          title: err.message,
          icon: 'none'
        })
      })
  },

  getDetail(dynamicId, cardType) {
    const that = this
    Http.get({
      url: config.api.cardInfoDetail + "?dynamicId=" + dynamicId,
    }).then(res => {
      that.setData({
        codeNum: res.data.cardId,
        inputNum: res.data.cardId, // 显示在输入框中的号码
        phone: '',// 将手机号置空
        cardType: cardType
      })
      this.getList(1)
    }).catch(err => {
      console.log(err, 'err');
      wx.showToast({
        title: err.message,
        icon: 'none'
      })
    })
  },

  goToRefund(e) {
    const item = e.currentTarget.dataset.item
    console.log(item, 'item');
    wx.navigateTo({
      url: `/pages/refundDetail/refundDetail?item=${JSON.stringify(item)}`,
    })
  },

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

    this.setData({
      pageNum: 1,
    });
    this.getList(1)
    console.log(this.data.pageNum, 'pageNum');
  },

  getCardNum(e) {
    const phoneReg = /^(?:(?:\+|00)86)?1\d{10}$/
    const isPhone = phoneReg.test(e.detail.value)

    // 当输入的值为手机号时
    if (isPhone) {
      this.setData({
        phone: e.detail.value,
        codeNum: '',
      })
      // 不为手机号时清空手机号
    } else {
      this.setData({
        codeNum: e.detail.value,
        phone: ''
      })
    }

  },

  // 检测数据是否为JSON字符串
  isJSON(item) {
    item = typeof item !== "string"
      ? JSON.stringify(item)
      : item;

    try {
      item = JSON.parse(item);
    } catch (e) {
      return false;
    }

    if (typeof item === "object" && item !== null) {
      return true;
    }

    return false;
  },

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

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log(this.data, 'data');
    if (this.data.phone || this.data.codeNum) {
      this.setData({
        pageNum: 1
      })
      this.getList(1)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  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() {

  }
})