<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="SGinputDatetime"
        v-model="table.getdataListParm.parammaps.SGinputDatetime"
        class="inputDatetime"
        type="datetimerange"
        style="width: 270px;top:-3px;"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd"
        range-separator="至"
        start-placeholder="申购开始时间"
        end-placeholder="申购结束时间"
        :clearable="false"
      />
      <el-date-picker
        ref="PDinputDatetime"
        v-model="table.getdataListParm.parammaps.PDinputDatetime"
        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="CGinputDatetime"
        v-model="table.getdataListParm.parammaps.CGinputDatetime"
        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="RKinputDatetime"
        v-model="table.getdataListParm.parammaps.RKinputDatetime"
        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="partName" />
      <el-table-column label="备件编号" min-width="90px" align="center" prop="partCode" />
      <el-table-column label="申购单号" min-width="90px" align="center" prop="SGCode" />
      <el-table-column label="申购日期" min-width="90px" align="center" prop="SGdate" />
      <el-table-column label="天数" min-width="70px" align="center" prop="SGDays" />
      <el-table-column label="配单号" min-width="90px" align="center" prop="PDCode" />
      <el-table-column label="配单日期" min-width="90px" align="center" prop="PDdate" />
      <el-table-column label="天数" min-width="70px" align="center" prop="PDDays" />
      <el-table-column label="采购单号" min-width="90px" align="center" prop="CGCode" />
      <el-table-column label="采购日期" min-width="90px" align="center" prop="CGdate" />
      <el-table-column label="天数" min-width="70px" align="center" prop="CGDays" />
      <el-table-column label="入库单号" min-width="90px" align="center" prop="RKCode" />
      <el-table-column label="入库日期" min-width="90px" align="center" prop="RKdate" />
    </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: 'PurchaseAndwarehousing',
  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: 'getAllPartProcess',
          page: 1, offset: 1, pagecount: 10, returntype: 'Map',
          parammaps: {
            SGTimeStart: '', SGTimeEnd: '', SGinputDatetime: [],
            PDTimeStart: '', PDTimeEnd: '', PDinputDatetime: [],
            CGTimeStart: '', CGTimeEnd: '', CGinputDatetime: [],
            RKTimeStart: '', RKTimeEnd: '', RKinputDatetime: [],
            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.getTimeFn()
  },

  methods: {
    get_select_list() {
      GetDataByNames(this.requestParams).then(response => {
        if (response.data.list !== null) {
          this.findAllPasture = response.data.findAllPasture.list
        }
      })
    },
    getTimeFn() {
      const that = this
      const start = new Date()
      const end = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
      that.table.getdataListParm.parammaps.SGinputDatetime[0] = parseTime(start, '{y}-{m}-{d}')
      that.table.getdataListParm.parammaps.SGinputDatetime[1] = parseTime(end, '{y}-{m}-{d}')
      that.table.getdataListParm.parammaps.SGTimeStart = that.table.getdataListParm.parammaps.SGinputDatetime[0]
      that.table.getdataListParm.parammaps.SGTimeEnd = that.table.getdataListParm.parammaps.SGinputDatetime[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
      if (this.table.getdataListParm.parammaps.SGinputDatetime !== '' && this.table.getdataListParm.parammaps.SGinputDatetime !== null) {
        if (this.table.getdataListParm.parammaps.SGinputDatetime.length !== 0) {
          this.table.getdataListParm.parammaps.SGTimeStart = parseTime(this.table.getdataListParm.parammaps.SGinputDatetime[0], '{y}-{m}-{d}')
          this.table.getdataListParm.parammaps.SGTimeEnd = parseTime(this.table.getdataListParm.parammaps.SGinputDatetime[1], '{y}-{m}-{d}')
        }
      } else {
        this.table.getdataListParm.parammaps.SGinputDatetime = []
        this.table.getdataListParm.parammaps.SGTimeStart = ''
        this.table.getdataListParm.parammaps.SGTimeEnd = ''
      }
      if (this.table.getdataListParm.parammaps.PDinputDatetime !== '' && this.table.getdataListParm.parammaps.PDinputDatetime !== null) {
        if (this.table.getdataListParm.parammaps.PDinputDatetime.length !== 0) {
          this.table.getdataListParm.parammaps.PDTimeStart = parseTime(this.table.getdataListParm.parammaps.PDinputDatetime[0], '{y}-{m}-{d}')
          this.table.getdataListParm.parammaps.PDTimeEnd = parseTime(this.table.getdataListParm.parammaps.PDinputDatetime[1], '{y}-{m}-{d}')
        }
      } else {
        this.table.getdataListParm.parammaps.PDinputDatetime = []
        this.table.getdataListParm.parammaps.PDTimeStart = ''
        this.table.getdataListParm.parammaps.PDTimeEnd = ''
      }
      if (this.table.getdataListParm.parammaps.CGinputDatetime !== '' && this.table.getdataListParm.parammaps.CGinputDatetime !== null) {
        if (this.table.getdataListParm.parammaps.CGinputDatetime.length !== 0) {
          this.table.getdataListParm.parammaps.CGTimeStart = parseTime(this.table.getdataListParm.parammaps.CGinputDatetime[0], '{y}-{m}-{d}')
          this.table.getdataListParm.parammaps.CGTimeEnd = parseTime(this.table.getdataListParm.parammaps.CGinputDatetime[1], '{y}-{m}-{d}')
        }
      } else {
        this.table.getdataListParm.parammaps.CGinputDatetime = []
        this.table.getdataListParm.parammaps.CGTimeStart = ''
        this.table.getdataListParm.parammaps.CGTimeEnd = ''
      }
      if (this.table.getdataListParm.parammaps.RKinputDatetime !== '' && this.table.getdataListParm.parammaps.RKinputDatetime !== null) {
        if (this.table.getdataListParm.parammaps.RKinputDatetime.length !== 0) {
          this.table.getdataListParm.parammaps.RKTimeStart = parseTime(this.table.getdataListParm.parammaps.RKinputDatetime[0], '{y}-{m}-{d}')
          this.table.getdataListParm.parammaps.RKTimeEnd = parseTime(this.table.getdataListParm.parammaps.RKinputDatetime[1], '{y}-{m}-{d}')
        }
      } else {
        this.table.getdataListParm.parammaps.RKinputDatetime = []
        this.table.getdataListParm.parammaps.RKTimeStart = ''
        this.table.getdataListParm.parammaps.RKTimeEnd = ''
      }
      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 = 'getAllPartProcess'
      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', 'partName', 'partCode', 'SGCode', ' SGdate', 'SGDays', 'PDCode', ' PDdate', 'PDDays', 'CGCode', ' CGdate', 'CGDays', 'RKCode', ' RKdate', 'RKDays'],
              tableDatas: this.downLoadParm.list,
              sheetName: 'Sheet'
            }
          ]
          json2excel(excelDatas, '申购入库效率统计', true, 'xlsx')
        })
      })
    }
  }
}
</script>
