
const config = require('../../../config/config.js')
const Common = require('../../../common/common.js')
const format = require('../../../utils/util.js')
var app = getApp()
Page({
  data: {
    val: '请输入券码',
    couponOrderId: null,
    couponOrderDetail: null,
    statusImg: '', 
    statusText: '',
    flag: false,
    detail: '',
    remark: '',
    contentType: 0,

  },
  rule() {
    wx.navigateTo({
      url: `/pages/main/rule/rule?detail=${this.data.couponOrderDetail.detail}&remark=${this.data.couponOrderDetail.remark}&contentType=${this.data.contentType}`
    })
  },
  onLoad(options) {
    console.log(wx.getStorageSync("couponOrderId"))
    if (wx.getStorageSync("couponOrderId")) {
      this.setData({
        couponOrderId: wx.getStorageSync("couponOrderId")
      })
      this.detailInfo(wx.getStorageSync("couponOrderId"))
    }
  },
  detailInfo(couponOrderId) {
    var that = this;
    Common.getCouponOrderDetail(couponOrderId)
    .then(res => {
      console.log(res)
      if (res.code == 200) {

        this.setData({
          contentType: res.data.contentType ? res.data.contentType : 0
        })
        console.log(this.data.contentType)
        if (res.data.contentType != undefined && res.data.contentType == 1) {
          Common.getHtml(couponOrderId).then(res => {
            if (res.data.html && res.data.html != '') {
              app.globalData.curHtml = decodeURI(res.data.html);
            }
          })
        }
        
        res.data.createDate = format.formatTime(res.data.createDate, 'yyyy-MM-dddd hh:mm:ss')
        res.data.expiredTime = format.formatTime(res.data.expiredTime, 'yyyy-MM-dddd hh:mm:ss')
        switch (res.data.couponOrderStatus) {
          case 0:
            //待使用
            that.setData({
              statusText: '未核销',
              flag: true
            })
            break;
          case 1:
            //已核销
            that.setData({
              statusImg: '/static/images/use.png',
              statusText: '已核销',
              flag: false
            })
            break;
          case 2:
            that.setData({
              statusImg: '/static/images/beOverdue.png',
              statusText: '已过期',
              flag: false
            })
            //已过期
            break;
          case 3:
            that.setData({
              statusImg: '/static/images/refund.png',
              statusText: '已退款',
              flag: false
            })
            //已作废
            break;
          default:
            wx.showToast({
              title: '数据有误',
              image:'./../../../static/images/fail.png'
            })
        }
        console.log(res)
        console.log("我是请求回来的数据")
        that.setData({
          couponOrderDetail: res.data
        })
      } else {
        wx.reLaunch({
          url: 'pages/main/writeoffPage/errorPage/errorPage',
        })
      }
    })
  },
  user() {
    console.log(wx.getStorageSync("couponOrderId"))
    // return
    Common.couponOrderVerify(wx.getStorageSync("couponOrderId"), wx.getStorageSync("verifyRemark"))
    .then(res => {
      console.log(res)
      if (res.code == 200) {
        wx.redirectTo({
          url: '../success/success',
        })
      }else{
        wx.showModal({
          title: '抱歉',
          content: res.message,
          showCancel:false
        })
      }
    })
    .catch(error=>{
      console.log(error)
      wx.showModal({
        title: '抱歉',
        content: error.message,
        showCancel: false
      })
    })
  },
  reBuck() {
    wx.switchTab({
      url: '/pages/main/main',
    })
  }
})