<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-select v-model="table.getdataListParm.parammaps.pastureName" placeholder="牧场" class="filter-item" style="width: 120px;">
        <el-option v-for="item in findAllPasture" :key="item.id" :label="item.name" :value="item.name" />
      </el-select>
      <el-date-picker
        ref="BXinputDatetime"
        v-model="table.getdataListParm.parammaps.BXinputDatetime"
        class="inputDatetime"
        type="datetimerange"
        style="width: 270px;top:-3px;"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd"
        range-separator="至"
        start-placeholder="报修开始时间"
        end-placeholder="报修结束时间"
      />
      <el-date-picker
        ref="JDinputDatetime"
        v-model="table.getdataListParm.parammaps.JDinputDatetime"
        class="inputDatetime"
        type="datetimerange"
        style="width: 270px;top:-3px;"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd"
        range-separator="至"
        start-placeholder="接单开始时间"
        end-placeholder="接单结束时间"
      />
      <el-date-picker
        ref="useSHinputDatetime"
        v-model="table.getdataListParm.parammaps.useSHinputDatetime"
        class="inputDatetime"
        type="datetimerange"
        style="width: 270px;top:-3px;"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd"
        range-separator="至"
        start-placeholder="维修开始时间"
        end-placeholder="维修结束时间"
      />
      <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" @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="90px" align="center" prop="pastureName" />
      <el-table-column label="单号" min-width="90px" align="center" prop="repairCode" />
      <el-table-column label="设备类别" min-width="90px" align="center" prop="eqClassName" />
      <el-table-column label="设备名称" min-width="90px" align="center" prop="eqName" />
      <el-table-column label="设备内部编号" min-width="90px" align="center" prop="eqCode" />
      <el-table-column label="报修日期" min-width="70px" align="center" prop="BXTime" />
      <el-table-column label="接单间隔" min-width="90px" sortable align="center" prop="BxAndJd" />
      <el-table-column label="接单日期" min-width="90px" sortable align="center" prop="JDTime" />
      <el-table-column label="维修间隔" min-width="70px" sortable align="center" prop="JdAndWx" />
      <el-table-column label="维修日期" min-width="90px" sortable align="center" prop="WXTime" />
      <el-table-column label="总维修时长" min-width="90px" sortable align="center" prop="WxAndJS" />
      <el-table-column label="完成维修日期" min-width="70px" sortable align="center" prop="JSTime" />
      <el-table-column label="确认间隔" min-width="70px" sortable align="center" prop="JSAnduseSH" />
      <el-table-column label="使用人确认日期" min-width="70px" sortable align="center" prop="useSHTime" />
    </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' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { GetDataByName, GetDataByNames } from '@/api/common'
