<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" placeholder="牧场" class="filter-item" style="width: 120px;" @change="changePastureName">
        <el-option v-for="item in findAllPasture" :key="item.id" :label="item.name" :value="item.name" />
      </el-select>
     

      <el-date-picker
            ref="inputDatetime"
            v-model="getdataListParm.parammaps.inputDatetime"
            class="inputDatetime"
            type="datetimerange"
            style="width: 280px;top:-3px;"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="时间"
            end-placeholder="时间"
            :picker-options="pickerOptions"
          />
      <el-input v-model="getdataListParm.parammaps.eqName" placeholder="请输入设备名称" style="width: 200px;" class="filter-item" />
 
 
      <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-download" style="" @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="tableCellStyle"
        class="elTable"
        :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 label="牧场" align="center" prop="pastureName" />
        <el-table-column label="设备名称" align="center" prop="eqName" />
        <el-table-column label="设备内部编号" align="center" prop="eqCode" />
        <el-table-column label="运行时间" align="center" prop="run" />
        <el-table-column label="停机时间" align="center" prop="stop" />
      </el-table>
      <pagination v-show="total>=0" :total="total" :page.sync="getdataListParm.offset" :limit.sync="getdataListParm.pagecount" @pagination="get_table_data" />
    </div>
  </div>
</template>

<script>
// 引入
import { GetDataByName, GetDataByNames, getRecuData, GetAccount,startStopRecord } from '@/api/common'
import waves from '@/directive/waves'
 
