const wxCharts = require('../../../utils/wxcharts.js');
const util = require('../../../utils/util.js');
const api = require('../../../utils/api.js');
const app = getApp()
let ringChart = null;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    totalInfo: '',
    startDate: '',
    endDate: '',
    listCollege: '',
    listDept: '',
    userArray: [
      [],
      []
    ],
    value: [0, 0],
    weiguiList: []
  },

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

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

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

  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.getStatisticsInfo();
          }
        }
      } else {
        let userArray = that.data.userArray;
        userArray[1] = arrayDept
        that.setData({
          userArray: userArray,
          listDept: '',
          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.getStatisticsInfo();
    }
  },

  getChart: function(e) {
    const that = this;
    ringChart = new wxCharts({
      animation: true,
      canvasId: 'ringCanvas',
      type: 'ring',
      extra: {
        ringWidth: 10,
        pie: {
          offsetAngle: -40
        }
      },
      title: {
        name: '归勤率',
        color: '#333',
        fontSize: 15
      },
      subtitle: {
        name: that.data.totalInfo.attendance * 100 + '%',
        color: '#6282f4',
        fontSize: 18
      },
      series: [{
        name: '归勤',
        data: that.data.totalInfo.attendance * 100,
        stroke: false,
        color: '#6282f4'
      }, {
        name: '缺勤',
        data: 100 - that.data.totalInfo.attendance * 100,
        stroke: false,
        color: '#ed5c68'
      }],
      disablePieStroke: true,
      width: 160,
      height: 160,
      dataLabel: false,
      legend: false,
      background: '#fff',
      padding: 0
    });
    ringChart.addEventListener('renderComplete', () => {
      console.log('renderComplete');
    });
    setTimeout(() => {
      ringChart.stopAnimation();
    }, 500);
  },

  touchHandler: function(e) {
    console.log(ringChart.getCurrentDataIndex(e));
  },

  getStatisticsInfo: function(e) {
    const that = this;
    let deptId;
    if (that.data.listDept != '') {
      deptId = that.data.listDept[that.data.value[1]].deptId
    } else {
      deptId = that.data.listCollege[that.data.value[0]].deptId
    }
    util.httpRequest(api.getStatisticsInfo, {
      start: that.data.startDate,
      end: that.data.endDate,
      deptId: deptId
    }, 'post').then(res => {
      that.setData({
        totalInfo: res.data.data
      })
      that.getChart();
    })
  },

  getUnInInfo: function(e) {
    const that = this;
    util.httpRequest(api.getUnInfo, {
      start: that.data.startDate,
      end: that.data.endDate,
      page: 1,
      rows: 20
    }, 'post').then(res => {
      weiguiList: res.data.data.data
    })
  },

  getOutAndInInfo: function(e) {
    const that = this;
    util.httpRequest(api.getOutAndInInfo, {
      startDate: that.data.startDate,
      endDate: that.data.endDate,
      page: 1,
      rows: 20
    }, 'post').then(res => {
      that.setData({

      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const that = this;
    that.getCurrentDate();
    that.data.userInfo = wx.getStorageSync('user');
    that.getCollegeInfo();
    that.getUnInInfo();
    that.getOutAndInInfo();
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})