<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-date-picker
        v-if="isPraseTime"
        v-model="getdataListParm.parammaps.praseTime"
        :clearable="false"
        type="date"
        :picker-options="pickerOptions1"
        placeholder="选择日期"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd"
        style="top:-3px"
      />
      <el-button v-if="isPraseTime" v-waves class="filter-item" type="primary" icon="el-icon-search" @click="form_search">搜索</el-button>
      <el-select v-if="isType" v-model="getDeptListParm.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-button v-if="isPraseTime" class="filter-item" type="success" icon="el-icon-upload2" style="position:absolute ;right:10px" @click="handleDownload">导出</el-button>
    </div>
    <div v-if="isTitle" style="line-height:50px;height:50px"><b @click="goBackPasture">所有牧场</b>&gt;<span>{{ seeTemp.pastureName }}</span></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="tableCellStyle"
      class="elTable"
      @sort-change="tableSort1"
    >
      <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="isTable1" :key="1" label="牧场" min-width="140px" align="center">
        <template slot-scope="{row}">
          <a @click="tableCellClick(row)">{{ row.pastureName }}</a>
        </template>
      </el-table-column>
      <el-table-column v-if="isTable2" :key="2" label="部门" min-width="140px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.departmentName }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="isTable3" :key="3" label="设备类别" min-width="140px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.typeName }}</span>
        </template>
      </el-table-column>
      <el-table-column :key="4" label="在用数量" min-width="100px" align="center" sortable prop="eqUse" />
      <el-table-column :key="5" label="使用数量" min-width="100px" align="center" sortable prop="eqQY" />
      <el-table-column :key="6" label="停用数量" min-width="100px" align="center" sortable prop="eqTY" />
      <el-table-column :key="7" label="设备运行时间(分钟)" min-width="90px" align="center" sortable prop="runTime" />
      <el-table-column :key="8" label="维修数量" min-width="90px" align="center" sortable prop="maintain" />
      <el-table-column :key="9" label="设备利用率" min-width="100px" align="center" sortable prop="eqQY2">
        <template slot-scope="scope">
          <span>{{ scope.row.eqQY2 }}%</span>
        </template>
      </el-table-column>
      <el-table-column :key="10" label="设备运转率" min-width="90px" align="center" sortable prop="totalTime2">
        <template slot-scope="scope">
          <span>{{ scope.row.runTime2 }}%</span>
        </template>
      </el-table-column>
      <el-table-column :key="11" label="设备故障率" min-width="100px" align="center" sortable prop="maintain2">
        <template slot-scope="scope">
          <span>{{ scope.row.maintain2 }}% </span>
        </template>
      </el-table-column>
      <el-table-column v-if="isDetailed" label="操作" align="center" width="150" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleDetailed(row)">明细</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" v-if="isTable1" :total="total" :page.sync="getdataListParm.offset" :limit.sync="getdataListParm.pagecount" @pagination="getList" />
    <pagination v-show="total>0" v-if="isTable2" :total="total2" :page.sync="getDeptListParm.offset" :limit.sync="getDeptListParm.pagecount" @pagination="getDeptList" />
    <pagination v-show="total>0" v-if="isTable3" :total="total3" :page.sync="getTypeListParm.offset" :limit.sync="getTypeListParm.pagecount" @pagination="getTypeList" />
  </div>

</template>

