<!--
 * @Author: zwq
 * @Date: 2021-11-18 14:16:25
 * @LastEditors: gtz
 * @LastEditTime: 2021-12-13 17:01:47
 * @Description:
-->
<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" v-loading="formLoading" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="报警编码" prop="code">
        <el-input v-model="dataForm.code" placeholder="报警编码"></el-input>
      </el-form-item>
      <el-form-item label="报警类型" prop="alarmClass">
        <el-input v-model="dataForm.alarmClass" placeholder="报警类型"></el-input>
      </el-form-item>
      <el-form-item label="报警内容" prop="alarmInfo">
        <el-input v-model="dataForm.alarmInfo" placeholder="报警内容"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: '',
          code: '',
          alarmClass: '',
          alarmInfo: ''
        },
        dataRule: {
          code: [
            { required: true, message: '报警编码不能为空', trigger: 'blur' }
          ],
          alarmInfo: [
            { required: true, message: '报警内容不能为空', trigger: 'blur' }
          ]
        },
        formLoading: false
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.formLoading = true
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/alarmBase/get`),
              method: 'post',
              data: this.$http.adornData({id})
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.data
              } else {
                this.$message.error(data.msg)
              }
              this.formLoading = false
            })
          } else {
            this.$http({
              url: this.$http.adornUrl(`/alarmBase/codeGenerator`),
              method: 'post',
              data: this.$http.adornData()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.code = data.data
              } else {
                this.$message.error('编码生成失败')
              }
              this.formLoading = false
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/alarmBase/${!this.dataForm.id ? 'add' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'code': this.dataForm.code,
                'alarmClass': this.dataForm.alarmClass,
                'alarmInfo': this.dataForm.alarmInfo
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>