const util = require('../../../utils/util.js');
const api = require('../../../utils/api.js');

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    apiTotal: '',
    isregiste: true,
    approvalNum: 0,
    approvedNum: 0,
    list: []
  },

  checkTab: function(e) {
    const that = this;
    that.setData({
      isregiste: !that.data.isregiste
    })
    if (that.data.isregiste) {
      that.getVisitorCheckInfo(2);
    } else {
      that.getVisitorCheckInfo(4);
    }
  },

  todetail: function(e) {
    let id = e.currentTarget.dataset.id;
    let type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '../visitorcheckdetail/visitorcheckdetail?id=' + id + '&type=' + this.data.isregiste,
    })
  },

  getVisitorCheckInfo: function(status) {
    const that = this;
    util.httpRequest(api.getVisitorCheckInfo, {
      status: status,
      rows: 20,
      page: 1
    }, 'post').then(res => {
      for (let i = 0; i < res.data.data.data.length; i++) {
        res.data.data.data[i].createDate = that.getLocalTime(res.data.data.data[i].createDate)
      }
      that.setData({
        list: res.data.data.data,
        approvalNum: res.data.data.approval,
        approvedNum: res.data.data.approved
      })
    })
  },

  getLocalTime: function(nS) {
    let date = new Date(nS);
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    let hour = date.getHours();
    let minute = date.getMinutes();
    let second = date.getSeconds();
    month = month < 10 ? "0" + month : month;
    day = day < 10 ? "0" + day : day;
    hour = hour < 10 ? "0" + hour : hour;
    minute = minute < 10 ? "0" + minute : minute;
    second = second < 10 ? "0" + second : second;
    date = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
    return date;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    const that = this;
    that.setData({
      apiTotal: api.total.substring(0, api.total.length - 4)
    })
    console.log(that.data.isregiste)
    if (that.data.isregiste) {
      that.getVisitorCheckInfo(2);
    } else {
      that.getVisitorCheckInfo(4);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  }
})