<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.type" class="filter-item" style="width: 120px;" @change="changeType">
        <el-option v-for="item in types" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
      <el-date-picker v-model="getdataListParm.parammaps.buyerDate" 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" type="success" icon="el-icon-upload2" @click="handleDownload">导出</el-button>
    </div>
    <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"
      @sort-change="tableSort1"
      :max-height="myHeight"
    >
      <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 v-if="isPasture" label="牧场" min-width="140px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.pastureName }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="isProvider" label="供应商" min-width="140px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.providerName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单数" min-width="140px" align="center" sortable prop="orderCount" />
      <el-table-column label="按时单数" min-width="140px" align="center" sortable prop="ontimeCount" />
      <el-table-column label="未到单数" min-width="140px" align="center" sortable prop="noCount" />
      <el-table-column label="到货及时率" min-width="140px" align="center" sortable prop="orderCount2">
        <template slot-scope="scope">
          <span>{{ scope.row.orderCount2 }}%</span>
        </template>
      </el-table-column>
      <el-table-column label="物料种数" min-width="140px" align="center" sortable prop="classCount" />
      <el-table-column label="单项完成量" min-width="140px" align="center" sortable prop="ontimeClassCount" />
      <el-table-column label="单项完成率" min-width="140px" align="center" sortable prop="ontimeClassCount2">
        <template slot-scope="scope">
          <span>{{ scope.row.ontimeClassCount2 }}%</span>
        </template>
      </el-table-column>
      <el-table-column label="订单量" min-width="140px" align="center" sortable prop="orderSum" />
      <el-table-column label="到货量" min-width="140px" align="center" sortable prop="receiveSum" />
      <el-table-column label="到货完成率" min-width="140px" align="center" sortable prop="receiveSum2">
        <template slot-scope="scope">
          <span>{{ scope.row.receiveSum2 }}%</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-if="isPasture"
      v-show="total>0"
      :total="total"
      :page.sync="getdataListParm.offset"
      :limit.sync="getdataListParm.pagecount"
      @pagination="get_table_data"
    />
    <pagination
      v-if="isProvider"
      v-show="total>0"
      :total="totalProvider"
      :page.sync="getProviderParm.offset"
      :limit.sync="getProviderParm.pagecount"
      @pagination="getProviderList"
    />
  </div>

</template>

