const _my = require("../../../__antmove/api/index.js")(my);
const wx = _my;
const {
  throttle
} = require("../../../utils/throttle/throttle");
const face = require("../../../utils/faceCheck/faceCheck");
const {
  updateVisitDeviceUser
} = require("../../../utils/api/api");
import QRCode from "../../../utils/util/weapp-qrcode";
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    machineInfo_FKJ: wx.getStorageSync("machineInfo_FKJ"),
    timeCount: 0,
    dateTime: "",
    //时间戳
    qrCode: "",
    //二维码
    timer: null
  },
  //切换用户
  back() {
    wx.showModal({
      title: "提示",
      content: `确定要切换用户吗?`,
      success(res) {
        if (res.confirm) {
          wx.removeStorageSync("machineInfo_FKJ");
          // wx.removeStorageSync('dateCode')
          wx.reLaunch({
            url: "/pages/temporary_prove_FKJ/index/index"
          });
        }
      }
    });
  },
  //查看头像
  preview() {
    wx.previewImage({
      current: this.data.machineInfo_FKJ.avatar,
      urls: [this.data.machineInfo_FKJ.avatar]
    });
  },
  updateTime() {
    this.setData({
      dateTime: new Date().getTime()
    });
    console.log(this.data.dateTime);
    this.creatQrCode();
  },
  creatQrCode() {
    // wx.showLoading({
    //     title: '加载中...',
    // })
    let that = this;
    new QRCode("myQrcode", {
      text: `HHFKJ?userId=${this.data.machineInfo_FKJ.userId}&timestamp=${this.data.dateTime}`,
      width: 141,
      //canvas 画布的宽
      height: 141,
      //canvas 画布的高
      padding: 0,
      // 生成二维码四周自动留边宽度，不传入默认为0
      correctLevel: QRCode.CorrectLevel.L,
      // 二维码可辨识度
      callback: res => {
        console.log(res);
        //工具回调数据
        // 接下来就可以直接调用微信小程序的api保存到本地或者将这张二维码直接画在海报上面去，看各自需求
        // wx.hideLoading()
        //将图片路劲放入data中，显示在wxml的image标签上
        that.setData({
          qrCode: res.path
        });
      }
    });
  },
  // 更新访客机用户
  updateVisitDeviceUser() {
    let {
      machineInfo_FKJ
    } = this.data;
    let data = {
      workPlace: app.data.workPlace,
      carNumber: app.data.carNumber,
      userId: machineInfo_FKJ.userId
    };
    updateVisitDeviceUser(data).then(res => {
      if (res.code === 200) {
        machineInfo_FKJ.workPlace = app.data.workPlace;
        machineInfo_FKJ.carNumber = app.data.carNumber;
        wx.setStorageSync("machineInfo_FKJ", machineInfo_FKJ);
        this.setData({
          machineInfo_FKJ
        });
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none"
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.setData({
      machineInfo_FKJ: wx.getStorageSync("machineInfo_FKJ")
    });
    if (app.data.workPlace) {
      my.alert({
        title: "提示",
        content: "恭喜认证成功，可刷码快速登记！"
      });
      this.updateVisitDeviceUser();
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideHomeButton();
    this.updateTime();
    let _this = this;
    this.setData({
      timer: setInterval(function () {
        _this.updateTime();
      }, 1000 * 30)
    });
    // setInterval(function () {
    //     this.updateTime()
    // }.bind(this), 1000 * 30);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.timer);
    this.setData({
      timer: null
    });
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.timer);
    this.setData({
      timer: null
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  antmoveAction: function () {
    //执行时动态赋值，请勿删除
  }
});