<template>
  <div class="app-container">
    <div v-if="isPercentage" class="percentage" style="width: 210px;height: 90px;background: #fff;position: fixed;bottom: 0;left: 0;z-index: 9999999999999;">
      <h4 style="padding-left: 10px;line-height: 0;">导出进度：</h4>
      <el-progress style="padding-left: 10px;" :text-inside="true" :stroke-width="26" :percentage="percentage" />
    </div>
    <div class="filter-container">
      <el-date-picker
        ref="inputDatetime"
        v-model="table.getdataListParm.parammaps.inputDatetime"
        class="inputDatetime"
        type="datetimerange"
        style="width: 250px;top:-3px;"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :clearable="false"
      />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="form_search">搜索</el-button>
      <el-button class="filter-item" type="success" icon="el-icon-upload2" style="position:absolute ;right:10px" @click="handleDownload">导出</el-button>
    </div>
    <el-table
      :key="table.tableKey"
      v-loading="table.listLoading"
      element-loading-text="给我一点时间"
      :data="table.list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      :row-style="rowStyle"
      :cell-style="cellStyle"
      class="elTable"
    >
      <el-table-column label="序号" align="center" type="index" width="50px">
        <template slot-scope="scope">
          <span>{{ scope.$index + (table.pageNum-1) * table.pageSize + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="牧场" min-width="100px" align="center" prop="totaluk" />
      <el-table-column label="单号" min-width="100px" align="center" prop="doneuk" />
      <el-table-column label="设备类别" min-width="100px" align="center" prop="doneuk" />
      <el-table-column label="设备名称" min-width="100px" align="center" prop="donerateuk" />
      <el-table-column label="设备内部编号" min-width="100px" align="center" prop="totalmt" />
      <el-table-column label="报修日期" min-width="100px" align="center" prop="donemt" />
      <el-table-column label="天数" min-width="100px" align="center" prop="donemt" />
      <el-table-column label="接单日期" min-width="100px" align="center" prop="donemt" />
      <el-table-column label="天数" min-width="100px" align="center" prop="donemt" />
      <el-table-column label="维修日期" min-width="100px" align="center" prop="donemt" />
      <el-table-column label="天数" min-width="100px" align="center" prop="donemt" />
      <el-table-column label="结束日期" min-width="100px" align="center" prop="donemt" />
    </el-table>
    <pagination v-show="table.total>0" :total="table.total" :page.sync="table.getdataListParm.offset" :limit.sync="table.getdataListParm.pagecount" @pagination="getList" />
  </div>

</template>

<script>
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { GetDataByName } from '@/api/common'
import { parseTime, json2excel } from '@/utils/index.js'
export default {
  name: 'RepairRateStatistics',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      rowStyle: { maxHeight: 50 + 'px', height: 45 + 'px' },
      cellStyle: { padding: 0 + 'px' },
      table: {
        tableKey: 0, list: [], total: 0, listLoading: true,
        getdataListParm: {
          name: 'UkdoneRatePasture', page: 1, offset: 1, pagecount: 10, returntype: 'Map',
          parammaps: { startTime: '', stopTime: '', inputDatetime: [] }
        }
      },
      downLoadParm: {},
      downLoadList: [],
      isPercentage: false,
      percentage: 1
    }
  },

  created() {
    this.getTimeFn()
    this.getDownClassList()
  },

  methods: {

    getTimeFn() {
      const that = this
      const start = new Date()
      const end = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
      that.table.getdataListParm.parammaps.inputDatetime[0] = parseTime(start, '{y}-{m}-{d}')
      that.table.getdataListParm.parammaps.inputDatetime[1] = parseTime(end, '{y}-{m}-{d}')
      that.table.getdataListParm.parammaps.startTime = that.table.getdataListParm.parammaps.inputDatetime[0]
      that.table.getdataListParm.parammaps.stopTime = that.table.getdataListParm.parammaps.inputDatetime[1]
      that.getList()
    },
    getList() {
      this.table.listLoading = true
      GetDataByName(this.table.getdataListParm).then(response => {
        console.log('table数据', response.data.list)
        this.table.list = response.data.list
        this.table.pageNum = response.data.pageNum
        this.table.pageSize = response.data.pageSize
        this.table.total = response.data.total
        setTimeout(() => {
          this.table.listLoading = false
        }, 100)
      })
    },
    form_search() {
      this.table.listLoading = true
      this.table.getdataListParm.offset = 1
      this.table.getdataListParm.parammaps.startTime = this.table.getdataListParm.parammaps.inputDatetime[0]
      this.table.getdataListParm.parammaps.stopTime = this.table.getdataListParm.parammaps.inputDatetime[1]
      this.getList()
    },
    handleDownload() {
      this.$alert('维修周期效率统计报表-设备正在导出中，请勿刷新或离开本页面，若导出时间过长，建议缩小导出数据范围重新导出', {})
      this.isPercentage = true
      this.percentage = 1
      var timer = setInterval(() => {
        this.percentage += 5
        if (this.percentage > 95) {
          this.percentage = 99
          clearInterval(timer)
        }
        this.percentage = this.percentage
      }, 1000)
      this.downLoadParm.getdataListParm = {}
      this.downLoadParm.getdataListParm.name = 'UkdoneRateEqclass'
      this.downLoadParm.getdataListParm.pagecount = 0
      this.downLoadParm.getdataListParm.parammaps = this.table.getdataListParm.parammaps
      GetDataByName(this.downLoadParm.getdataListParm).then(response => {
        if (response.data.list !== '') {
          this.percentage = 99
          setTimeout(() => {
            this.isPercentage = false
          }, 2000)
        }
        this.$nextTick(() => {
          this.downLoadParm.list = response.data.list
          var excelDatas = [
            {
              tHeader: ['牧场', '单号', '设备类别', '设备名称', '设备内部编号', '报修日期', '天数', '接单日期', '天数', '维修日期', '天数', '结束日期'],
              filterVal: ['', '', '', '', '', '', '', '', '', '', '', ''],
              tableDatas: this.downLoadParm.list,
              sheetName: 'Sheet'
            }
          ]
          json2excel(excelDatas, '维修周期效率统计', true, 'xlsx')
        })
      })
    }
  }
}
</script>