<script>
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { GetDataByName } from '@/api/common'
import { parseTime, json2excel, sortChange } from '@/utils/index.js'
export default {
  name: 'EquipmentOperation',
  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' },
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      getdataListParm: {
        name: 'getEqRunstatueListPasture',
        page: 1,
        offset: 1,
        pagecount: 10,
        returntype: 'Map',
        parammaps: {
          praseTime: parseTime(new Date().getTime() - 24 * 60 * 60 * 1000, '{y}-{m}-{d}')
        }
      },
      pickerOptions1: {
        disabledDate(time) {
          return time.getTime() > new Date().getTime() - 24 * 60 * 60 * 1000
        }
      },
      seeTemp: {},
      isTitle: false,
      isPraseTime: true,
      isTable1: true,
      types: [{ id: 0, name: '按部门' }, { id: 1, name: '按设备类别' }],
      isType: false,
      total2: 0,
      isTable2: false,
      getDeptListParm: {
        name: 'getEqRunstatueListDept',
        page: 1,
        offset: 1,
        pagecount: 10,
        returntype: 'Map',
        parammaps: {
          type: 0
        }
      },
      total3: 0,
      isTable3: false,
      getTypeListParm: {
        name: 'getEqRunstatueListEqclass',
        page: 1,
        offset: 1,
        pagecount: 10,
        returntype: 'Map',
        parammaps: {}
      },
      isDetailed: false,
      downLoadParm: {},
      downLoadList: [],
      isPercentage: false,
      percentage: 1
    }
  },

  created() {
    this.getList()
  },

  methods: {
    tableSort1(column) {
      sortChange(column, this.list)
    },
    getList() {
      GetDataByName(this.getdataListParm).then(response => {
        if (response.data.list !== null) {
          console.log('table数据', response.data.list)
          for (let i = 0; i < response.data.list.length; i++) {
            if (response.data.list[i].eqQY !== 0 && response.data.list[i].eqUse !== 0) {
              this.$set(response.data.list[i], 'eqQY2', (parseFloat(response.data.list[i].eqQY) / parseFloat(response.data.list[i].eqUse) * 100).toFixed(2))
            } else {
              this.$set(response.data.list[i], 'eqQY2', 0)
            }
            if (response.data.list[i].runTime !== 0 && response.data.list[i].totalTime !== 0) {
              this.$set(response.data.list[i], 'runTime2', (parseFloat(response.data.list[i].runTime) / parseFloat(response.data.list[i].totalTime) * 100).toFixed(2))
            } else {
              this.$set(response.data.list[i], 'ontimeClassCount2', 0)
            }
            if (response.data.list[i].maintain !== 0 && response.data.list[i].eqUse !== 0) {
              this.$set(response.data.list[i], 'maintain2', (parseFloat(response.data.list[i].maintain) / parseFloat(response.data.list[i].eqUse) * 100).toFixed(2))
            } else {
              this.$set(response.data.list[i], 'maintain2', 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)
      })
    },
    form_search() {
      this.listLoading = true
      this.getdataListParm.offset = 1
      this.getList()
    },
    tableCellStyle({ row, column, rowIndex, columnIndex }) {
      if (row.pastureName !== '现代牧业' && row.departmentName == null && row.typeName == null && columnIndex === 1) {
        return {
          textDecoration: 'underline'
        }
      }
      return {
        textDecoration: 'none'
      }
    },
    tableCellClick(row) { // 获取焦点弹出关联多个子账号
      if (row.pastureName !== '现代牧业') {
        this.seeTemp = Object.assign({}, row)
        // 日期/牧场table隐藏
        this.isPraseTime = false
        this.isTable1 = false
        // 部门table/标题显示
        this.isTable2 = true
        this.isTitle = true
        this.isType = true
        // 明细显示
        this.isDetailed = true
        this.listLoading = true
        this.getDeptList()
      } else {
        this.isTitle = false
      }
    },
    changeType(val) {
      console.log('选中值')
      this.Type = val
      if (this.Type == 0) {
        this.listLoading = true
        this.getDeptList()
        // 类别table隐藏/部门table显示
        this.isTable2 = true
        this.isTable3 = false
      } else {
        this.listLoading = true
        // 部门table隐藏/类别table显示
        this.isTable2 = false
        this.isTable3 = true
        this.getTypeList()
      }
    },
    getDeptList() {
      this.getDeptListParm.parammaps.pastureId = this.seeTemp.pastureId
      this.getDeptListParm.parammaps.praseTime = this.getdataListParm.parammaps.praseTime
      GetDataByName(this.getDeptListParm).then(response => {
        console.log('table数据', response.data.list)
        this.$nextTick(() => {
          this.list = response.data.list
        })
        this.pageNum = response.data.pageNum
        this.pageSize = response.data.pageSize
        this.total2 = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 100)
      })
    },
    getTypeList() {
      this.getTypeListParm.parammaps.pastureId = this.seeTemp.pastureId
      this.getTypeListParm.parammaps.praseTime = this.getdataListParm.parammaps.praseTime
      GetDataByName(this.getTypeListParm).then(response => {
        console.log('table数据', response.data.list)
        this.list = response.data.list
        this.pageNum = response.data.pageNum
        this.pageSize = response.data.pageSize
        this.total3 = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 100)
      })
    },
    goBackPasture() {
      // 集团table1/时间/搜索/导出显示/部门table/类别table/标题/明细按钮隐藏
      this.isType = false
      this.isPraseTime = true
      this.isTable1 = true
      this.isTable2 = false
      this.isTable3 = false
      this.isTitle = false
      this.isDetailed = false
      this.listLoading = true
      this.getList()
    },
    handleDetailed(row) {
      console.log(row)
      this.$router.push({ path: '/maintenance/SST', query: { deptId: row.deptId }})
      this.getdataListParm.parammaps.deptId = row.deptId
    },
    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 = 'getEqRunstatueListPasture'
      this.downLoadParm.returntype = 'Map'
      this.downLoadParm.parammaps = this.getdataListParm.parammaps
      GetDataByName(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].LYL = ((parseFloat(response.data.list[i].eqQY) / parseFloat(response.data.list[i].eqUse)) * 100).toFixed(2) + '%'
              if (parseFloat(response.data.list[i].runTime) === 0 || parseFloat(response.data.list[i].totalTime) === 0) {
                this.downLoadList[i].YZL = 0
              } else {
                this.downLoadList[i].YZL = ((parseFloat(response.data.list[i].runTime) / parseFloat(response.data.list[i].totalTime)) * 100).toFixed(2) + '%'
              }
              this.downLoadList[i].GZL = ((parseFloat(response.data.list[i].maintain) / parseFloat(response.data.list[i].eqUse)) * 100).toFixed(2) + '%'
            }
          }
          console.log(this.downLoadList)
          const ExcelDatas = [
            {
              tHeader: ['牧场', '在用数量', '使用数量', '停用数量', '设备运行时间（分钟）', '维修数量', '设备利用率', '设备运转率', '设备故障率'],
              filterVal: ['pastureName', 'eqUse', 'eqQY', 'eqTY', 'runTime', 'maintain', 'LYL', 'YZL', 'GZL'],
              tableDatas: this.downLoadList,
              sheetName: '设备运行统计'
            }
          ]
          json2excel(ExcelDatas, '设备运行统计', true, 'xlsx')
        })
      })
    }
  }
}
</script>