import { parseTime, json2excel, sortChange } from '@/utils/index.js'
import Pagination from '@/components/Pagination'
import TreeSelect from '@/components/TreeSelect'
import Cookies from 'js-cookie'
export default {
  name: 'StopRecord',
  components: { Pagination, TreeSelect },
  directives: { waves },
  data() {
    return {
      findAllPasture: [], findAllDepart: [],
      requestParams: [
        { name: 'findAllPasture', offset: 0, pagecount: 0, returntype: 'Map', parammaps: { 'id': Cookies.get('pastureid') }}
      ],
      getDepartParam: { name: 'findAllDepart1', offset: 0, pagecount: 0, parammaps: { 'pastureId': Cookies.get('pastureid'), 'eId': Cookies.get('employeid') }},
      disabled: false, placeholder: '请选择设备类别',
 
  
 
   
      getRecuListParm: { name: 'getAssetTypeList', idname: 'id', params: [-1] },
      tableKey: 0,
      list: [
         
      ],

      selectData: '',
      pickerMinDate: '',
      pickerRange:3600 * 1000 * 24 * 60,
      pickerOptions: {
      //   onPick:({maxDate,minDate }) => {
      //     if (minDate && this.pickerMinDate) {
      //       this.pickerMinDate = null
      //     } else if (minDate) {
      //       this.pickerMinDate = minDate.getTime()
      //     }
      //  },
      //   disabledDate: (time) => {
      //     if (this.pickerMinDate) {
      //         return (time.getTime() > (this.pickerMinDate + this.pickerRange))  ||(time.getTime() < (this.pickerMinDate - this.pickerRange))
      //     }
      //   return false
      //   },
      },


      total: 0,
      listLoading: true,
      getdataListParm: {
        name: 'startStopRecord', page: 1, offset: 1, pagecount: 10, returntype: 'Map',inputDatetime: [parseTime(new Date().getTime() - 30 * 24 * 60 * 60 * 1000, '{y}-{m}-{d}'), parseTime(new Date().getTime() - 24 * 60 * 60 * 1000, '{y}-{m}-{d}')],
        parammaps: { pastureId: Cookies.get('pastureid'), pastureName: Cookies.get('pasturename'), departmentName: '', eqClassId: '', eqName: '',startTime: "",
        endTime: "",departmentId: "" ,inputDatetime: [parseTime(new Date().getTime() - 30 * 24 * 60 * 60 * 1000, '{y}-{m}-{d}'), parseTime(new Date().getTime() - 24 * 60 * 60 * 1000, '{y}-{m}-{d}')], page: 1, offset: 1, pagecount: 10,}
      },
      requestParam: { name: 'insertAsset', offset: 0, pagecount: 0, parammaps: {}}, statueReason: false,
      downLoadParm: {},
      downLoadList: [],
      pageNum: 0,
      pageSize: 0,
      isPercentage: false,
      percentage: 1,
      isokDisable: false,
      rowStyle: { maxHeight: 50 + 'px', height: 45 + 'px' },
      cellStyle: { padding: 0 + 'px' },
      myHeight:document.documentElement.clientHeight - 85- 150
    }
  },
  created() {
    this.get_select_list()
     this.get_table_data()
 
  },
  methods: {

    getSummaries1(param) {
        const { columns, data } = param;
        const sums = [];
        columns.forEach((column, index) => {
          if (index === 0) {
              sums[index] = '合计';
            return;
          }
          const values = data.map(item => Number(item[column.property]));
          console.log(column.property)
          console.log(values)
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
            // sums[index] += ' 元';
          } else {
            sums[index] = '-';
          }
        });

        return sums;
    },

    get_select_list() {
      GetDataByNames(this.requestParams).then(response => {
        if (response.data.list !== null) {
          this.findAllPasture = response.data.findAllPasture.list
          this.getDepartDownList()
        }
      })
    },
    getDepartDownList() {
      GetDataByName(this.getDepartParam).then(response => {
        this.findAllDepart = response.data.list
      })
    },
    changePastureName(item) {
      this.getDepartParam.parammaps.pastureId = this.findAllPasture.find(obj => obj.name == item).id
      this.getdataListParm.parammaps.departmentId = ''
      this.getDepartDownList()
    },
    get_table_data() {
      this.listLoading = true
      if (this.getdataListParm.parammaps.pastureName == '现代牧业') {
        // this.getdataListParm.parammaps.pastureName = ''
        this.getdataListParm.parammaps.pastureId = ''
      }

      if (this.getdataListParm.parammaps.inputDatetime == null) {
        this.getdataListParm.parammaps.startTime = ''
        this.getdataListParm.parammaps.endTime = ''
      } else {
        

        this.getdataListParm.parammaps.startTime = this.getdataListParm.parammaps.inputDatetime[0]
        this.getdataListParm.parammaps.endTime = this.getdataListParm.parammaps.inputDatetime[1]
      }


      this.getdataListParm.parammaps.offset = this.getdataListParm.offset
      this.getdataListParm.parammaps.pagecount = this.getdataListParm.pagecount
      this.getdataListParm.parammaps.page = this.getdataListParm.page
      startStopRecord(this.getdataListParm.parammaps).then(response => {
        console.log('table数据', response.data.list)
        if (response.data.list !== null) {
          this.list = response.data.list
          this.pageNum = response.data.pageNum
          this.pageSize = response.data.pageSize
        } else {
          this.list = []
        }
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 100)
      })
    },
    form_search() {
      this.listLoading = true
      this.getdataListParm.offset = 1
      this.getdataListParm.parammaps.pastureId = this.findAllPasture.find(obj => obj.name == this.getdataListParm.parammaps.pastureName).id
      this.get_table_data()
    },

  
  
    tableCellStyle({ row, column, rowIndex, columnIndex }) {
      // if (columnIndex === 3) {
      //   return {
      //     textDecoration: 'underline'
      //   }
      // }
      // return {
      //   textDecoration: 'none'
      // }
    },
   

    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 = 'startStopRecord'
      

      
      this.downLoadParm.parammaps = this.getdataListParm.parammaps
      this.downLoadParm.parammaps.pagecount = 0
      this.downLoadParm.parammaps.page = 0
      this.downLoadParm.parammaps.offset = 0
      startStopRecord(this.downLoadParm.parammaps).then(response => {
        this.downLoadList = response.data.list
        if (response.data.list !== '') {
          this.percentage = 99
          setTimeout(() => {
            this.isPercentage = false
          }, 2000)
        }
        console.log(this.downLoadList)
        const elecExcelDatas = [
          {
            tHeader: ['牧场', '设备名称', '设备内部编号',  '运行时间', '停机时间' ],
            filterVal: ['pastureName', 'eqName','eqCode', 'run', 'stop' ],
            tableDatas: this.downLoadList,
            sheetName: '启停记录'
          }
        ]
        json2excel(elecExcelDatas, '启停记录', true, 'xlsx')
      })
    }
 

  }
}
</script>
<style lang="scss" scoped>
  .el-autocomplete-suggestion li{
    padding:0 3px!important;
  }
  .el-table .warning-row {
      background: oldlace;
  }
  .el-table .success-row {
      background: #f0f9eb;
  }
</style>
<style lang="scss">
  .upkeepgrade .el-form-item__label{
    line-height: 20px;
  }
  .inputDatetime .el-range-separator{
   padding: 0;
   margin: 0 10px;
  }
  .el-radio__label{
    padding-left: 2px !important;
  }
  .typeSelect span .el-popover__reference .el-input--suffix {
    top:-3px;
    .el-input__inner{
      height:40px !important;
    }
  }
</style>
