const navigationBarHeight = (getApp().statusBarHeight + 60) + 'px'
const util = require("../../../utils/util");
const config = require("../../../config/config.js");
const Http = require("../../../utils/HttpBasics");
const imgurl = require("../../../utils/imgurl");
let app = getApp();
//券详情页面
Page({
  data: {
    navigationBarHeight,
    code: "",
    lineUrl: imgurl.line.url,
    wm01Url: imgurl.wm01.url,
    wm02Url: imgurl.wm02.url,
    wm03Url: imgurl.wm03.url,
    teljpgUrl: imgurl.teljpg.url,
    newUrl: imgurl.new1.url,
    data: {
      salePrice: null,
      price: null,
      usePrice: null
    },
    createDate: "",
    expiredTime: "",
    updateDate: "",
    //存储计时器
    setInter: "",
    staticGamedata: {},
    showIf: false,
  },
  onUnload:function(){
    let that = this;
    clearInterval(that.data.setInter);
  },
  onHide:function(){
    let that = this;
    clearInterval(that.data.setInter);
  },
  gotogame: function () {
    let that = this;
    wx.redirectTo({
      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
        });
      })
  },
  // onShow(options) {
  //   setTimeout(function () {
  //     wx.setScreenBrightness({
  //       value: 0.7,
  //     })
  //   }, 200)
  // },
  onLoad: function (options) {
    let that = this;
    that.setData({
      code: options.quancode,
      couponorderstatus: options.couponorderstatus
    });
    //获得优惠券的详情
    that.data.setInter = setInterval(function () {
      if (
        options.quancode &&
        that.data.couponorderstatus == 0
      ) {
        Http.get({
          url: config.api.couponOrderDetail,
          data: {
            couponOrderId: options.quancode
          }
        }).then(res => {
          console.log(res);
          that.setData({
            couponorderstatus: res.data.couponOrderStatus,
            data: res.data
          });
          if (res.data.couponOrderStatus == 1) {
            /**
             * 动态改变上一级页面的核销状态
             */

            that.getStaticGame()
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2]; //上一个页面
            //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
            prevPage.setData({
              mystatus: res.data.couponOrderStatus
            });
          }
          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")
          });
        })
          .catch(err => {
            wx.showToast({
              title: err.errMsg,
              icon: 'none',
              duration: 2000,
              mask: false
            });
          })
      }
    }, 2000);

    /**
     * 页面需要初始渲染的效果
     */
    Http.get({
      url: config.api.couponOrderDetail, 
      data: {
        couponOrderId: options.quancode
      }
    }).then(res => {
      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")
      });
      util.barcode("barcode", options.quancode, 510, 100);
      util.qrcode("qrcode", options.quancode, 350, 350);
    })
      .catch(err => {
        wx.showToast({
          title: err.errMsg,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      })
  },
  phone: function (e) {
    let that = this;
    if (e.currentTarget.dataset.merchantlinkphone) {
      wx.makePhoneCall({
        phoneNumber: e.currentTarget.dataset.merchantlinkphone
      });
    }
  }
});