const util = require("../../../utils/util");
const config = require("../../../config/config.js");
const Http = require("../../../utils/HttpBasics");

//券详情页面
Page({
  data: {
    code: "",
    data: {},
    createDate: "",
    expiredTime: "",
    updateDate: "",
    //存储计时器
    setInter: ""
  },
  onUnload: function() {
    let that = this;
    clearInterval(that.data.setInter);
    wx.setScreenBrightness({
      value: that.data.sight,
    })
  },
  onLoad: function(options) {
    let that = this;
    console.log(options.sight);
    setTimeout(function() {
        wx.setScreenBrightness({
          value: 0.7,
        })
      }, 200),
      that.setData({
        sight: options.sight,
        code: options.quancode,
        couponorderstatus: options.couponorderstatus
      });


    //获得优惠券的详情
    that.data.setInter = setInterval(function() {
      if (
        options.quancode != null &&
        options.quancode != "" &&
        options.quancode != undefined &&
        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) {
            /**
             * 动态改变上一级页面的核销状态
             */
            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")
          });
        });
      }
    }, 2000);

    /**
     * 页面需要初始渲染的效果
     */
    Http.get({
      url: config.api.couponOrderDetail,
      data: {
        couponOrderId: options.quancode
      }
    }).then(res => {
      console.log(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);
    })
  },
  phone: function() {
    let that = this;
    console.log(that.data);
    if (that.data.data.merchantLinkPhone) {
      wx.makePhoneCall({
        phoneNumber: that.data.data.merchantLinkPhone
      });
    }
  }
});