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

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    apiTotal: '',
    userInfo: '',
    isregiste: true,
    arrayCollege: [],
    listCollege: '',
    indexCollege: 0,
    arrayDept: [],
    listDept: '',
    indexDept: 0,
    arrayClass: [],
    indexClass: 0,
    listClass: '',
    registerCount: '',
    unRegisterCount: '',
    list: []
  },

  checkTab: function(e) {
    const that = this;
    that.setData({
      isregiste: !that.data.isregiste
    })
    if (that.data.isregiste) {
      that.getRegisterInfo(0)
    } else {
      that.getRegisterInfo(1)
    }
  },

  pickerChangeCollege: function(e) {
    this.setData({
      indexCollege: e.detail.value
    })
    this.getDeptInfo();
  },

  pickerChangeDept: function(e) {
    this.setData({
      indexDept: e.detail.value
    })
    this.getDeptInfo();
  },

  pickerChangeClass: function(e) {
    this.setData({
      indexClass: e.detail.value
    })
    this.getDeptInfo();
  },

  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) {
            that.setData({
              arrayCollege: arrayCollege,
              listCollege: res.data
            })
            that.getDeptInfo();
          }
        }
      } else {
        that.setData({
          arrayCollege: arrayCollege,
          arrayDept: [],
          listDept: '',
          list: ''
        })
      }
    })
  },

  getDeptInfo: function(e) {
    const that = this;
    util.httpRequest(api.getDeptInfo, {
      companyId: that.data.userInfo.companyId,
      parentId: that.data.listCollege[that.data.indexCollege].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) {
            that.setData({
              arrayDept: arrayDept,
              listDept: res.data
            })
            if (that.data.isregiste) {
              that.getRegisterInfo(0)
            } else {
              that.getRegisterInfo(1)
            }
          }
        }
      } else {
        that.setData({
          arrayClass: [],
          arrayDept: [],
          listDept: '',
          registerCount: 0,
          unRegisterCount: 0,
          list: ''
        })
      }
    })
  },

  getRegisterInfo: function(status = 0) {
    const that = this;
    util.httpRequest(api.getRegisterInfo, {
      deptId: that.data.listDept[that.data.indexDept].deptId,
      register: status
    }, 'post').then(res => {
      that.setData({
        registerCount: res.data.data.registerCount,
        unRegisterCount: res.data.data.unEegisterCount,
        list: res.data.data.data
      })
    })
  },

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

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})