<template>
  <div class="app-container">
    <div class="btn-condition">
      <div class="condition-left" v-show="showSearch">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
        >
          <el-form-item label="设备名称" prop="deviceName">
            <el-input
              v-model="queryParams.deviceName"
              placeholder="请输入设备名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="报警类型" prop="alarmType">
            <el-select
              v-model="queryParams.alarmType"
              placeholder="选择报警类型"
              clearable
              filterable
            >
              <el-option
                v-for="dict in dict.type.alarm_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="事件类型" prop="eventType">
            <el-select
              v-model="queryParams.eventType"
              placeholder="选择事件类型"
              clearable
              filterable
            >
              <el-option
                v-for="dict in dict.type.alarm_event_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="报警等级" prop="alarmLevel">
            <el-select
              v-model="queryParams.alarmLevel"
              placeholder="选择报警等级"
              clearable
              filterable
            >
              <el-option
                v-for="dict in dict.type.alarm_level"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="报警内容" prop="content">
            <el-input
              v-model="queryParams.content"
              placeholder="请输入报警内容"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="处理状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="选择处理状态"
              clearable
              filterable
            >
              <el-option label="未处理" value="0" />
              <el-option label="已处理" value="1" />
            </el-select>
          </el-form-item>
          <el-form-item label="日期范围" prop="timeArr">
            <el-date-picker
              style="width: 220px"
              v-model="timeArr"
              type="daterange"
              unlink-panels
              range-separator="~"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
              @change="dateChange"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
        <div class="primary-btn">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
        </div>
        <div class="cancel-btn">
          <el-button
            type="cancel"
            icon="el-icon-refresh"
            size="mini"
            @click="resetQuery"
            >重置</el-button
          >
        </div>
        <!-- <div class="warning-btn" v-hasPermi="['system:post:export']">
          <el-button
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            >导出</el-button
          >
        </div> -->
      </div>
    </div>
    <el-table v-loading="loading" :data="postList" stripe>
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="仪表名称" align="center" prop="cpmName" />
      <el-table-column label="报警类型" align="center" prop="alarmType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.alarm_type"
            :value="scope.row.alarmType"
          />
        </template>
      </el-table-column>
      <el-table-column label="事件类型" align="center" prop="eventType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.alarm_event_type"
            :value="scope.row.eventType"
          />
        </template>
      </el-table-column>
      <el-table-column label="报警等级" align="center" prop="alarmLevel">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.alarm_level"
            :value="scope.row.alarmLevel"
          />
        </template>
      </el-table-column>
      <el-table-column label="报警内容" align="center" prop="content" />
      <el-table-column label="报警时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" align="center" prop="status">
        <template slot-scope="scope">
          <span
            :class="getStatusClass(scope.row.status)"
            v-if="scope.row.status === 0"
            >未处理</span
          >
          <span
            :class="getStatusClass(scope.row.status)"
            v-if="scope.row.status === 1"
            >已处理</span
          >
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['alarm:ard:edit']"
            >处理</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handleLook(scope.row)"
            v-hasPermi="['alarm:ard:query']"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 修改报警记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules">
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-form-item label="设备名称" prop="deviceName">
              <el-input
                v-model="form.deviceName"
                placeholder="请输入设备名称"
                readonly
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报警类型" prop="alarmType">
              <el-select
                v-model="form.alarmType"
                placeholder="请选择报警类型"
                disabled
              >
                <el-option
                  v-for="dict in dict.type.alarm_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-form-item label="事件类型" prop="eventType">
              <el-select
                v-model="form.eventType"
                placeholder="请选择事件类型"
                disabled
              >
                <el-option
                  v-for="dict in dict.type.alarm_event_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="报警等级" prop="alarmLevel">
              <el-select
                v-model="form.alarmLevel"
                placeholder="请选择报警等级"
                disabled
              >
                <el-option
                  v-for="dict in dict.type.alarm_level"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select> </el-form-item
          ></el-col>
        </el-row>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-form-item label="仪表名称" prop="cpmName">
              <el-input v-model="form.cpmName" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报警时间" prop="createTime">
              <el-input v-model="form.createTime" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="center">
          <el-col :span="24">
            <el-form-item label="报警内容" prop="content">
              <el-input v-model="form.content" type="textarea" readonly />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  alarmRecordList,
  getAlarmRecord,
  updateAlarmRecord,
} from "@/api/alarm/alarmRecord";

