<!-- 报警详情 -->
<template>
  <el-dialog :title="textMap[dialogStatus]" :close-on-click-modal="false" :visible.sync="dialogFormVisible" append-to-body>
    <el-form ref="dataForm" :model="dataForm" label-well-code="right" label-width="110px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="报警编号">
            <el-input v-model.trim="dataForm.alarmCode" :disabled="isDetailMode" type="text" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="报警时间">
            <el-input v-model.trim="dataForm.alarmTime" :disabled="isDetailMode" type="text" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="报警状态">
            <el-input v-model.trim="dataForm.validStr" :disabled="isDetailMode" type="text" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="区域名称">
            <el-input v-model.trim="dataForm.areaPosition" :disabled="isDetailMode" type="text" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="报警内容">
            <el-input v-model.trim="dataForm.alarmContent" :disabled="isDetailMode" type="text" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-show="dataForm.alarmValue !== ''" label="报警数值">
            <el-input v-model.trim="dataForm.alarmValueStr" :disabled="isDetailMode" type="text" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="设备编号">
            <el-input v-model.trim="dataForm.devcode" :disabled="isDetailMode" type="text" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备名称">
            <el-input v-model.trim="dataForm.devName" :disabled="isDetailMode" type="text" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="所属防护">
            <el-input v-model.trim="dataForm.areaType" :disabled="isDetailMode" type="text" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row v-show="!dataForm.valid" :gutter="20">
        <el-col :span="12">
          <el-form-item label="取消报警原因">
            <el-input v-model.trim="dataForm.reason" :disabled="isDetailMode" type="text" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row v-show="!dataForm.valid" :gutter="20">
        <el-col :span="24">
          <el-form-item label="备注">
            <el-input v-model.trim="dataForm.remark" :disabled="isDetailMode" type="text" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

  </el-dialog>
</template>

<script>
import { alarmDetail } from '@/api/alarm'

export default {
  name: 'AlarmDetail',
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update,detail
      dataForm: {
        id: '', // id
        alarmCode: '', // 报警编号
        alarmTime: '', // 报警事件
        valid: '', // 报警状态
        validStr: '', // 报警状态中文
        areaPosition: '', // 区域名称
        alarmContent: '', // 报警内容
        alarmValue: '', // 报警值
        alarmValueStr: '', // 报警值格式化
        devcode: '', // 设备编号
        devName: '', // 设备名
        areaType: '', // 所属防护
        reason: '', // 取消报警原因
        remark: '' // 备注
      }, // 表单
      textMap: {
        detail: '报警详情'
      }, // 表头显示标题
      isDetailMode: false // 是否详情模式
    }
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible

      if (dialogStatus === 'detail') {
        this.isDetailMode = true

        this.dataForm = {
          id: row.id
        }

        alarmDetail(this.dataForm.id).then(response => {
          if (response.code === 200) {
            this.dataForm = {
              id: row.id,
              alarmCode: response.data.alarmCode,
              alarmTime: response.data.alarmTimeFmt,
              areaPosition: response.data.areaPosition,
              alarmContent: response.data.alarmContent,
              alarmValue: response.data.alarmValue,
              valid: response.data.valid,
              devcode: response.data.devcode,
              devName: response.data.devName,
              reason: response.data.reson,
              remark: response.data.remark
            }

            // 格式化报警状态
            if (response.data.valid === 1) {
              this.dataForm.validStr = '正在报警'
            } else {
              this.dataForm.validStr = '已消警'
            }

            // 格式化报警数值
            if (response.data.alarmContent.indexOf('温度') >= 0) {
              this.dataForm.alarmValueStr = response.data.alarmValue + '（℃）'
            } else if (response.data.alarmContent.indexOf('湿度') >= 0) {
              this.dataForm.alarmValueStr = response.data.alarmValue + '（%）'
            }
          }
        })
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .el-select{
    width: 100%;
  }
  .el-date-editor{
    width: 100%;
  }
  .dialog-footer {
    margin-top: -20px;
    text-align: center;
  }
</style>
