let util = require("../../utils/util");
let Http = require("../../utils/HttpBasics");
let config = require("../../config/config.js");
let app = getApp();
Page({
  data: {
    code: "",
    //存储计时器
    setInter: "",
    staticGamedata: {},
    showIf: false,
  },
  gotogame: function () {
    let that = this;
    wx.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 => {
        wx.showToast({
          title: err.errMsg,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      })
  },
  onLoad: function (options) {
    let that = this;
    util.barcode("barcode", options.quancode, 500, 100);
    util.qrcode("qrcode", options.quancode, 350, 350);
    that.setData({
      code: options.quancode,
      title: options.title,
      subtitle: options.subtitle,
      remark: options.remark,
      couponorderstatus: options.couponorderstatus
    });
    /**
     * 如果没有核销
     */
    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 => {
            wx.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 => {
          wx.showToast({
            title: err.errMsg,
            icon: 'none',
            duration: 2000,
            mask: false
          });
        })
    }
    /**
     * couponorderstatus
     * 0 没有核销
     * 1 已经核销成功
     */
  },
  // onShow(options) {
  //   console.log("屏幕的亮度")
  //   setTimeout(function () {
  //     wx.setScreenBrightness({
  //       value: 1,
  //     })
  //   }, 200)
  // },
  onUnload: function () {
    let that = this;
    clearInterval(that.data.setInter);
  },
  onHide: function () {
    let that = this;
    clearInterval(that.data.setInter);
  },
  // onHide:function(){
  //   console.log(app.globalData.sight)
  //   wx.setScreenBrightness({
  //     value: app.globalData.sight,
  //   })
  // }
});