export default {
  name: "alarmRecord",
  dicts: ["alarm_type", , "alarm_type", "alarm_event_type", "alarm_level"],
  data() {
    return {
      timeArr: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 网关表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        alarmType: "",
        deviceName: "",
        eventType: "",
        alarmLevel: "",
        content: "",
        status: "",
        beginTime: "",
        endTime: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 表格动态类名
    getStatusClass(status) {
      switch (status) {
        case 0:
          return "status-green";
        case 1:
          return "status-red";
        default:
          return "";
      }
    },
    // 选中日期事件
    dateChange() {
      // console.log("打印时间", this.timeArr);
      if (!this.timeArr) {
        this.$nextTick(() => {
          this.timeArr = [];
        });
      }
    },
    /** 查询报警编码列表 */
    getList() {
      this.loading = true;
      alarmRecordList(this.addDateRange(this.queryParams, this.timeArr)).then(
        (response) => {
          this.postList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.form.id = "";
    },
    // 表单重置
    reset() {
      this.form = {
        msgCode: "",
        collectionType: "",
        eventType: "",
        alarmLevel: "",
        msgTitle: "",
        msgContent: "",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 详情 */
    handleLook(row) {
      this.reset();
      this.form.id = "";
      const acId = row.id || this.ids;
      console.log("");
      getAlarmRecord(acId).then((response) => {
        // this.form = response.data;
        this.form = {
          id: response.data.id,
          deviceName: response.data.deviceName,
          alarmType:
            response.data.alarmType !== null
              ? response.data.alarmType.toString()
              : null,
          eventType:
            response.data.eventType !== null
              ? response.data.eventType.toString()
              : null,
          alarmLevel:
            response.data.alarmLevel !== null
              ? response.data.alarmLevel.toString()
              : null,
          cpmName: response.data.cpmName,
          content: response.data.content,
          createTime: response.data.createTime,
        };
        this.open = true;
        this.title = "报警记录详情";
      });
    },
    /** 处理 */
    handleUpdate(row) {
      if (row.status === 0) {
        this.$confirm(`是否要处理该条报警记录?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            let data = {
              id: row.id,
              status: 1,
            };
            updateAlarmRecord(data).then((res) => {
              if (res.code == 200) {
                this.$modal.msgSuccess("处理成功！");
                this.getList();
              }
            });
          })
          .catch(() => {});
      } else if (row.status === 1) {
        this.$modal.msgWarning("该报警记录已被处理!");
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "device/gateway/export",
        {
          ...this.queryParams,
        },
        `post_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.status-green {
  color: red;
}
.status-red {
  color: rgb(7, 214, 110);
}
</style>
<style scoped lang="scss">
/* 媒体查询，适配大于2000px分辨率的大屏样式 - element样式 */
@media (max-width: 2000px) {
  ::v-deep .el-dialog {
    width: 700px !important;
  }

  ::v-deep .el-dialog .el-form-item__label {
    width: 140px !important;
  }
  ::v-deep .el-dialog .el-form-item {
    display: flex;
    flex-direction: row;
    align-items: center;
  }
}
@media (min-width: 2000px) {
  ::v-deep .el-dialog {
    width: 7.5rem !important;
  }

  ::v-deep .el-dialog .el-form-item__label {
    width: 1.4rem !important;
  }
  ::v-deep .el-dialog .el-form-item {
    display: flex;
    flex-direction: row;
    align-items: center;
  }
}
</style>