<script>
import waves from '@/directive/waves' // waves directive
import { GetDataByNames, GetDataByName, GetAccount } from '@/api/common'
import Pagination from '@/components/Pagination'
import Cookies from 'js-cookie'
import { parseTime, json2excel, sortChange } from '@/utils/index.js'
export default {
  name: 'TimelyArrivalRate',
  components: { Pagination },
  directives: { waves },
  filters: {
    keepTwoNum(value) {
      value = Number(value)
      return value.toFixed(2)
    }
  },
  data() {
    return {
      rowStyle: { maxHeight: 50 + 'px', height: 45 + 'px' },
      cellStyle: { padding: 0 + 'px' },
      types: [{ id: 0, name: '按牧场' }, { id: 1, name: '按供应商' }],
      findAllPasture: [],
      requestParams: [{ name: 'findAllPasture', offset: 0, pagecount: 0, returntype: 'Map', parammaps: { 'id': Cookies.get('pastureid') }}],
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      isPasture: true,
      getdataListParm: {
        name: 'getOnTineRateListbyPasture',
        page: 1,
        offset: 1,
        pagecount: 10,
        returntype: 'Map',
        parammaps: {
          buyerDate: parseTime(new Date(), '{y}-{m}'),
          type: 0
        }
      },
      isProvider: false,
      totalProvider: 0,
      getProviderParm: {
        name: 'getOnTineRateListbyProvider',
        page: 1,
        offset: 1,
        pagecount: 10,
        returntype: 'Map',
        parammaps: {
          buyerDate: parseTime(new Date(), '{y}-{m}')
        }
      },
      Type: 0,
      downLoadParm: {},
      downLoadList: [],
      isPercentage: false,
      percentage: 1,
      myHeight:document.documentElement.clientHeight - 85- 150
    }
  },

  created() {
    this.get_select_list()
    this.get_table_data()
  },
  mounted() {

  },
  methods: {
    tableSort1(column) {
      sortChange(column, this.list)
    },
    get_select_list() {
      GetDataByNames(this.requestParams).then(response => {
        this.findAllPasture = response.data.findAllPasture.list
      })
    },
    get_table_data() {
      GetDataByName(this.getdataListParm).then(response => {
        console.log('table数据', response.data.list)
        for (let i = 0; i < response.data.list.length; i++) {
          if (response.data.list[i].ontimeCount !== 0 && response.data.list[i].orderCount !== 0) {
            this.$set(response.data.list[i], 'orderCount2', (parseFloat(response.data.list[i].ontimeCount) / parseFloat(response.data.list[i].orderCount) * 100).toFixed(2))
          } else {
            this.$set(response.data.list[i], 'orderCount2', 0)
          }
          if (response.data.list[i].ontimeClassCount !== 0 && response.data.list[i].classCount !== 0) {
            this.$set(response.data.list[i], 'ontimeClassCount2', (parseFloat(response.data.list[i].ontimeClassCount) / parseFloat(response.data.list[i].classCount) * 100).toFixed(2))
          } else {
            this.$set(response.data.list[i], 'ontimeClassCount2', 0)
          }
          if (response.data.list[i].receiveSum !== 0 && response.data.list[i].orderSum !== 0) {
            this.$set(response.data.list[i], 'receiveSum2', (parseFloat(response.data.list[i].receiveSum) / parseFloat(response.data.list[i].orderSum) * 100).toFixed(2))
          } else {
            this.$set(response.data.list[i], 'receiveSum2', 0)
          }
        }
        this.list = response.data.list
        this.pageNum = response.data.pageNum
        this.pageSize = response.data.pageSize
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 100)
      })
    },
    getProviderList() {
      GetDataByName(this.getProviderParm).then(response => {
        console.log('table数据', response.data.list)
        this.list = response.data.list
        this.pageNumEquipmentType = response.data.pageNum
        this.pageSizeEquipmentType = response.data.pageSize
        if (response.data.total) {
          this.totalProvider = response.data.total
        }
        console.log(response.data.total)
        console.log(this.totalProvider)
        setTimeout(() => {
          this.listLoading = false
        }, 100)
      })
    },
    form_search() {
      if (this.getdataListParm.parammaps.type === 0) {
        this.isPasture = true
        this.getdataListParm.offset = 1
        this.get_table_data()
      } else {
        this.isPasture = false
        this.getProviderParm.offset = 1
        this.getProviderParm.parammaps.buyerDate = this.getdataListParm.parammaps.buyerDate
        this.getProviderList()
      }
    },
    changeType(val) {
      this.Type = val
      if (this.Type == 0) {
        this.listLoading = true
        this.getdataListParm.parammaps.pastureName = this.pastureName
        this.isPasture = true
        this.isProvider = false
        this.get_table_data()
      } else {
        this.listLoading = true
        this.isPasture = false
        this.isProvider = true
        this.getProviderParm.parammaps.pastureName = this.pastureName
        this.getProviderList()
      }
    },
    handleDownload() {
      console.log('点击了导出')

      console.log(this.Type)
      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)
      if (this.Type == 0) {
        this.downLoadParm.name = 'getOnTineRateListbyPasture'
        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
            if (response.data.list.length > 0) {
              for (var i = 0; i < response.data.list.length; i++) {
                this.downLoadList[i].DHJSL = ((parseFloat(response.data.list[i].ontimeCount) / parseFloat(response.data.list[i].orderCount)) * 100).toFixed(2) + '%'
                this.downLoadList[i].DXWCL = ((parseFloat(response.data.list[i].ontimeClassCount) / parseFloat(response.data.list[i].classCount)) * 100).toFixed(2) + '%'
                this.downLoadList[i].DHWCL = ((parseFloat(response.data.list[i].receiveSum) / parseFloat(response.data.list[i].orderSum)) * 100).toFixed(2) + '%'
              }
            }
            console.log(this.downLoadList)
            const ExcelDatas = [
              {
                tHeader: ['牧场', '订单数', '按时单数', '未到单数', '到货及时率', '物料种数', '单项完成量', '单项完成率', '订单量', '到货量', '到货完成率'],
                filterVal: ['pastureName', 'orderCount', 'ontimeCount', 'noCount', 'DHJSL', 'classCount', 'ontimeClassCount', 'DXWCL', 'orderSum', 'receiveSum', 'DHWCL'],
                tableDatas: this.downLoadList,
                sheetName: '备件到货及时率'
              }
            ]
            json2excel(ExcelDatas, '备件到货及时率', true, 'xlsx')
          })
        })
      } else if (this.Type == 1) {
        this.downLoadParm.name = 'getOnTineRateListbyProvider'
        this.downLoadParm.returntype = 'Map'
        this.downLoadParm.parammaps = this.getdataListParm.parammaps
        GetAccount(this.downLoadParm).then(response => {
          this.$nextTick(() => {
            this.downLoadList = response.data.list
            if (response.data.list.length > 0) {
              for (var i = 0; i < response.data.list.length; i++) {
                this.downLoadList[i].DHJSL = ((parseFloat(response.data.list[i].ontimeCount) / parseFloat(response.data.list[i].orderCount)) * 100).toFixed(2) + '%'
                this.downLoadList[i].DXWCL = ((parseFloat(response.data.list[i].ontimeClassCount) / parseFloat(response.data.list[i].classCount)) * 100).toFixed(2) + '%'
                this.downLoadList[i].DHWCL = ((parseFloat(response.data.list[i].receiveSum) / parseFloat(response.data.list[i].orderSum)) * 100).toFixed(2) + '%'
              }
            }
            console.log(this.downLoadList)
            const ExcelDatas = [
              {
                tHeader: ['供应商', '订单数', '按时单数', '未到单数', '到货及时率', '物料种数', '单项完成量', '单项完成率', '订单量', '到货量', '到货完成率'],
                filterVal: ['providerName', 'orderCount', 'ontimeCount', 'noCount', 'DHJSL', 'classCount', 'ontimeClassCount', 'DXWCL', 'orderSum', 'receiveSum', 'DHWCL'],
                tableDatas: this.downLoadList,
                sheetName: '备件到货及时率'
              }
            ]
            json2excel(ExcelDatas, '备件到货及时率', true, 'xlsx')
          })
        })
      }
    }
  }
}
</script>