import { parseTime, json2excel } from '@/utils/index.js'
import Cookies from 'js-cookie'
export default {
  name: 'MaintenanceCycle',
  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: 'getAlleqWX',
          page: 1, offset: 1, pagecount: 10, returntype: 'Map',
          parammaps: {
            BXinputDatetime: '', SGTimeStart: '', SGTimeEnd: '',
            JDinputDatetime: '', JDTimeStart: '', JDTimeEnd: '',
            useSHinputDatetime: '', useSHTimeStart: '', useSHTimeEnd: '',
            pastureName: Cookies.get('pasturename')
          }
        }
      },
      downLoadParm: {},
      downLoadList: [],
      isPercentage: false,
      percentage: 1,
      findAllPasture: [],
      requestParams: [
        { name: 'findAllPasture', offset: 0, pagecount: 0, returntype: 'Map', parammaps: { 'id': Cookies.get('pastureid') }}
      ]
    }
  },

  created() {
    this.get_select_list()
    this.getList()
  },

  methods: {
    get_select_list() {
      GetDataByNames(this.requestParams).then(response => {
        if (response.data.list !== null) {
          this.findAllPasture = response.data.findAllPasture.list
        }
      })
    },
    getList() {
      this.table.listLoading = true
      GetDataByName(this.table.getdataListParm).then(response => {
        console.log('table数据', response.data.list)
        if (response.data.list !== null) {
          for (let i = 0; i < response.data.list.length; i++) {
            if (response.data.list[i].BxAndJd !== undefined) {
              this.$set(response.data.list[i], 'BxAndJd', parseFloat(response.data.list[i].BxAndJd))
            }
            if (response.data.list[i].JdAndWx !== undefined) {
              this.$set(response.data.list[i], 'JdAndWx', parseFloat(response.data.list[i].JdAndWx))
            }
            if (response.data.list[i].WxAndJS !== undefined) {
              this.$set(response.data.list[i], 'WxAndJS', parseFloat(response.data.list[i].WxAndJS))
            }
            if (response.data.list[i].JSAnduseSH !== undefined) {
              this.$set(response.data.list[i], 'JSAnduseSH', parseFloat(response.data.list[i].JSAnduseSH))
            }
          }
          this.table.list = response.data.list
          this.table.pageNum = response.data.pageNum
          this.table.pageSize = response.data.pageSize
          this.table.total = response.data.total
        } else {
          this.table.list = []
          this.table.total = 0
        }

        setTimeout(() => {
          this.table.listLoading = false
        }, 100)
      })
    },
    form_search() {
      this.table.listLoading = true
      this.table.getdataListParm.offset = 1
      if (this.table.getdataListParm.parammaps.BXinputDatetime !== [] && this.table.getdataListParm.parammaps.BXinputDatetime !== '' && this.table.getdataListParm.parammaps.BXinputDatetime !== null) {
        if (this.table.getdataListParm.parammaps.BXinputDatetime.length !== 0) {
          this.table.getdataListParm.parammaps.BXTimeStart = parseTime(this.table.getdataListParm.parammaps.BXinputDatetime[0], '{y}-{m}-{d}')
          this.table.getdataListParm.parammaps.BXTimeEnd = parseTime(this.table.getdataListParm.parammaps.BXinputDatetime[1], '{y}-{m}-{d}')
        }
      } else {
        this.table.getdataListParm.parammaps.BXinputDatetime = []
        this.table.getdataListParm.parammaps.BXTimeStart = ''
        this.table.getdataListParm.parammaps.BXTimeEnd = ''
      }
      if (this.table.getdataListParm.parammaps.JDinputDatetime !== [] && this.table.getdataListParm.parammaps.JDinputDatetime !== '' && this.table.getdataListParm.parammaps.JDinputDatetime !== null) {
        if (this.table.getdataListParm.parammaps.JDinputDatetime.length !== 0) {
          this.table.getdataListParm.parammaps.JDTimeStart = parseTime(this.table.getdataListParm.parammaps.JDinputDatetime[0], '{y}-{m}-{d}')
          this.table.getdataListParm.parammaps.JDTimeEnd = parseTime(this.table.getdataListParm.parammaps.JDinputDatetime[1], '{y}-{m}-{d}')
        }
      } else {
        this.table.getdataListParm.parammaps.JDinputDatetime = []
        this.table.getdataListParm.parammaps.JDTimeStart = ''
        this.table.getdataListParm.parammaps.JDTimeEnd = ''
      }
      if (this.table.getdataListParm.parammaps.useSHinputDatetime !== [] && this.table.getdataListParm.parammaps.useSHinputDatetime !== '' && this.table.getdataListParm.parammaps.useSHinputDatetime !== null) {
        if (this.table.getdataListParm.parammaps.useSHinputDatetime.length !== 0) {
          this.table.getdataListParm.parammaps.useSHTimeStart = parseTime(this.table.getdataListParm.parammaps.useSHinputDatetime[0], '{y}-{m}-{d}')
          this.table.getdataListParm.parammaps.useSHTimeEnd = parseTime(this.table.getdataListParm.parammaps.useSHinputDatetime[1], '{y}-{m}-{d}')
        }
      } else {
        this.table.getdataListParm.parammaps.useSHinputDatetime = []
        this.table.getdataListParm.parammaps.useSHTimeStart = ''
        this.table.getdataListParm.parammaps.useSHTimeEnd = ''
      }
      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.form_search()
      this.downLoadParm.getdataListParm = {}
      this.downLoadParm.getdataListParm.name = 'getAlleqWX'
      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: ['pastureName', 'repairCode', 'eqClassName', 'eqName', 'eqCode', 'BXTime', 'BxAndJd', 'JDTime', 'JdAndWx', 'WXTime', 'WxAndJS', 'JSTime', 'JSAnduseSH', 'useSHTime'],
              tableDatas: this.downLoadParm.list,
              sheetName: 'Sheet'
            }
          ]
          json2excel(excelDatas, '维修周期效率统计', true, 'xlsx')
        })
      })
    }
  }
}
</script>
