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()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item: {},
    cardSend: {},
    refund: {},
    refundFee: '',
    payStatus: 0 // 3退款中、4已退款
  },

  getPayMoney(e) {
    this.setData({
      refundFee: e.detail.value
    })
  },

  getDetail(cardSpendId) {
    const that = this
    const data = {
      cardSpendId: cardSpendId
    }
    Http.get({
      url: config.api.cardPayDetail,
      data
    }).then(res => {
      if (res.data.cardSend) {
        res.data.cardSend.createDate = that.timestampToTime(res.data.cardSend.createDate, 'YYYY-MM-DD hh:mm:ss')
      }

      if (res.data.refund) {
        res.data.refund.createTime = that.timestampToTime(res.data.refund.createTime, 'YYYY-MM-DD hh:mm:ss')
      }

      that.setData({
        cardSend: res.data.cardSend,
        refund: res.data.refund,
      })
    }).catch(err => {
      console.log(err, 'err');
    })
  },

  refundConfirm() {
    const data = {
      cardSpendId: this.data.item.id,
      refundFee: this.data.refundFee
    }
    Http.post({
      url: config.api.cardRefundCardOrder,
      data
    })
      .then(res => {
        wx.showToast({
          title: '申请成功！',
          icon: 'success'
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 1500);

      }).catch(err => {
        console.log(err, 'err');
        wx.showToast({
          title: err.message,
          icon: 'none'
        })
      })
  },

  back() {
    wx.navigateBack()
  },


  /**
  * @description 根据时间戳获取时间
  * @param {*} timestamp 必传，number类型，时间戳数据（10位及以下，10位至13位）;若不传，则返回：“无时间戳”
  * @param {*} format  选传，string类型，提供以下时间格式：YYYY-MM-DD hh:mm:ss、YYYY/MM/DD hh:mm:ss、YYYY.MM.DD hh:mm:ss、YYYY MM DD hh:mm:ss、YYYY年MM月DD日 hh:mm:ss、YYYY-MM-DD、YYYY/MM/DD、YYYY.MM.DD、YYYY MM DD、YYYY年MM月DD日；若不传，则默认为：YYYY-MM-DD
  * @returns 根据要求的时间格式
  * @version V 1.0, Created by YWQ, 2022.10.20
  */
  timestampToTime(timestamp, format) {
    //时间戳为10位需*1000，时间戳为13位不需乘1000
    const length = timestamp.length
    if (length <= 10) {
      var date = new Date(timestamp * 1000)
    } else {
      var date = new Date(timestamp)
    }
    let Y = String(date.getFullYear())
    let M = String(date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
    let D = String(date.getDate() + 1 < 10 ? '0' + (date.getDate()) : date.getDate())
    let h = String(date.getHours() + 1 < 10 ? '0' + (date.getHours()) : date.getHours())
    let m = String(date.getMinutes() + 1 < 10 ? '0' + (date.getMinutes()) : date.getMinutes())
    let s = String(date.getSeconds() + 1 < 10 ? '0' + (date.getSeconds()) : date.getSeconds())
    // return Y + M + D + h + m + s
    if (format == "YYYY-MM-DD hh:mm:ss") {
      return Y + "-" + M + "-" + D + " " + h + ":" + m + ":" + s
    } else if (format == "YYYY/MM/DD hh:mm:ss") {
      return Y + "/" + M + "/" + D + " " + h + ":" + m + ":" + s
    } else if (format == "YYYY.MM.DD hh:mm:ss") {
      return Y + "." + M + "." + D + " " + h + ":" + m + ":" + s
    } else if (format == "YYYY MM DD hh:mm:ss") {
      return Y + " " + M + " " + D + " " + h + ":" + m + ":" + s
    } else if (format == "YYYY年MM月DD日 hh:mm:ss") {
      return Y + "年" + M + "月" + D + "日" + " " + h + ":" + m + ":" + s
    } else if (format == "YYYY-MM-DD") {
      return Y + "-" + M + "-" + D
    } else if (format == "YYYY/MM/DD") {
      return Y + "/" + M + "/" + D
    } else if (format == "YYYY.MM.DD") {
      return Y + "." + M + "." + D
    } else if (format == "YYYY MM DD") {
      return Y + " " + M + " " + D
    } else if (format == "YYYY年MM月DD日") {
      return Y + "年" + M + "月" + D + "日"
    } else {
      return Y + "-" + M + "-" + D
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.item) {
      console.log(options, 'options');
      const item = JSON.parse(options.item)
      console.log(item, 'item');
      this.setData({
        item: item
      })
      this.getDetail(item.id)
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})