<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="设备分组ID，关联t_equipment_group" prop="equipmentGroupId">
      <el-input v-model="dataForm.equipmentGroupId" placeholder="设备分组ID，关联t_equipment_group"></el-input>
    </el-form-item>
    <el-form-item label="编码" prop="code">
      <el-input v-model="dataForm.code" placeholder="编码"></el-input>
    </el-form-item>
    <el-form-item label="报警类型 字典表关联label_value" prop="typeDictValue">
      <el-input v-model="dataForm.typeDictValue" placeholder="报警类型 字典表关联label_value"></el-input>
    </el-form-item>
    <el-form-item label="报警级别 字典表关联label_value" prop="gradeDictValue">
      <el-input v-model="dataForm.gradeDictValue" placeholder="报警级别 字典表关联label_value"></el-input>
    </el-form-item>
    <el-form-item label="报警内容" prop="alarmContent">
      <el-input v-model="dataForm.alarmContent" placeholder="报警内容"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="description">
      <el-input v-model="dataForm.description" placeholder="描述"></el-input>
    </el-form-item>
    <el-form-item label="外部编码，用于对照外部系统的编码" prop="externalCode">
      <el-input v-model="dataForm.externalCode" placeholder="外部编码，用于对照外部系统的编码"></el-input>
    </el-form-item>
    <el-form-item label="启用状态:0 、停用，1、启用" prop="enabled">
      <el-input v-model="dataForm.enabled" placeholder="启用状态:0 、停用，1、启用"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="删除标志，是否有效:1 可用 0不可用" prop="valid">
      <el-input v-model="dataForm.valid" placeholder="删除标志，是否有效:1 可用 0不可用"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="creatorId">
      <el-input v-model="dataForm.creatorId" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="创建人姓名" prop="creatorName">
      <el-input v-model="dataForm.creatorName" placeholder="创建人姓名"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新人" prop="updaterId">
      <el-input v-model="dataForm.updaterId" placeholder="更新人"></el-input>
    </el-form-item>
    <el-form-item label="更新人姓名" prop="updaterName">
      <el-input v-model="dataForm.updaterName" placeholder="更新人姓名"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="版本号" prop="version">
      <el-input v-model="dataForm.version" placeholder="版本号"></el-input>
    </el-form-item>
    <el-form-item label="推送方式 0邮箱 1微信" prop="alarmNotify">
      <el-input v-model="dataForm.alarmNotify" placeholder="推送方式 0邮箱 1微信"></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: 0,
          equipmentGroupId: '',
          code: '',
          typeDictValue: '',
          gradeDictValue: '',
          alarmContent: '',
          description: '',
          externalCode: '',
          enabled: '',
          remark: '',
          valid: '',
          creatorId: '',
          creatorName: '',
          createTime: '',
          updaterId: '',
          updaterName: '',
          updateTime: '',
          version: '',
          alarmNotify: ''
        },
        dataRule: {
          equipmentGroupId: [
            { required: true, message: '设备分组ID，关联t_equipment_group不能为空', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '编码不能为空', trigger: 'blur' }
          ],
          typeDictValue: [
            { required: true, message: '报警类型 字典表关联label_value不能为空', trigger: 'blur' }
          ],
          gradeDictValue: [
            { required: true, message: '报警级别 字典表关联label_value不能为空', trigger: 'blur' }
          ],
          alarmContent: [
            { required: true, message: '报警内容不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ],
          externalCode: [
            { required: true, message: '外部编码，用于对照外部系统的编码不能为空', trigger: 'blur' }
          ],
          enabled: [
            { required: true, message: '启用状态:0 、停用，1、启用不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          valid: [
            { required: true, message: '删除标志，是否有效:1 可用 0不可用不能为空', trigger: 'blur' }
          ],
          creatorId: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          creatorName: [
            { required: true, message: '创建人姓名不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updaterId: [
            { required: true, message: '更新人不能为空', trigger: 'blur' }
          ],
          updaterName: [
            { required: true, message: '更新人姓名不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本号不能为空', trigger: 'blur' }
          ],
          alarmNotify: [
            { required: true, message: '推送方式 0邮箱 1微信不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/monitoring/equipmengroupalarm/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.equipmentGroupId = data.equipmengroupAlarm.equipmentGroupId
                this.dataForm.code = data.equipmengroupAlarm.code
                this.dataForm.typeDictValue = data.equipmengroupAlarm.typeDictValue
                this.dataForm.gradeDictValue = data.equipmengroupAlarm.gradeDictValue
                this.dataForm.alarmContent = data.equipmengroupAlarm.alarmContent
                this.dataForm.description = data.equipmengroupAlarm.description
                this.dataForm.externalCode = data.equipmengroupAlarm.externalCode
                this.dataForm.enabled = data.equipmengroupAlarm.enabled
                this.dataForm.remark = data.equipmengroupAlarm.remark
                this.dataForm.valid = data.equipmengroupAlarm.valid
                this.dataForm.creatorId = data.equipmengroupAlarm.creatorId
                this.dataForm.creatorName = data.equipmengroupAlarm.creatorName
                this.dataForm.createTime = data.equipmengroupAlarm.createTime
                this.dataForm.updaterId = data.equipmengroupAlarm.updaterId
                this.dataForm.updaterName = data.equipmengroupAlarm.updaterName
                this.dataForm.updateTime = data.equipmengroupAlarm.updateTime
                this.dataForm.version = data.equipmengroupAlarm.version
                this.dataForm.alarmNotify = data.equipmengroupAlarm.alarmNotify
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/monitoring/equipmengroupalarm/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'equipmentGroupId': this.dataForm.equipmentGroupId,
                'code': this.dataForm.code,
                'typeDictValue': this.dataForm.typeDictValue,
                'gradeDictValue': this.dataForm.gradeDictValue,
                'alarmContent': this.dataForm.alarmContent,
                'description': this.dataForm.description,
                'externalCode': this.dataForm.externalCode,
                'enabled': this.dataForm.enabled,
                'remark': this.dataForm.remark,
                'valid': this.dataForm.valid,
                'creatorId': this.dataForm.creatorId,
                'creatorName': this.dataForm.creatorName,
                'createTime': this.dataForm.createTime,
                'updaterId': this.dataForm.updaterId,
                'updaterName': this.dataForm.updaterName,
                'updateTime': this.dataForm.updateTime,
                'version': this.dataForm.version,
                'alarmNotify': this.dataForm.alarmNotify
              })
            }).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>
