const app = getApp();
const navigationBarHeight = (getApp().statusBarHeight + 50) + "px";
const Http = require("../../../utils/HttpBasics");
const config = require("../../../config/config");
const util = require("../../../utils/util");
const QR = require("../../../utils/memberqrcode.js");
Page({
  data: {
    showIdFalg: false,
    navigationBarHeight,
    couponorderstatus: "",
    quancode: "",
    setInter: "",
    validStartDate: "",
    validEndDate: "",
    pickStartDate: "",
    pickEndDate: "",
    expiredTime: "",
    updateDate: "",
    createDate: "",
    code: "",
    data: {

    },
    showhieRq: false,
    dynamicRq: "",
    expiredSeconds: 0,
    couponTenantId: '',
    templTiem: "",
    curHtml: "",
    tenantId: '',
    mallList: '',
    mallTenantId: ""
  },
  showId() {
    let this_ = this;
    if (!this_.data.showIdFalg) {
      tt.showModal({
        // title: "查看券码",
        content: "是否查看完整券码",
        success(res) {
          if (res.confirm) {
            console.log("confirm, continued");
            this_.setData({
              showIdFalg: true
            })
          } else if (res.cancel) {
            //取消
            console.log("cancel, cold");
          } else {
            // what happend?
          }
        },
        fail(err) {
          console.log(`showModal 调用失败`, err);
        },
      });
    } else {
      this.setData({
        showIdFalg: false
      })
    }

  },

  //获取适用门店
  getCouponMerchant(couponId) {
    Http.get({
      url: `/wxCoupon/couponMerchantByCouponId?couponId=${couponId}&mallTenantId=${this.data.mallTenantId || ''}`,
    }).then(res => {
      const keys = Object.keys(res.data)
      const mallList = []
      let i = 0
      keys.forEach(item => {
        const arr = item.split('|')
        const obj = {
          tenantId: arr[0],
          mallName: arr[1],
          merchantVoList: res.data[item],
          expand: false
        }
        if (i == 0) {
          obj.expand = true
        }
        mallList.push(obj)
        i++
      })
      this.setData({
        mallList: mallList
      })
    }).catch(err => {

    })
  },

  getHtml(couponOrderId) {
    Http.get({
      url: config.api.couponHtmlDetailForPkg,
      data: {
        couponOrderId: couponOrderId
      }
    }).then(res => {
      console.log(res)
      if (res.code == 200 && res.data.html) {
        this.setData({
          curHtml: decodeURI(res.data.html)
        })
        // app.globalData.curHtml = this.data.curHtml;
        // console.log(app.globalData.curHtml)
      }
    })
  },

  onLoad: function (options) {
    let that = this;
    console.log(options);
    that.setData({
      code: options.quancode,
      codeS: options.quancode.slice(0, 4) + `******` + options.quancode.slice(14),
      couponorderstatus: options.couponorderstatus,
      validStatus: options.validStatus
    });

    if (options.couponorderstatus == 0 && options.validStatus != 0) {
      that.setRq() //动态二维码 //这个方法必须在拿到 options.quancode 后调用
    }

    if (options.mallTenantId) {
      that.setData({
        mallTenantId: options.mallTenantId
      })
    }

    //初始数据不能延时收到写一边
    Http.get({
      url: config.api.couponOrderDetail,
      data: {
        couponOrderId: options.quancode,
        mallTenantId: options.mallTenantId
      }
    }).then(res => {
      if (res.data.tenantId) {
        that.setData({
          tenantId: res.data.tenantId
        })
        that.setRq()
        that.getCouponMerchant(res.data.couponId)
      }
      that.setData({
        couponorderstatus: res.data.couponOrderStatus,
        data: res.data,
      });

      that.setData({
        expiredTime: util.formatTime(that.data.data.expiredTime, "yyyy-MM-dd hh:mm:ss"),
        updateDate: util.formatTime(that.data.data.updateDate, "yyyy-MM-dd hh:mm:ss"),
        createDate: util.formatTime(that.data.data.createDate, "yyyy-MM-dd hh:mm:ss")
      });
      if (res.data.contentType != undefined && res.data.contentType == 1) {
        //获取图文展示详情html
        that.setData({
          contentType: res.data.contentType
        })
        that.getHtml(options.quancode);
      }
      if (that.data.data.validStartDate && that.data.data.validEndDate) {
        that.setData({
          validStartDate: util.formatTime(that.data.data.validStartDate, "yyyy-MM-dd hh:mm:ss"),
          validEndDate: util.formatTime(that.data.data.validEndDate, "yyyy-MM-dd hh:mm:ss"),
          // pickStartDate: util.formatTime(res.data.pickStartDate, "yyyy-MM-dd hh:mm:ss"),
          // pickEndDate: util.formatTime(res.data.pickEndDate, "yyyy-MM-dd hh:mm:ss"),
        })
      }

    }).catch(err => {
      console.log(err, 'err');
      tt.showToast({
        title: err.message ? err.message : err.data,
        icon: 'none',
        duration: 2000,
        mask: false
      });
    })
    //




    //获得优惠券的详情
    let setInter = setInterval(function () {
      if (
        options.quancode &&
        that.data.couponorderstatus == 0
      ) {

        Http.get({
          url: config.api.couponOrderDetail,
          data: {
            couponOrderId: options.quancode,
            mallTenantId: options.mallTenantId
          }
        }).then(res => {
          console.log(res.data, '1233211234567');
          that.setData({
            couponorderstatus: res.data.couponOrderStatus,
            data: res.data,
          });
          if (res.data.tenantId) {
            that.setData({
              tenantId: res.data.tenantId
            })
            that.setRq()
          }
          console.log(that.data.tenantId, 'tenantId');
          that.setData({
            expiredTime: util.formatTime(that.data.data.expiredTime, "yyyy-MM-dd hh:mm:ss"),
            updateDate: util.formatTime(that.data.data.updateDate, "yyyy-MM-dd hh:mm:ss"),
            createDate: util.formatTime(that.data.data.createDate, "yyyy-MM-dd hh:mm:ss")
          });
          if (that.data.data.validStartDate && that.data.data.validEndDate) {
            that.setData({
              validStartDate: util.formatTime(that.data.data.validStartDate, "yyyy-MM-dd hh:mm:ss"),
              validEndDate: util.formatTime(that.data.data.validEndDate, "yyyy-MM-dd hh:mm:ss"),
              // pickStartDate: util.formatTime(res.data.pickStartDate, "yyyy-MM-dd hh:mm:ss"),
              // pickEndDate: util.formatTime(res.data.pickEndDate, "yyyy-MM-dd hh:mm:ss"),
            })
          }

        }).catch(err => {
          tt.showToast({
            title: err.message ? err.message : err.data,
            icon: 'none',
            duration: 2000,
            mask: false
          });
        })
      }
    }, 2000);
    that.setData({
      setInter: setInter
    })
  },
  setRq() {
    let _this = this
    _this.setData({
      showhieRq: false,
    })
    Http.get({ //获取动态二维码
      url: config.api.dynamicId,
      data: {
        couponOrderId: _this.data.code,
        couponTenantId: _this.data.tenantId || ''
      }
    }).then(res => {
      console.log(res, "res")
      _this.setData({
        dynamicRq: res.data.dynamicId,
        expiredSeconds: res.data.expiredSeconds,
        couponTenantId: res.data.couponTenantId
      })

      let url = JSON.stringify({
        END: "C",
        TYPE: "couponorder",
        ID: _this.data.dynamicRq,
        couponTenantId: _this.data.couponTenantId
      })
      if (res.data.expiredSeconds == 0) {

      } else {
        let inre = setInterval(() => {
          if (_this.data.expiredSeconds > 1) {
            _this.setData({
              expiredSeconds: _this.data.expiredSeconds - 1
            })
            console.log("有效", _this.data.expiredSeconds)
          } else {
            console.log("无效", _this.data.expiredSeconds)
            clearInterval(_this.data.templTiem)
            _this.setData({
              showhieRq: true
            })
          }
        }, 1000)
        _this.setData({
          templTiem: inre
        })
      }
      // util.qrcode("qrcode", url, 350, 350);
      _this.createQrCode(url, "qrcode", 210, 210);
    }).catch(err => {
      tt.showToast({
        title: err.message,
        icon: 'none',
        duration: 4000,
        mask: false
      });
    })
  },

  createQrCode: function (url, canvasId, cavW, cavH) {
    //调用插件中的draw方法，绘制二维码图片
    let that = this;
    QR.api.draw(url, canvasId, cavW, cavH, function (res) {
      that.setData({
        tempFilePath: res
      })
    });
  },
  onUnload: function () {
    let that = this;
    clearInterval(that.data.setInter);
    // clearInterval(that.data.templTiem);
  },
  onHide: function () {
    let that = this;
    clearInterval(that.data.setInter);
    // clearInterval(that.data.templTiem);
  },
})