const app = getApp()
const baseUrl = app.globalData.baseUrl
// console.log("baseUrl", baseUrl)

var util = require('../../utils/util.js');
Page({
  data: {
    imgUrl: '',
    stuId: wx.getStorageSync('stuId'),
    name: wx.getStorageSync('name'),
    isHealth: 0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("onLoad")
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log("onReady")
  },

  onPullDownRefresh: function () {
    console.log("onPullDownRefresh");
  },

  onRefresh: function () {
    console.log("onRefresh");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("onShow");
    this.updateQrCode();
  },

  updateQrCode: function () {
    this.setData({
      stuId: wx.getStorageSync('stuId'),
      name: wx.getStorageSync('name')
    })
    if (wx.getStorageSync("state") == this.formatDate(new Date())) {
      this.setData({
        isHealth: 1
      })
    } else {
      this.setData({
        isHealth: 0
      })
    }
    wx.showLoading({
      title: '请稍候'
    })
    wx.request({
      url: `${baseUrl}/code/showCode`,
      data: {
        stuId: this.data.stuId,
        name: this.data.name,
        isHealth: this.data.isHealth
      },
      success: (data) => {
        console.log(data)
        this.setData({
          imgUrl: '../../img/greenCode.png'
        })
        wx.hideLoading()
        wx.showToast({
          title: '加载成功',
          icon: 'none',
          duration: 800
        })
        setInterval(() => {
          var time = util.formatTime(new Date())
          //为页面中time赋值
          this.setData({
            time: time
          })
        }, 1000);
      },
      fail: (error) => {
        console.log(error)
        wx.hideLoading()
        wx.showModal({
          title: '加载失败',
          showCancel: false
        })
      },
    })
  },

  formatDate: function (date) {
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    month = month.toString()
    month[1] ? month = month : month = '0' + month
    let day = date.getDate()
    day = day.toString()
    day[1] ? day = day : day = '0' + day
    return year + "-" + month + "-" + day
  },
})
