<template>
  <app-container>
    <el-tabs v-model="activeName" type="card" @tab-click="tabClick">
      <el-tab-pane label="按日统计" name="byDay">
        <search-area @search="searchByDay">
          <search-item>
            <el-select v-model="listQuery.staffType" size="small" placeholder="人员类型" clearable>
              <el-option
                v-for="item in typeList"
                :key="item.value"
                :label="item.name"
                :value="item.value"/>
            </el-select>
          </search-item>
          <search-item>
            <el-date-picker
              v-model="timeRange"
              :clearable="false"
              :picker-options="pickerOptions"
              size="small"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"/>
          </search-item>
        </search-area>
      </el-tab-pane>
      <el-tab-pane label="按月统计" name="byMonth">
        <search-area @search="searchByMonth">
          <search-item>
            <el-select v-model="listQuery.staffType" size="small" placeholder="人员类型" clearable>
              <el-option
                v-for="item in typeList"
                :key="item.value"
                :label="item.name"
                :value="item.value"/>
            </el-select>
          </search-item>
          <search-item>
            <el-date-picker
              v-model="year"
              :clearable="false"
              size="small"
              type="year"
              value-format="yyyy"
              placeholder="请选择年份"/>
          </search-item>
        </search-area>
      </el-tab-pane>
    </el-tabs>
    <normal-table
      id="dataTable"
      :data="list"
      :head="tableOption.head"
      :pagination="false"
      :query="listQuery"
      :total="total"
      :columns="columns"
      :list-loading="listLoading"
      :options="tableOption.options"
      :tools-option="tableOption.toolsOption"
      @change="changePage">
      <template slot="btns">
        <el-button size="small" class="edit_btn" @click="batchExport">导出</el-button>
      </template>
    </normal-table>
  </app-container>
</template>

<script>
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
import { getDictByType } from '@/api/common'
import { getAttendancePercent } from '@/api/sanitation/statistics'
import { getToday, getLastWeek } from '@/utils/dateutils'
export default {
  name: 'StaffAssessment',
  data() {
    return {
      activeName: 'byDay', // 当前激活tab:byDay按日统计或byMonth按月统计
      year: '2021', // 按月统计面板中的查询年份
      timeRange: [getLastWeek('yyyy-MM-dd'), getToday('yyyy-MM-dd')], // 按日统计面板中的日期范围
      listQuery: {
        type: '', // 统计方式，day按天，month按月
        staffType: '', // 人员类型
        startTime: '', // 开始时间
        endTime: '' // 结束时间
      }, // 筛选条件
      columns: [
        { text: '时间', value: 'name', align: 'center' },
        // { text: '应出勤人数', value: 'total', align: 'center' },
        { text: '实际出勤人数', value: 'count', align: 'center' },
        // { text: '出勤率', value: 'percent', align: 'center' }
      ], // 显示列
      list: [], // 列表数据
      total: 0, // 数据总数
      listLoading: true, // 列表加载动画
      typeList: [], // 人员类别
      tableOption: {
        head: {
          show: true, // 是否需要标题栏，
          text: '统计结果' // 标题名称
        },
        options: {
          needIndex: false // 是否需要序号列
        },
        toolsOption: {
          selectColumns: false, // 是否需要筛选列
          refresh: false // 是否需要刷新按钮
        }
      }, // 表格属性
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }// 日期快捷选项
    }
  },
  created() {
    this.searchByDay()
    this.fetchPersonType()
  },
  methods: {
    // 切换面板
    tabClick(tab, event) {
      if (tab.name === 'byDay') {
        this.searchByDay()
      } else {
        this.searchByMonth()
      }
    },
    // 按月统计
    searchByMonth() {
      const startTime = this.year + '-01-01'
      const endTime = this.year + '-12-31'
      this.listQuery.type = 'month'
      this.listQuery.startTime = startTime
      this.listQuery.endTime = endTime
      this.fetchData()
      // this.listLoading = true
      // this.list = [
      //   { name: '1月', total: 15, real: 5, percent: '32.23%' },
      //   { name: '2月', total: 15, real: 5, percent: '32.23%' },
      //   { name: '3月', total: 15, real: 5, percent: '32.23%' },
      //   { name: '4月', total: 15, real: 5, percent: '32.23%' },
      //   { name: '5月', total: 15, real: 5, percent: '32.23%' },
      //   { name: '6月', total: 15, real: 5, percent: '32.23%' },
      //   { name: '7月', total: 15, real: 5, percent: '32.23%' },
      //   { name: '8月', total: 15, real: 5, percent: '32.23%' },
      //   { name: '9月', total: 15, real: 5, percent: '32.23%' },
      //   { name: '10月', total: 15, real: 5, percent: '32.23%' },
      //   { name: '11月', total: 15, real: 5, percent: '32.23%' },
      //   { name: '12月', total: 15, real: 5, percent: '32.23%' }
      // ]
      // this.listLoading = false
    },
    // 按日统计
    searchByDay() {
      this.listQuery.type = 'day'
      if (this.timeRange && this.timeRange.length > 0) {
        this.listQuery.startTime = this.timeRange[0]
        this.listQuery.endTime = this.timeRange[1]
      } else {
        this.listQuery.startTime = ''
        this.listQuery.endTime = ''
      }
      this.fetchData()
      // this.listLoading = true
      // this.list = [
      //   { name: '1.1', total: 15, real: 5, percent: '32.23%' },
      //   { name: '1.2', total: 15, real: 5, percent: '32.23%' },
      //   { name: '1.3', total: 15, real: 5, percent: '32.23%' },
      //   { name: '1.4', total: 15, real: 5, percent: '32.23%' },
      //   { name: '1.5', total: 15, real: 5, percent: '32.23%' },
      //   { name: '1.6', total: 15, real: 5, percent: '32.23%' },
      //   { name: '1.7', total: 15, real: 5, percent: '32.23%' },
      //   { name: '1.8', total: 15, real: 5, percent: '32.23%' },
      //   { name: '1.9', total: 15, real: 5, percent: '32.23%' },
      //   { name: '1.10', total: 15, real: 5, percent: '32.23%' },
      //   { name: '1.11', total: 15, real: 5, percent: '32.23%' },
      //   { name: '1.12', total: 15, real: 5, percent: '32.23%' },
      //   { name: '1.13', total: 15, real: 5, percent: '32.23%' },
      //   { name: '1.14', total: 15, real: 5, percent: '32.23%' },
      //   { name: '1.15', total: 15, real: 5, percent: '32.23%' }
      // ]
      // this.listLoading = false
    },
    fetchData() {
      this.listLoading = true
      getAttendancePercent(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.data.length
        this.listLoading = false
      })
    },
    // 批量导出
    batchExport() {
      let tableName = '出勤统计.xlsx'
      if (this.listQuery.type === 'day') {
        tableName = this.timeRange[0] + '至' + this.timeRange[1] + '按日出勤统计.xlsx'
      } else if (this.listQuery.type === 'month') {
        tableName = this.year + '年按月出勤统计.xlsx'
      }
      /* generate workbook object from table */
      var wb = XLSX.utils.table_to_book(document.querySelector('#dataTable'))
      /* get binary string as output */
      var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
      try {
        FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), tableName)
      } catch (e) { if (typeof console !== 'undefined') console.log(e, wbout) }
      return wbout
    },
    // 获取人员类别
    fetchPersonType(val) {
      getDictByType('staffType').then(response => {
        this.typeList = response.data
      })
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .edit_btns{
    .edit_btn{
      float:right;
      margin-left:5px;
    }
  }
</style>
