<template>
  <div>
    <el-card>
      <span class="show-text">设备:</span>
      <el-select v-model="OpName" clearable @change="getPostList()" filterable placeholder="请选择设备"
        style="width: 200px;margin-right: 10px">
        <el-option v-for="item in OpNameList" :key="item.value" :label="item.label" :value="item.value" size="mini" />
      </el-select>
      <span class="show-text">数据名称:</span>
      <el-input v-model="posName" placeholder="数据名称" clearable style="width: 200px;margin-right: 10px" />
      <span class="show-text">采集日期:</span>
      <el-date-picker v-model="collectionTime" :clearable="false" :picker-options="pickerOptions" end-placeholder="结束时间"
        format="yyyy-MM-dd" popper-class="virtual-cell-time-picker" range-separator="-" start-placeholder="开始时间"
        style="width: 280px;vertical-align: top" type="datetimerange" value-format="yyyy-MM-dd" />
      <el-button :disabled="loading" icon="el-icon-search" plain type="primary" @click="searchTable()">查询</el-button>
      <el-button :disabled="loading" icon="el-icon-download" plain type="success" @click="searchTableExport()">导出
      </el-button>
      <div style="margin-top: 10px;display: flex;">
        <div>
          <el-table v-loading="loading" :data="tableData" border element-loading-background="rgba(0, 0, 0, 0.2)"
            :height="'77vh'" element-loading-spinner="el-icon-loading" size="mini" style="width: 1101px;"
            tooltip-effect="dark">
            <el-table-column align="center" label=" " type="index" width="100" />
            <el-table-column align="center" label="设备" width="150">
              <template slot-scope="scope">
                {{ scope.row.工位号 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="设备名称" width="150">
              <template slot-scope="scope">
                {{ scope.row.工位名称 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="数据名称">
              <template slot-scope="scope">
                {{ scope.row.数据名称 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="数据值">
              <template slot-scope="scope">
                {{ scope.row.数据值 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="采集时间" width="200">
              <template slot-scope="scope">
                {{ scope.row.采集时间 }}
              </template>
            </el-table-column>
          </el-table>
          <div class="block" style="margin-top: 10px;">
            <el-pagination v-if="!loading" :current-page="pageList" :page-size="pageSize"
              :page-sizes="[50, 100, 200, 300]" :total="total" background
              layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange"
              @current-change="handleCurrentChange" />
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  data() {
    return {
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      OpName: '',
      OpNameList: [],
      posName: '',
      posNameList: [],
      showEchartsType: '1',
      collectionTime: [],
      tableDataExcel: [],
      loading: false,
      tableData: [],
      total: 0, // 总条数
      pageSize: 50, // 每页显示条数
      pageList: 1 // 后台获取页,
    }
  },
  created() {
    this.getOpName()
    var now = new Date()
    var start = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0)).toISOString().replace('T', ' ').replace('.000Z', '')
    var end = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59)).toISOString().replace('T', ' ').replace('.000Z', '')
    this.collectionTime = [start, end]
    this.searchTable()
  },
  methods: {
    // 查询工位号
    getOpName() {
      var param = []
      var Data = this.CreateData('11', '工位与名称_查询', param)
      this.ExecDatabase(Data).then(response => {
        this.OpNameList = []
        for (const item of response.data) {
          this.OpNameList.push({ label: item.工位号 + '-' + item.工位名称简称, value: item.工位号 })
        }
      })
    },
    // 查询
    searchTable() {
      if (this.collectionTime.length === 0) {
        this.$message.error('请选择需要查询的时间段！')
        return
      }
      this.loading = true
      this.tableData = []
      var param = []
      param.push(['工位号', this.OpName])
      param.push(['数据名称', this.posName])
      param.push(['开始时间', this.collectionTime[0]])
      param.push(['结束时间', this.collectionTime[1]])
      param.push(['PageCurrent', this.pageList])
      param.push(['PageSize', this.pageSize])
      param.push(['PageCount', '1111', 'int', '1'])
      param.push(['ItemCount', '1111', 'int', '1'])
      var Data = this.CreateData('11', 'WEB_历史_设备数据_定时_分页查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableData = response.data.result
        }
        this.total = parseInt(response.data.output[0].ItemCount)
        this.loading = false
      })
    },
    // 导出数据
    searchTableExport() {
      if (this.tableData.length !== 0) {
        var param = []
        param.push(['工位号', this.OpName])
        param.push(['数据名称', this.posName])
        param.push(['开始时间', this.collectionTime[0]])
        param.push(['结束时间', this.collectionTime[1]])
        param.push(['PageCurrent', this.pageList])
        param.push(['PageSize', this.pageSize])
        param.push(['PageCount', '1111', 'int', '1'])
        param.push(['ItemCount', '1111', 'int', '1'])
        var Data = this.CreateData('2001', 'WEB_历史_设备数据_定时_分页查询', param)
        this.exportExcel_NPOI(Data)
      } else {
        this.$message.warning('暂无数据')
      }
    },
    // 分页
    handleSizeChange(val) {
      this.pageList = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageList = val
      this.searchTable()
    }
  }
}
</script>
