<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>
    <el-button class="filter-item" type="success" icon="el-icon-download" style="float: right;margin-bottom: 10px;" @click="handleDownload">导出</el-button>
    <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="tableCellStyle"
      class="elTable table-fixed"
      :span-method="objectSpanMethod"
    >
      <!-- <el-table-column label="序号" align="center" width="50px" class-name="small-padding fixed-width" fixed="left" prop="i" /> -->
      <el-table-column label="牧场" prop="pastureName" min-width="90px" align="center" class-name="small-padding fixed-width" fixed="left" />
      <el-table-column label="项目" min-width="90px" align="center">
        <template slot-scope="{row}">
          <a @click="tableCellClick(row)">{{ row.project }}</a>
        </template>
      </el-table-column>
      <el-table-column label="迄今汇总设备" prop="sumEq" min-width="70px" align="center" />
      <el-table-column :label="year+'-01'" prop="m1" min-width="70px" align="center" />
      <el-table-column :label="year+'-02'" prop="m2" min-width="70px" align="center" />
      <el-table-column :label="year+'-03'" prop="m3" min-width="70px" align="center" />
      <el-table-column :label="year+'-04'" prop="m4" min-width="70px" align="center" />
      <el-table-column :label="year+'-05'" prop="m5" min-width="70px" align="center" />
      <el-table-column :label="year+'-06'" prop="m6" min-width="70px" align="center" />
      <el-table-column :label="year+'-07'" prop="m7" min-width="70px" align="center" />
      <el-table-column :label="year+'-08'" prop="m8" min-width="70px" align="center" />
      <el-table-column :label="year+'-09'" prop="m9" min-width="70px" align="center" />
      <el-table-column :label="year+'-10'" prop="m10" min-width="70px" align="center" />
      <el-table-column :label="year+'-11'" prop="m11" min-width="70px" align="center" />
      <el-table-column :label="year+'-12'" prop="m12" min-width="70px" align="center" />
    </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 Pagination from '@/components/Pagination'
import { GetDataByName } from '@/api/common'
import { parseTime, json2excel } from '@/utils/index.js'
import Cookies from 'js-cookie'
export default {
  name: 'PurchaseScrap',
  components: { Pagination },
  data() {
    return {
      table: {
        listLoading: true, tableKey: 0,
        getdataListParm: {
          name: 'equipmentStatistical', page: 1, offset: 1, pagecount: '', returntype: 'Map',
          parammaps: {}
        },
        list: [], pageNum: '', pageSize: '', total: ''
      },
      year: parseTime(new Date(), '{y}'),
      spanObj: {},
      mergekeys: ['pastureName', 'i'],
      isPercentage: false,
      percentage: 1,
      downLoadParm: {},
      downLoadList: [],
      rowStyle: { maxHeight: 50 + 'px', height: 45 + 'px' }
    }
  },
  created() {
    this.getList()
  },

  methods: {
    getList() {
      this.table.listLoading = true
      GetDataByName(this.table.getdataListParm).then(response => {
        console.log('table数据', response.data.list)
        if (response.data.list !== null) {
          console.log('table数据', response.data.list)
          var arrList = []
          for (let i = 0; i < response.data.list.length; i++) {
            for (let j = i + 1; j < response.data.list.length; j++) {
              if (response.data.list[i].pastureName == response.data.list[j].pastureName) {
                arrList.push(response.data.list[i])
                arrList.push(response.data.list[j])
              }
            }
            response.data.list[i].sumEq = parseFloat(response.data.list[i].m1) + parseFloat(response.data.list[i].m2) + parseFloat(response.data.list[i].m3) + parseFloat(response.data.list[i].m4) + parseFloat(response.data.list[i].m5) + parseFloat(response.data.list[i].m6) + parseFloat(response.data.list[i].m7) + parseFloat(response.data.list[i].m8) + parseFloat(response.data.list[i].m9) + parseFloat(response.data.list[i].m10) + parseFloat(response.data.list[i].m11) + parseFloat(response.data.list[i].m12)
          }
          console.log(arrList)
          this.table.list = arrList
          this.handleSpan()
          this.table.pageNum = response.data.pageNum
          this.table.pageSize = response.data.pageSize
          this.table.total = response.data.total
        } else {
          this.table.list = []
        }
        setTimeout(() => {
          this.table.listLoading = false
        }, 100)
      })
    },

    handleSpan() {
      this.mergekeys.forEach(key => {
        this.spanObj[key] = []
        let position = 0
        this.table.list.forEach((item, index) => {
          if (index === 0) {
            this.spanObj[key].push(1)
            position = 0
          } else {
            if (key == 'i') {
              if (this.table.list[index][key] === this.table.list[index - 1][key] && this.table.list[index]['pastureName'] === this.table.list[index - 1]['pastureName']) {
                this.spanObj[key][position] += 1
                this.spanObj[key].push(0)
              } else {
                this.spanObj[key].push(1)
                position = index
              }
            } else {
              if (this.table.list[index][key] === this.table.list[index - 1][key]) {
                this.spanObj[key][position] += 1
                this.spanObj[key].push(0)
              } else {
                this.spanObj[key].push(1)
                position = index
              }
            }
          }
        })
      })
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      for (let i = 0; i < this.mergekeys.length; i++) {
        if (column.property === this.mergekeys[i]) {
          const _row = this.spanObj[this.mergekeys[i]][rowIndex]
          const _col = _row > 0 ? 1 : 0
          return {
            rowspan: _row,
            colspan: _col
          }
        }
      }
    },
    tableCellStyle({ row, column, rowIndex, columnIndex }) {
      if ((row.project !== '新购设备数' || row.project !== '报废设备数') && columnIndex == 1) {
        return {
          textDecoration: 'underline'
        }
      }
      return {
        textDecoration: 'none'
      }
    },
    tableCellClick(row) {
      if (Cookies.get('pasturename') == '现代牧业') {
        this.$router.push({ path: '/asset/basics', query: { myPath: 'PurchaseScrap', pastureName: row.pastureName, pastureId: row.pastureId, project: row.project }})
      } else {
        if (row.name == Cookies.get('pasturename')) {
          this.$router.push({ path: '/asset/basics', query: { myPath: 'PurchaseScrap', pastureName: row.pastureName, pastureId: row.pastureId, project: row.project }})
        } else {
          this.$message({ type: 'error', message: '请联系管理员开通相关牧场数据权限' })
        }
      }
    },
    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 = 'equipmentStatistical'
      this.downLoadParm.parammaps = this.table.getdataListParm.parammaps
      GetDataByName(this.downLoadParm).then(response => {
        this.downLoadList = response.data.list
        if (response.data.list !== '') {
          this.percentage = 99
          setTimeout(() => {
            this.isPercentage = false
          }, 2000)
        }
        console.log(this.downLoadList)
        var headerList = ['牧场', '项目', '迄今设备']
        for (let i = 1; i <= 12; i++) {
          if (i < 10) {
            headerList.push(this.year + '-0' + i)
          } else {
            headerList.push(this.year + '-' + i)
          }
        }
        console.log(headerList)
        const elecExcelDatas = [
          {
            tHeader: headerList,
            filterVal: ['pastureName', 'project', 'sumEq', 'm1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8', 'm9', 'm10', 'm11', 'm12'],
            tableDatas: this.downLoadList,
            sheetName: 'SheetJS'
          }
        ]
        json2excel(elecExcelDatas, '设备购置报废统计', true, 'xlsx')
      })
    }
  }
}
</script>
