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

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    apiTotal: '',
    listCollege: '',
    listDept: '',
    listUser: '',
    userArray: [
      [],
      [],
      []
    ],
    value: [0, 0, 0],
    deviceIndex: 2,
    deviceArray: [],
    deviceList: '',
    startDate: '',
    endDate: '',
    list: []
  },

  getCollegeInfo: function(e) {
    const that = this;
    util.httpRequest(api.getDeptInfo, {
      companyId: that.data.userInfo.companyId,
      level: 1
    }, 'post').then(res => {
      let arrayCollege = [];
      if (res.data.length >= 1) {
        for (let i = 0; i < res.data.length; i++) {
          arrayCollege.push(res.data[i].deptName)
          if (i == res.data.length - 1) {
            let userArray = that.data.userArray;
            userArray[0] = arrayCollege;
            that.setData({
              userArray: userArray,
              listCollege: res.data
            })
            that.getDeptInfo();
          }
        }
      }
    })
  },

  getDeptInfo: function(e) {
    const that = this;
    util.httpRequest(api.getDeptInfo, {
      companyId: that.data.userInfo.companyId,
      parentId: that.data.listCollege[that.data.value[0]].deptId
    }, 'post').then(res => {
      let arrayDept = [];
      if (res.data.length >= 1) {
        for (let i = 0; i < res.data.length; i++) {
          arrayDept.push(res.data[i].deptName)
          if (i == res.data.length - 1) {
            let userArray = that.data.userArray
            userArray[1] = arrayDept
            that.setData({
              userArray: userArray,
              listDept: res.data
            })
            that.getRegisterInfo()
          }
        }
      } else {
        let userArray = that.data.userArray;
        userArray[1] = arrayDept
        userArray[2] = []
        that.setData({
          userArray: userArray,
          listDept: '',
          list: ''
        })
      }
    })
  },

  getRegisterInfo: function(status = 0) {
    const that = this;
    util.httpRequest(api.getRegisterInfo, {
      deptId: that.data.listDept[that.data.value[1]].deptId,
      register: status
    }, 'post').then(res => {
      let arrayUser = [];
      if (res.data.data.data.length >= 1) {
        for (let i = 0; i < res.data.data.data.length; i++) {
          arrayUser.push(res.data.data.data[i].userName)
          if (i == res.data.data.data.length - 1) {
            let userArray = that.data.userArray
            userArray[2] = arrayUser
            that.setData({
              userArray: userArray,
              listUser: res.data.data.data
            })
            that.getPassInfo();
          }
        }
      } else {
        let userArray = that.data.userArray
        userArray[2] = arrayUser
        that.setData({
          userArray: userArray,
          listUser: '',
          list: ''
        })
      }
    })
  },

  bindUserChange: function(e) {

  },

  columnChange: function(e) {
    const that = this;
    let value = that.data.value
    value[e.detail.column] = e.detail.value
    that.setData({
      value: value
    })
    if (e.detail.column == 0) {
      that.getDeptInfo()
    } else if (e.detail.column == 1) {
      that.getRegisterInfo()
    } else if (e.detail.column == 2) {
      that.getPassInfo();
    }
  },

  bindDeviceChange: function(e) {
    this.setData({
      deviceIndex: e.detail.value
    })
    this.getPassInfo();
  },

  bindStartDateChange: function(e) {
    this.setData({
      startDate: e.detail.value
    })
    this.getPassInfo();
  },

  bindendDateChange: function(e) {
    this.setData({
      endDate: e.detail.value
    })
    this.getPassInfo();
  },

  getDeviceInfo: function(e) {
    const that = this;
    util.httpRequest(api.getDeviceInfo, {

    }, 'post').then(res => {
      let deviceArray = [];
      for (let i = 0; i < res.data.length; i++) {
        deviceArray.push(res.data[i].name)
        if (i == res.data.length - 1) {
          that.setData({
            deviceArray: deviceArray,
            deviceList: res.data
          })
        }
      }
    })
  },

  getPassInfo: function(e) {
    const that = this;
    util.httpRequest(api.getPassInfo, {
      loginId: that.data.listUser[that.data.value[2]].loginid,
      start: that.data.startDate,
      end: that.data.endDate,
      driviceId: that.data.deviceList[that.data.deviceIndex].id,
      rows: 20,
      page: 1
    }, 'post').then(res => {
      for (let i = 0; i < res.data.data.length; i++) {
        res.data.data[i].openTime = that.getLocalTime(res.data.data[i].openTime)
      }
      that.setData({
        list: res.data.data
      })
    })
  },

  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;
  },

  getCurrentDate: function(e) {
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    month = month < 10 ? "0" + month : month;
    day = day < 10 ? "0" + day : day;
    date = year + '-' + month + '-' + day
    this.setData({
      startDate: date,
      endDate: date
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const that = this;
    that.setData({
      apiTotal: api.total.substring(0, api.total.length - 4)
    })
    that.getCurrentDate();
    that.data.userInfo = wx.getStorageSync('user');
    that.getCollegeInfo();
    that.getDeviceInfo();
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

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

  },

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

  },

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

  },

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

  }
})