const navigationBarHeight = (getApp().statusBarHeight + 44) + 'px';
var config = require("../../config/config.js");
var app = getApp();
const Http = require("../../utils/HttpBasics");
const QR = require("../../utils/memberqrcode.js");
const imgurl = require("../../utils/imgurl");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navigationBarHeight,
    myspeacialUrl: imgurl.myspeacial.url,
    teliconUrl: imgurl.telicon.url,
    wmhome: imgurl.wmhome.url,
    memberId: "000000",
    goHomeUrl: "",
    showCode: true,
    showTimeLine: true,
    expiredSeconds: 0
  },
  phone: function (e) {
    console.log(e)
    let that = this;
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.merchantlinkphone
    });
  },
  //是否授权手机号
  ifPhoneInfo() {
    let that = this;
    Http.get({
      url: config.api.checkPhoneStatus,
      data: {}
    }).then(res => {
      return
    }).catch(err => {
      wx.navigateTo({
        url: `/pages/getphoneInfo/index?mineFlag=index`,
      })
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    that.ifPhoneInfo()
    that.setData({
      goHomeUrl: app.globalData.goHomeUrl,
    })
    console.log("渲染开始")
    that.getDiscountInfo()
  },

  refreshCode() {
    this.setData({
      showCode: true
    })
    this.getDiscountInfo()
  },

  getDiscountInfo() {
    let that = this;
    Http.get({
      url: config.api.getDiscountInfo,
    })
      .then(res => {
        that.setData({
          level: res.data.level ? res.data.level : ''
        })
        that.qrcode(res.data.dynamicId);
        that.setData({
          memberId: res.data.id,
          expiredSeconds: res.data.expiredSeconds,
        })
        let discountMerchantList = [];
        res.data.levelMerchantList.map(file => {
          if (file.discount != 100) {
            discountMerchantList.push(file);
          }
        })
        that.setData({
          discountMerchantList: discountMerchantList,
        })
        if (that.data.expiredSeconds * 1) {
          const timer = setInterval(() => {
            const expiredSeconds = that.data.expiredSeconds
            if (expiredSeconds) {
              that.setData({
                expiredSeconds: expiredSeconds - 1,
              })
            } else {
              clearInterval(timer)
              that.setData({
                showCode: false,
              })
            }

          }, 1000);
        }
      }).catch(err => {
        console.log(err, 'err');
        wx.showToast({
          title: err.message,
        })
      })
  },

  goback: function () {
    let this_ = this
    wx.switchTab({
      url: this_.data.goHomeUrl,
    })
  },
  /**
   * 二维码
   */
  qrcode(memberId) {
    let that = this;
    var size = that.setCanvasSize();
    let url = JSON.stringify({
      END: "C",
      TYPE: "memberCode",
      ID: memberId,
    });
    that.createQrCode(url, "mycanvas2", size.w, size.h);
  },
  createQrCode: function (url, canvasId, cavW, cavH) {
    //调用插件中的draw方法，绘制二维码图片
    let that = this;
    QR.api.draw(url, canvasId, cavW, cavH, function (res) {
      that.setData({
        tempFilePath: res
      })
    });
  },
  //适配不同屏幕大小的canvas
  setCanvasSize: function () {
    var size = {};
    try {
      var res = wx.getSystemInfoSync();
      var scale = 750 / 400;
      //不同屏幕下canvas的适配比例；设计稿是750宽
      var width = res.windowWidth / scale;
      var height = width;
      //canvas画布为正方形
      size.w = width;
      size.h = height;
    } catch (e) {
      // Do something when catch error
      console.log("获取设备信息失败" + e);
    }
    return size;
  },

})