<template>
  <el-dialog
    :visible.sync="dialogVisible"
    append-to-body
    title="取消告警"
    width="30%"
    @close="closeDialog"
  >
    <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="告警取消原因" prop="jobStatus">
        <el-select v-model="formData.jobStatus" placeholder="请选择">
          <el-option
            v-for="item in closeReasons"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="handleMessage">
        <el-input
          v-model.trim="formData.handleMessage"
          type="textarea"
          placeholder="请输入详细的告警取消原因"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="cancelAlarm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { batchCancel, cancelAlarm } from '@/api/alarm/alarm'

export default {
  name: 'HandlePrompt',
  data() {
    return {
      formData: {
        keywords: '', // 批量消警查询条件——关键字
        alarmType: '', // 批量消警查询条件——报警类型
        alarmContent: '', // 批量消警查询条件——报警内容
        beginTime: '', // 批量消警查询条件——开始时间
        endTime: '', // 批量消警查询条件——结束时间
        id: '', // 工单id
        jobStatus: '', // 工单状态
        handleMessage: '' // 原因
      },
      isBatch: false, // 是不是批量删除
      dialogVisible: false,
      closeReasons: [{
        value: '3',
        label: '已处理' // 已完成
      }, {
        value: '4',
        label: '无需处理' // 已取消
      }],
      rules: {
        jobStatus: [{ required: true, message: '关闭原因必选', trigger: ['blur', 'change'] }],
        handleMessage: [{ required: true, message: '备注不得为空', trigger: ['blur', 'change'] },
          { min: 1, max: 30, message: '长度不得超过30字', trigger: 'blur' }]
      },
      value: ''
    }
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogFormVisible, jobId, isBatch, params) {
      debugger
      this.dialogVisible = dialogFormVisible
      this.isBatch = isBatch
      if (isBatch) {
        this.formData.keywords = params.keywords
        this.formData.alarmType = params.alarmType
        this.formData.alarmContent = params.alarmContentType
        this.formData.beginTime = params.beginTime
        this.formData.endTime = params.endTime
      } else {
        this.formData.id = jobId
      }
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.closeDialog()
        })
        .catch(_ => {})
    },
    // 取消报警,调用结束工单接口
    cancelAlarm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.$confirm(
            '确定要取消报警吗？',
            '确认操作',
            {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }
          ).then(() => {
            // 批量消警接口
            if (this.isBatch) {
              batchCancel(this.formData).then(response => {
                this.$message.success('批量消警成功')
                this.closeDialog()
              })
            } else { // 单个消警接口
              const formData = {
                id: this.formData.id,
                jobStatus: this.formData.jobStatus, // 工单状态
                handleMessage: this.formData.handleMessage // 原因
              }
              cancelAlarm(formData).then(response => {
                this.$message.success('取消报警成功')
                this.closeDialog()
              })
            }
          })
        }
      })
    },
    closeDialog() {
      this.dialogVisible = false
      this.$emit('watchChild')
      this.formData.jobStatus = ''
      this.formData.handleMessage = ''
      // 清除验证
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    }
  }
}
</script>

<style scoped>

</style>
