<template>
  <el-container class="content-height-fit">
    <inner-sidebar>
      <!-- <ProjectSectionTree @node-click="handleTreeClick" /> -->
      <project-section-device-tree @node-click="handleTreeClick" />
    </inner-sidebar>
    <el-container class="resize-fit-container">
      <div class="app-container">
        <div class="filter-container blue-top-border">
          <!-- <el-select
            v-model="listQuery.deviceType"
            placeholder="设备类型"
            class="filter-item el-selects"
            clearable
            @clear="delValue('deviceType')"
            @change="getList"
          >
            <el-option
              v-for="item in deviceTypes"
              :key="item.key"
              :value="item.key"
              :label="item.name"
            />
          </el-select>
          <el-input
            v-model="listQuery.deviceName"
            placeholder="设备名称"
            class="filter-item"
            clearable
            @change="getList"
          /> -->
          <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="100" align="center" />
            <el-table-column label="标准值" prop="standardValue" width="80" align="center" />
            <el-table-column label="生效标识" width="100" align="center">
              <template slot-scope="{ row }">
                <audit-status-label :audit="row" />
              </template>
            </el-table-column>
            <el-table-column label="审核进度" align="center" width="80">
              <template slot-scope="{ row }">
                <audit-progress-label :audit="row" @show_progress="handleProgress" />
              </template>
            </el-table-column>
            <el-table-column label="生效日期" width="100" :show-overflow-tooltip="true" align="center">
              <template slot-scope="{ row }">
                {{ row.submitDate | date }}
              </template>
            </el-table-column>
            <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>
                <el-button
                  v-if="$requirePermission('BSET.DVWR.A') && row.canRevoke"
                  size="mini"
                  type="danger"
                  @click="handleRevoke(row)"
                >撤销处理</el-button>
                <el-button
                  v-if="$requirePermission('BSET.DVWR.A') && row.auditStatus == 1"
                  type="danger"
                  size="mini"
                  @click="handleAudit(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>

      </div>
      <audit-progress-dialog v-model="showAuditProgressDialog" :progress-id="entityWarning.auditProgressId" />
      <audit-dialog v-model="showAuditDialog" :progress-id="entityWarning.auditProgressId" @audited="handleAuditCallback" />
      <el-dialog
        :title="deviceName"
        :close-on-click-modal="false"
        :visible.sync="dialogDetailsVisible"
      >
        <DetailsDialog :warning-id="warningId" />
      </el-dialog>
    </el-container>
  </el-container>
</template>

<script>
import * as interfaceApi from '@/api/device-warning.js'
// import ProjectSectionTree from '@/views/projects/components/ProjectSectionTree'
import ProjectSectionDeviceTree from '@/views/projects/components/ProjectSectionDeviceTree'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import InnerSidebar from '@/components/InnerSidebar'

import DetailsDialog from './details.vue'
import AuditProgressDialog from '@/views/sys/audit-flow/components/AuditProgressDialog'
import AuditStatusLabel from '@/views/sys/audit-flow/components/AuditStatusLabel'
import AuditDialog from '@/views/sys/audit-flow/components/AuditDialog'
import AuditProgressLabel from '@/views/sys/audit-flow/components/AuditProgressLabel'
export default {
  name: 'DeviceWarningMng',
  directives: { waves },
  components: { Pagination,
    // ProjectSectionTree,
    ProjectSectionDeviceTree, InnerSidebar, DetailsDialog,
    AuditProgressDialog,
    AuditStatusLabel,
    AuditProgressLabel,
    AuditDialog },
  data() {
    return {
      list: [],
      deviceTypes: [
        { key: 1, name: '架桥机' },
        { key: 2, name: '运梁车' }
      ],
      total: 0,
      listLoading: false,
      listQuery: {
        projectId: undefined,
        sectionId: undefined,
        deviceNo: '',
        deviceName: '',
        startTime: undefined,
        endTime: undefined,
        deviceType: undefined,
        skipCount: 0,
        maxResultCount: 20
      },
      selectDate: '',
      entityWarning: {
        auditProgressId: undefined
      },
      showAuditDialog: false,
      showAuditProgressDialog: false,
      deviceName: '',
      warningId: undefined,
      dialogDetailsVisible: false
    }
  },
  watch: {
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 树形事件
    handleTreeClick(data, node) {
      // shortName
      this.listQuery.projectId = undefined
      this.listQuery.sectionId = undefined
      this.listQuery.deviceId = undefined
      if (data.isProject) {
        this.listQuery.projectId = data.id
        this.listQuery.sectionId = undefined
      }
      if (data.isSection) {
        this.listQuery.projectId = data.projectId
        this.listQuery.sectionId = data.id
      }
      if (data.isDevice) {
        this.listQuery.projectId = data.projectId
        this.listQuery.sectionId = data.sectionId
        this.listQuery.deviceId = data.id
      }
      this.getList()
    },

    // 清空下拉
    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
      })
    },
    handleProgress(row) {
      this.entityWarning = row
      this.showAuditProgressDialog = true
    },
    handleAudit(row) {
      this.entityWarning = row
      this.showAuditDialog = true
    },
    handleAuditCallback() {
      this.getList()
      // 审核通过执行
      // this.audited()
    },
    handleRevoke(row) {
      this.$confirm('是否撤回提交?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        interfaceApi.revoke(row.id).then(() => {
          this.getList()
          this.$notify({
            title: '成功',
            message: '撤回成功',
            type: 'success',
            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>
