// let util = require("../../utils/util");
const navigationBarHeight = (getApp().statusBarHeight + 50) + 'px'
let Http = require("../../utils/HttpBasics");
let config = require("../../config/config.js");
let app = getApp();
const imgurl = require("../../utils/imgurl");
const QR = require("../../utils/memberqrcode.js");
Page({
  data: {
    navigationBarHeight,
    btomLineUrl: imgurl.btomLine.url,
    topLineUrl: imgurl.topLine.url,
    lineUrl: imgurl.line.url,
    wm01Url: imgurl.wm01.url,
    wm02Url: imgurl.wm02.url,
    wm03Url: imgurl.wm03.url,
    wm04Url: imgurl.wm04.url,
    upDataRqUrl: imgurl.upDataRq.url,
    upDataRqUrlF: imgurl.upDataRqF.url,
    code: "",
    //存储计时器
    setInter: "",
    staticGamedata: {},
    showIf: false,
    curHtml: '',
    contentType: 0,
    showhieRq: false,
    dynamicRq: "",
    expiredSeconds: 0,
    templTiem: ""
  },
  gotogame: function () {
    let that = this;
    tt.navigateTo({
      url: '/pages/game/index?url=' + that.data.staticGamedata.url + "&id=" + that.data.staticGamedata.id + "&gameId=" + that.data.staticGamedata.gameId,
    })
  },
  // 获取游戏
  getStaticGame(token) {
    let _this = this;
    Http.get({
        url: config.api.getGame,
        data: {
          triggleAction: 4 // 核销触发
        }
      }).then(res => {
        if (res.data.id) {
          _this.setData({
            showIf: true
          })
        }
        _this.setData({
          staticGamedata: res.data
        })
      })
      .catch(err => {
        tt.showToast({
          title: err.errMsg,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      })
  },

  setRq() {
    let _this = this
    _this.setData({
      showhieRq: false,
    })
    Http.get({ //获取动态二维码
      url: config.api.dynamicId,
      data: {
        couponOrderId: _this.data.code
      }
    }).then(res => {
      console.log(res, "res")
      _this.setData({
        dynamicRq: res.data.dynamicId,
        expiredSeconds: res.data.expiredSeconds
      })

      let url = JSON.stringify({
        END: "C",
        TYPE: "couponorder",
        ID: _this.data.dynamicRq
      })
      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", 350, 350);
    }).catch(err => {
      tt.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000,
        mask: false
      });
    })
  },

  onLoad: function (options) { //quancode
    console.log(options)
    console.log(app.globalData.curHtml)
    let that = this;
    // let value = JSON.stringify({ END: "C", TYPE: "couponorder", ID: options.quancode})
    // that.createQrCode(value, "qrcode", 350, 350);
    that.setData({
      code: options.quancode,
      title: options.title,
      subtitle: options.subtitle,
      remark: options.remark,
      couponorderstatus: options.couponorderstatus,
      validStatus: options.validstatus,
      contentType: options.contentType
    });
    if (options.couponorderstatus == 0) {
      that.setRq() //动态获取二维码 这个方法必须在拿到 options.quancode 后调用
    }

    if (options.contentType != undefined && options.contentType == 1) {
      this.setData({
        curHtml: app.globalData.curHtml
      })
    }
    /**
     * 如果没有核销
     */
    that.data.setInter = setInterval(function () {
      if (that.data.couponorderstatus == 0) {
        Http.get({
            url: config.api.getStatus,
            data: {
              couponOrderId: options.quancode
            }
          }).then(res => {
            console.log(res);
            that.setData({
              couponorderstatus: res.data.CouponOrderStatus
            });
            if (res.data.CouponOrderStatus == 1) {
              /**
               * 动态改变上一级页面的核销状态
               */
              that.getStaticGame()
              var pages = getCurrentPages();
              var prevPage = pages[pages.length - 2]; //上一个页面
              //直接调用上一个页面的setData()方法,把数据存到上一个页面中去
              prevPage.setData({
                mystatus: res.data.CouponOrderStatus
              });
            }
          })
          .catch(err => {
            tt.showToast({
              title: err.errMsg,
              icon: 'none',
              duration: 2000,
              mask: false
            });
          })
      }
    }, 2000);

    if (that.data.couponorderstatus == 1) {
      /**
       * 如果已经核销
       * 不需要循环
       */
      Http.get({
          url: config.api.getStatus,
          data: {
            couponOrderId: options.quancode
          }
        }).then(res => {
          that.setData({
            couponorderstatus: res.data.CouponOrderStatus
          });
        })
        .catch(err => {
          tt.showToast({
            title: err.errMsg,
            icon: 'none',
            duration: 2000,
            mask: false
          });
        })
    }
    /**
     * couponorderstatus
     * 0 没有核销 
     * 1 已经核销成功
     */
  },
  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);
  },
  // onHide:function(){
  //   console.log(app.globalData.sight)
  //   tt.setScreenBrightness({
  //     value: app.globalData.sight,
  //   })
  // }
});