let util = require("../../utils/util");
let Http = require("../../utils/HttpBasics");
let config = require("../../config/config.js");
Page({
  data: {
    code: "",
    //存储计时器
    setInter: ""
  },
  onLoad: function(options) {
    let that = this;
    setTimeout(function () {
      wx.setScreenBrightness({
        value: 0.7,
      })
    }, 200),
    util.barcode("barcode", options.quancode, 500, 100);
    util.qrcode("qrcode", options.quancode, 350, 350);
    that.setData({
      sight: options.sight,
      code: options.quancode,
      title: options.title,
      subtitle: options.subtitle,
      remark: options.remark,
      couponorderstatus: options.couponorderstatus
    });

    console.log(options.couponorderstatus);
    console.log("我是状态");
    /**
     * 如果没有核销
     */
    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) {
            /**
             * 动态改变上一级页面的核销状态
             */
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2]; //上一个页面
            //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
            prevPage.setData({
              mystatus: res.data.CouponOrderStatus
            });
            
          }
        });
      }
    }, 2000);

    if (that.data.couponorderstatus == 1) {
      /**
       * 如果已经核销
       * 不需要循环
       */
      Http.get({
        url: config.api.getStatus,
        data: {
          couponOrderId: options.quancode
        }
      }).then(res => {
        console.log(res);
        that.setData({
          couponorderstatus: res.data.CouponOrderStatus
        });
      });
    }
    /**
     * couponorderstatus
     * 0 没有核销
     * 1 已经核销成功
     */
  },
  onUnload: function() {
    let that = this;
    clearInterval(that.data.setInter);
    wx.setScreenBrightness({
      value: that.data.sight,
    })
  }
});
