<template>
  <div class="app-container">
    <div class="filter-container blue-top-border">
      <el-date-picker
        v-model="selectDate"
        class="filter-item el-selects"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        @change="selectDateChange"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        size="small"
        @click="handleFilter"
      >查询</el-button>
    </div>
    <div class="content-container">
      <el-table
        v-loading="listLoading"
        row-key="id"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%"
        height="calc(100vh - 276px)"
      >
        <div slot="empty">
          <span v-if="listQuery.sectionId">没有数据</span>
          <span v-else>请选择标段</span>
        </div>
        <el-table-column label="设备名称" prop="deviceName" :show-overflow-tooltip="true" align="center" />
        <el-table-column label="报警开始时间" width="140" :show-overflow-tooltip="true" align="center">
          <template slot-scope="{ row }">
            {{ row.warningTime | datetime }}
          </template>
        </el-table-column>
        <el-table-column label="最新报警时间" width="140" :show-overflow-tooltip="true" align="center">
          <template slot-scope="{ row }">
            {{ row.lastWarningTime | datetime }}
          </template>
        </el-table-column>
        <el-table-column label="报警次数" prop="warningCount" width="80" align="center" />
        <el-table-column label="检测指标" prop="earlyWarningConfigurationBillName" :show-overflow-tooltip="true" align="center" />
        <el-table-column label="最大等级" prop="maxWarningLevelName" width="80" align="center" />
        <el-table-column label="最新等级" prop="lastWarningLevelName" width="80" align="center" />
        <el-table-column label="最新预警值" prop="lastWarningValue" width="120" align="center" />
        <el-table-column label="标准值" prop="standardValue" width="120" align="center" />
        <el-table-column
          label="操作"
          align="center"
          width="110"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="{ row }">
            <el-button
              v-if="$requirePermission('BSET.DVWR.U')"
              type="primary"
              size="mini"
              @click="showDetails(row)"
            >明细</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        :total="total"
        :skip-count.sync="listQuery.skipCount"
        :max-result-count.sync="listQuery.maxResultCount"
        @pagination="getList"
      />
    </div>
    <el-dialog
      :title="deviceName"
      :close-on-click-modal="false"
      :visible.sync="dialogDetailsVisible"
    >
      <DetailsDialog :warning-id="warningId" />
    </el-dialog>
  </div>
</template>

<script>

import * as interfaceApi from '@/api/device-warning.js'

import DetailsDialog from './DeviceWarningDetails.vue'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'DeviceWarningMng',
  directives: { waves },
  components: {
    Pagination,
    DetailsDialog
  },
  props: {
    query: { type: Object, default: null, required: false }
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: false,
      listQuery: {
        projectId: undefined,
        sectionId: undefined,
        deviceId: undefined,
        deviceNo: '',
        deviceName: '',
        startTime: undefined,
        endTime: undefined,
        deviceType: undefined,
        skipCount: 0,
        maxResultCount: 20
      },
      selectDate: '',
      deviceName: '',
      warningId: undefined,
      dialogDetailsVisible: false
    }
  },
  watch: {
    query: {
      deep: true,
      immediate: true,
      handler(val) {
        if (val) {
          this.listQuery.projectId = val.projectId
          this.listQuery.sectionId = val.sectionId
          this.listQuery.deviceId = val.deviceId
          this.deviceNo = val.deviceNoShow
          this.getList()
        }
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 清空下拉
    delValue(parameter) {
      this.listQuery[parameter] = undefined
    },
    // 获取数据列表
    getList() {
      if (!this.listQuery.sectionId) {
        this.list = []
        return
      }
      this.listLoading = true
      interfaceApi
        .getList(this.listQuery)
        .then(res => {
          this.list = res.items
          this.total = res.totalCount
        })
        .finally(() => {
          this.listLoading = false
        })
    },
    // 查询
    handleFilter() {
      this.listQuery.skipCount = 0
      this.getList()
    },
    selectDateChange(val) {
      if (val) {
        this.listQuery.startTime = val[0]
        this.listQuery.endTime = val[1]
      } else {
        this.listQuery.startTime = undefined
        this.listQuery.endTime = undefined
      }
      this.getList()
    },
    // 提示
    verificationTips(message) {
      this.$notify({
        title: '提示',
        message: message,
        type: 'error',
        duration: 2000
      })
    },
    showDetails(row) {
      this.warningId = row.id
      this.deviceName = row.deviceName
      this.dialogDetailsVisible = true
    }
  }
}
</script>

<style  scoped>
.number >>> input{
  text-align: left !important;
}
.el-selects >>> .el-input--suffix{
  width: 150px !important;
}
</style>
