<template>
  <div>
    <el-card>
      <span class="show-text">报警名称:</span>
      <el-input v-model="TagName" clearable placeholder="请输入报警名称" style="width: 280px;margin-right: 26px"/>
      <span class="show-text">采集时间:</span>
      <el-date-picker
        v-model="collectionTime"
        :clearable="false"
        :picker-options="pickerOptions"
        end-placeholder="结束时间"
        format="yyyy-MM-dd HH:mm:ss"
        popper-class="virtual-cell-time-picker"
        range-separator="-"
        start-placeholder="开始时间"
        style="width: 360px;margin-right: 16px;vertical-align: top"
        type="datetimerange"
        value-format="yyyy-MM-dd HH:mm:ss"/>
      <el-button :disabled="loading" icon="el-icon-search" plain type="primary" @click="searchTable()">查询</el-button>
      <el-button icon="el-icon-download" style="margin-left: 14px" type="info" @click="searchTableALL()">导出
      </el-button>
      <div style="margin-top: 10px">
        <el-table
          v-loading="loading"
          :data="tableData"
          border
          element-loading-background="rgba(0, 0, 0, 0.2)"
          element-loading-spinner="el-icon-loading"
          element-loading-text="数据加载中"
          height="707"
          highlight-current-row
          size="mini"
          stripe
          style="width: 1301px"
          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="450">
            <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="报警时间" width="200">
            <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-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>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      TagName: '',
      collectionTime: [],
      tableDataExcel: [],
      loading: false,
      tableData: [],
      total: 0, // 总条数
      pageSize: 50, // 每页显示条数
      pageList: 1 // 后台获取页
    }
  },
  created() {

  },
  methods: {
    // 查询
    searchTable() {
      if (this.collectionTime.length === 0) {
        this.$message.error('请选择需要查询的时间段！')
        return
      }
      this.loading = true
      this.tableData = []
      var param = []
      param[0] = ['TagName', this.TagName]
      param[1] = ['开始时间', this.collectionTime[0]]
      param[2] = ['结束时间', this.collectionTime[1]]
      param[3] = ['PageCurrent', this.pageList]
      param[4] = ['PageSize', this.pageSize]
      param[5] = ['PageCount', '1111', 'int', '1']
      param[6] = ['ItemCount', '1111', 'int', '1']
      var Data = this.CreateData('11', '故障数据_查询', 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
      })
    },
    // 导出数据
    searchTableALL() {
      if (this.collectionTime.length === 0) {
        this.$message.error('请选择需要查询的时间段！')
        return
      }
      if (this.tableData.length === 0) {
        this.$message.error('请查询需要导出的数据！')
        return
      }
      var param = []
      if (this.tableData.length === 0) {
        this.$message.warning('请查询要导出的数据')
        return
      }
      param[0] = ['TagName', this.TagName]
      param[1] = ['开始时间', this.collectionTime[0]]
      param[2] = ['结束时间', this.collectionTime[1]]
      var Data = this.CreateData('2003', '故障数据_查询_导出', param)
      const blob = 'data:image/gif;base64,R0lGODlhCQACAIAAAMzMzP///yH5BAEAAAEALAAAAAAJAAIAAAIEjI9pUAA7'
      this.exportExcel_NPOI(Data, blob)
    },
    // 分页
    handleSizeChange(val) {
      this.pageList = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageList = val
      this.searchTable()
    }
  }
}
</script>
