<template>
  <div>
    <el-card>
      <span class="show-text">客户:</span>
      <el-select v-model="client" clearable filterable placeholder="客户" style="width: 200px;margin-right: 10px">
        <el-option v-for="item in clientList" :key="item.value" :label="item.label" :value="item.value" size="mini" />
      </el-select>
      <span class="show-text">设备:</span>
      <el-select v-model="OpName" clearable 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-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)"
            element-loading-spinner="el-icon-loading" element-loading-text="数据加载中" highlight-current-row size="small"
            :height="'77vh'" stripe style="width: 1101px;" tooltip-effect="dark">
            <el-table-column align="center" label="序号" type="index" width="60" />
            <el-table-column :width="setColumnWidth('客户')" align="center" label="客户" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.客户 }}
              </template>
            </el-table-column>
            <el-table-column :width="setColumnWidth('设备')" align="center" label="设备">
              <template slot-scope="scope">
                {{ scope.row.设备 }}
              </template>
            </el-table-column>
            <el-table-column :width="setColumnWidth('报警类型')" align="center" label="报警类型">
              <template slot-scope="scope">
                {{ scope.row.报警类型 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="报警内容" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.报警内容 }}
              </template>
            </el-table-column>
            <el-table-column :width="setColumnWidth('datetime')" align="center" label="报警日期">
              <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" layout="total, sizes, prev, pager, next, jumper"
              @size-change="handleSizeChange" @current-change="handleCurrentChange" />
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      OpName: '',
      client: '',
      clientList: [],
      OpNameList: '',
      showEchartsType: '1',
      collectionTime: [],
      tableDataExcel: [],
      loading: false,
      tableData: [],
      total: 0, // 总条数
      pageSize: 50, // 每页显示条数
      pageList: 1 // 后台获取页,
    }
  },
  computed: {
    ...mapGetters([
      'id'
    ])
  },
  created() {
    this.getOpName()
    this.getClientList()
    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()
    // 进入页面 更新报警信息查看日期
    this.updateUserCheckTime()
  },
  methods: {
    // 查询工位号
    getOpName() {
      var param = []
      param.push(['Personnel_Number', this.$store.state.user.id])
      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.client])
      param.push(['工位号', this.OpName])
      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
      })
    },
    getClientList() {
      var param = []
      param.push(['Personnel_Number', this.$store.state.user.id])
      var Data = this.CreateData('11', 'WEB_客户查询', param)
      this.ExecDatabase(Data).then(response => {
        this.clientList = []
        for (const item of response.data) {
          this.clientList.push({ label: item.出售公司名称, value: item.出售公司名称 })
        }
      })
    },
    // 导出数据
    searchTableExport() {
      if (this.tableData.length !== 0) {
        var param = []
        param.push(['客户', this.client])
        param.push(['工位号', this.OpName])
        param.push(['开始时间', this.collectionTime[0]])
        param.push(['结束时间', this.collectionTime[1]])
        param.push(['PageCurrent', this.pageList])
        param.push(['PageSize', 9999999])
        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('暂无数据')
      }
    },
    updateUserCheckTime() {
      var param = []
      param.push(['personnel_Number', this.id])
      var Data = this.CreateData('11', 'WEB_报警查询_报警读取时间_更新', param)
      this.ExecDatabase(Data)
    },
    // 分页
    handleSizeChange(val) {
      this.pageList = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageList = val
      this.searchTable()
    }
  }
}
</script>
