<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="getdataListParm.parammaps.pastureName" style="width: 140px;" placeholder="牧场" class="filter-item">
        <el-option v-for="item in findAllPasture" :key="item.id" :label="item.name" :value="item.name" />
      </el-select>
      <el-date-picker v-model="getdataListParm.parammaps.checkDate" type="month" style="width: 250px;top:-3px;" placeholder="选择月份" format="yyyy-MM" value-format="yyyy-MM" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="form_search">搜索</el-button>
      <el-button class="filter-item" style="position:absolute;right:10px" type="success" icon="el-icon-edit" @click="handleDownload">导出</el-button>
      <el-table
        :key="tableKey"
        v-loading="listLoading"
        element-loading-text="给我一点时间"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
        :row-style="rowStyle"
        :cell-style="cellStyle"
        class="elTable table-fixed"
        @sort-change="tableSort"
      >
        <el-table-column label="序号" align="center" type="index" width="50px">
          <template slot-scope="scope">
            <span>{{ scope.$index + (pageNum-1) * pageSize + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="月份" sortable prop="checkDate" align="center" min-width="150px" />
        <el-table-column label="牧场" prop="pastureName" min-width="130px" align="center" />
        <el-table-column label="盘盈数量" min-width="130px" align="center" sortable prop="moreAount" />
        <el-table-column label="盘盈金额" align="center" min-width="150px" sortable prop="moreSum" />
        <el-table-column label="盘亏数量" min-width="130px" align="center" sortable prop="lessAount" />
        <el-table-column label="盘亏金额" min-width="130px" align="center" sortable prop="lessSum" />
        <el-table-column label="盈亏净额" min-width="130px" align="center" sortable prop="sums" />
        <el-table-column label="盘点状态" min-width="130px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.sumStatue }}</span><br>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="getdataListParm.offset" :limit.sync="getdataListParm.pagecount" @pagination="getList" />
    </div>
  </div>
</template>
<script>
// 引入
import { GetDataByName, GetDataByNames, GetAccount } from '@/api/common'
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves' // waves directive
import Cookies from 'js-cookie'
import { parseTime, json2excel, sortChange } from '@/utils/index.js'
import { } from '@/utils/index.js'
export default {
  name: 'InventorySpareParts',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      rowStyle: { maxHeight: 50 + 'px', height: 45 + 'px' },
      cellStyle: { padding: 0 + 'px' },
      rules: {},
      findAllPasture: [],
      requestParams: [
        { name: 'findAllPasture', offset: 0, pagecount: 0, returntype: 'Map', parammaps: { 'id': Cookies.get('pastureid') }}
      ],
      getdataListParm: {
        name: 'getcheckpartReportList',
        page: 1,
        offset: 1,
        pagecount: 10,
        returntype: 'Map',
        parammaps: {
          pastureName: Cookies.get('pasturename'),
          checkDate: ''
        }
      },
      listLoading: false,
      total: 0,
      tableKey: 0,
      list: [],
      downLoadParm: {},
      downLoadList: [],
      isPercentage: false,
      percentage: 1
    }
  },
  created() {
    this.get_select_list()
    this.getList()
  },
  methods: {
    tableSort(column) {
      sortChange(column, this.list)
    },
    get_select_list() {
      GetDataByNames(this.requestParams).then(response => {
        this.findAllPasture = response.data.findAllPasture.list
      })
    },
    getList() {
      this.listLoading = true
      GetDataByName(this.getdataListParm).then(response => {
        console.log('table数据', response.data.list)
        this.list = response.data.list
        this.pageNum = response.data.pageNum
        this.pageSize = response.data.pageSize
        if (response.data.total) {
          this.total = response.data.total
        }
        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 100)
      })
    },
    form_search() {
      console.log('点击了table搜索')
      if (this.getdataListParm.parammaps.checkDate == null) {
        this.getdataListParm.parammaps.checkDate = ''
      }
      this.getdataListParm.offset = 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.name = 'getcheckpartReportList'
      this.downLoadParm.returntype = 'Map'
      this.downLoadParm.parammaps = this.getdataListParm.parammaps
      GetAccount(this.downLoadParm).then(response => {
        if (response.data.list !== '') {
          this.percentage = 99
          setTimeout(() => {
            this.isPercentage = false
          }, 2000)
        }
        this.$nextTick(() => {
          this.downLoadList = response.data.list
          console.log(this.downLoadList)
          const ExcelDatas = [
            {
              tHeader: ['月份', '牧场', '盘盈数量', '盘亏数量', '盘亏金额', '盈亏净额', '盘点状态'],
              filterVal: ['checkDate', 'pastureName', 'moreAount', 'lessAount', 'lessSum', 'sums', 'sumStatue'],
              tableDatas: this.downLoadList,
              sheetName: '备件盘点报表'
            }
          ]
          json2excel(ExcelDatas, '备件盘点报表', true, 'xlsx')
        })
      })
    }
  }
}
</script>
