<!-- 取消报警 -->
<template>
  <el-dialog :title="textMap[dialogStatus]" :close-on-click-modal="false" :visible.sync="dialogFormVisible" append-to-body>
    <el-form ref="dataForm" :rules="rules" :model="dataForm" label-well-code="right" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="取消报警原因" prop="reason">
            <el-radio-group v-model="dataForm.reason">
              <el-radio label="已解决" border>已解决</el-radio>
              <el-radio label="无需处理" border>无需处理</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model.trim="dataForm.remark" type="text" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="btnLoading" type="primary" @click="saveData">保存</el-button>
    </div>
  </el-dialog>
</template>

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

export default {
  name: 'AlarmCancel',
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update,detail
      dataForm: {
        id: '', // id
        reason: '', // 取消原因
        remark: '' // 备注
      }, // 表单
      rules: {
        reason: [{ required: true, message: '消警原因不能为空', trigger: ['blur', 'change'] }]
      }, // 前端校验规则
      textMap: {
        detail: '取消报警'
      }, // 表头显示标题
      btnLoading: true // 保存按钮是否不允许点击
    }
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.btnLoading = false
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible

      if (dialogStatus === 'detail') {
        // 清除
        this.dataForm = {
          id: row.id,
          reason: '',
          remark: ''
        }
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      }
    },
    // 保存数据
    saveData: function() {
      this.btnLoading = true

      this.updateData()
    },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          alarmCancel(this.dataForm).then(response => {
            if (response.code === 200) {
              this.$message.success('消警成功')

              // 关闭对话框，并刷新列表的记录
              this.dialogFormVisible = false
              this.$emit('refreshList')
            } else {
              this.btnLoading = false
              this.$message.error(response.message)
            }
          }).catch(() => {
            this.btnLoading = false
          })
        } else {
          this.btnLoading = false
        }
      })
    }
  }
}
</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>
