Newer
Older
smartwell_front / src / views / systemConfig / alarmContent / components / editAlarmContent.vue
<template>
  <el-dialog
    append-to-body
    :title="textMap[dialogStatus]"
    :visible.sync="dialogFormVisible"
  >
    <el-form
      ref="dataForm"
      :rules="rules"
      :model="dataForm"
      label-width="100px"
      label-well-code="right"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="告警内容" prop="alarmContent">
            <el-input
              v-model.trim="dataForm.alarmContent"
              type="text"
              clearable
              placeholder="请输入告警内容"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="告警类型" prop="alarmType">
            <el-select
              v-model="dataForm.alarmType"
              placeholder="告警类型"
              clearable
            >
              <el-option
                v-for="item in alarmTypeList"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="设备类型" prop="deviceType">
            <!--            <el-select v-model="dataForm.deviceType" clearable multiple placeholder="请选择设备类型">-->
            <el-select v-model="dataForm.deviceType" clearable placeholder="请选择设备类型">
              <el-option
                v-for="item in deviceTypeList"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-show="dialogStatus === 'detail' ? true : false" :span="12">
          <el-form-item label="设备编号" prop="devcode">
            <el-input
              v-model.trim="dataForm.devcode"
              type="text"
              placeholder="未知"
              disabled
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div
      v-show="dialogStatus === 'detail' ? false : true"
      slot="footer"
      class="dialog-footer"
    >
      <el-button type="primary" :loading="btnLoading" @click="saveData">
        保存
      </el-button>
      <el-button @click="dialogFormVisible = false">
        取消
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDevice, updateAlarmContent, addAlarmContent } from '@/api/systemConfig/alarmContent'
import { getAlarmType } from '@/api/alarm/alarm'
export default {
  name: 'EditAlarmContent',
  data() {
    return {
      dialogFormVisible: false, // 对话框显示隐藏
      deviceTypeList: [], // 设备类型
      dialogStatus: '', // 对话框类型:create,update,detail
      // 告警类型列表
      alarmTypeList: [],
      // 对话框标题
      textMap: {
        update: '编辑',
        create: '新增',
        detail: '详情'
      },
      // 是否启用
      isValidList: [
        { label: '是', value: '1' },
        { label: '否', value: '0' }
      ],
      // 表单内容
      dataForm: {
        id: '',
        alarmContent: '',
        deviceType: '',
        alarmType: ''
      },
      btnLoading: false,
      // 表单校验规则
      rules: {
        deviceType: [{ required: true, message: '设备类型必选', trigger: ['blur', 'change'] }],
        alarmType: [{ required: true, message: '告警类型必选', trigger: ['blur', 'change'] }],
        alarmContent: [{ required: true, message: '告警内容不能为空', trigger: ['blur', 'change'] }]
      }
    }
  },
  created() {
    this.fetchDeviceType()
    this.fetchAlarmType()
  },
  methods: {
    // 获取设备类型
    fetchDeviceType() {
      getDevice().then(response => {
        this.deviceTypeList = response.data
      })
    },
    // 获取告警类型
    fetchAlarmType() {
      getAlarmType().then(response => {
        this.alarmTypeList = response.data
      })
    },
    // 重置表单
    resetForm() {
      this.dataForm = {
        id: '',
        alarmContent: '',
        deviceType: '',
        alarmType: ''
      }
    },
    // 父子组件通信
    initDialog(dialogStatus, row = null) {
      this.dialogFormVisible = true
      this.dialogStatus = dialogStatus
      if (this.dialogStatus === 'create') {
        this.resetForm()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (this.dialogStatus === 'update') {
        this.dataForm = {
          id: row.id,
          alarmContent: row.alarmContent,
          alarmType: row.alarmType + '',
          deviceType: row.deviceType + ''
        }
      }
    },
    // 保存数据
    saveData() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 修改数据
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const form = {
            id: this.dataForm.id,
            alarmContent: this.dataForm.alarmContent,
            deviceType: this.dataForm.deviceType,
            alarmType: this.dataForm.alarmType
          }
          this.btnLoading = true
          updateAlarmContent(form).then(response => {
            this.btnLoading = false
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          }).catch(_ => {
            this.btnLoading = false
          })
        }
      })
    },
    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const form = {
            alarmContent: this.dataForm.alarmContent,
            deviceType: this.dataForm.deviceType,
            alarmType: this.dataForm.alarmType
          }
          this.btnLoading = true
          addAlarmContent(form).then(response => {
            this.btnLoading = false
            if (response.code === 200) {
              this.$message.success('新增成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          }).catch(_ => {
            this.btnLoading = false
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .el-select{
    width: 100%;
  }
</style>