Newer
Older
smartcity_env_front / src / views / alarmRule / editAlarmRule.vue
StephanieGitHub on 27 Jul 2021 6 KB MOD:设备管理、报警阈值管理
<!--报警规则编辑-->
<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :fullscreen="false" width="60%" append-to-body>
    <el-form ref="dataForm" :rules="rules" :model="ruleForm" label-well-code="right" label-width="100px">
      <div class="form-div">
        <div class="form-left">
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="设备编号" prop="deviceNo">
                <el-input v-model.trim="ruleForm.deviceNo" disabled type="text"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="参数名称" prop="sensorName">
                <el-input v-model.trim="ruleForm.sensorName" disabled type="text"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="报警阈值" prop="alarmThreshold">
                <el-input v-model.trim="ruleForm.alarmThreshold" :disabled="isDetailMode" type="text" placeholder="报警阈值">
                  <template slot="append">{{ruleForm.unit}}</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="报警开关" prop="alarmOpen">
                <el-switch
                  v-model="ruleForm.alarmOpen"
                  active-value="1"
                  inactive-value="0">
                </el-switch>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </div>

    </el-form>
    <div v-show="!isDetailMode" slot="footer" class="dialog-footer">
      <el-button :disabled="!canEdit" type="primary" @click="saveData">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
    <!--地图选点-->
  </el-dialog>
</template>

<script>
import { addDevice, updateDevice } from '@/api/environment/device'
export default {
  name: 'EditAlarmRule',
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型:create,update,detail
      ruleForm: {
        id: '', // id
        unit:'', // 单位
        deviceNo: '', // 编号
        sensor: '', // 参数
        sensorName: '', // 参数名称
        alarmThreshold: '', // 详细地址
        alarmOpen: '', // 经度
      }, // 表单
      textMap: {
        update: '修改规则',
        create: '新增规则',
        detail: '详情'
      }, // 表头显示标题
      areaList: [],
      rules: {
        alarmThreshold: [{ required: true, message: '报警阈值不能为空', trigger: ['blur', 'change'] }],
        alarmOpen: [{ required: true, message: '报警开关不能为空', trigger: ['blur', 'change'] }],
      }, // 前端校验规则
      isEditMode: false, // 编辑模式
      isDetailMode: false, // 详情模式
      canEdit: true
    }
  },
  created() {
  },
  methods: {
    /**
     * 初始化对话框
     * @param dialogStatus 对话框类型
     * @param dialogFormVisible 对话框是否可见
     * @param row 内容参数(编辑和详情需要)
     */
    initDialog: function(dialogStatus, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = true
      if (dialogStatus === 'create') { // 如果是新增,清除验证
        this.isEditMode = false
        this.isDetailMode = false
        this.ruleForm = {
          deviceNo: row.deviceNo, // 设备编号
          sensorName: row.sensorName, // 参数名称
          sensor: row.sensor, // 参数编码
          unit: row.unit, // 单位
          alarmThreshold: '', // 报警阈值
          alarmOpen: '1'
        }
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (dialogStatus === 'update') { // 如果是修改,将row中数据填写到输入框中
        this.ruleForm = {
          id: row.id, // id
          deviceNo: row.deviceNo, // 设备编号
          sensorName: row.sensorName, // 参数名称
          sensor: row.sensor, // 参数编码
          unit: row.unit, // 单位
          alarmThreshold: row.alarmThreshold, // 报警阈值
          alarmOpen: row.alarmOpen
        }
        this.isEditMode = true
        this.isDetailMode = false
      } else {
        this.ruleForm = {
          id: row.id, // id
          deviceNo: row.deviceNo, // 设备编号
          sensorName: row.sensorName, // 参数名称
          sensor: row.sensor, // 参数编码
          unit: row.unit, // 单位
          alarmThreshold: row.alarmThreshold, // 报警阈值
          alarmOpen: row.alarmOpen
        }
        this.isEditMode = true
        this.isDetailMode = true
      }
    },
    // 保存数据
    saveData: function() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 新增数据
    createData: function() {
      this.canEdit = false
      this.$refs['dataForm'].validate((valid) => {
        console.log(this.ruleForm)
        if (valid) {
          addDevice(this.ruleForm).then(response => {
            if (response.code === 200) {
              this.$confirm('新增成功,是否继续新增?', '提示', {
                confirmButtonText: '是',
                cancelButtonText: '否',
                type: 'info'
              }).then(() => {
                this.resetForm()
                this.$nextTick(() => {
                  this.$refs['dataForm'].clearValidate()
                  this.canEdit = true
                })
              }).catch(() => {
                this.$emit('watchChild')
                this.dialogFormVisible = false
                this.canEdit = true
              })
            }
          }).catch((e) => {
            this.canEdit = true
          })
        } else {
          this.canEdit = true
        }
      })
    },
    // 修改数据
    updateData: function() {
      this.canEdit = false
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateDevice(this.ruleForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
              this.canEdit = true
            }
          }).catch((e) => {
            this.canEdit = true
          })
        } else {
          this.canEdit = true
        }
      })
    },
    cancel: function() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .el-select{
    width: 100%;
  }
  .el-date-editor{
    width: 100%;
  }
  .form-div{
    width:100%;
    height:100%;
    display:flex;
    justify-content: space-between;
  }
  .form-left{
    flex:1;
    height:100%;
  }
  .form-right{
    width: 0px;
    height:100%;
    .avatar{
      margin-bottom: 10px;
    }
    text-align: center;
  }
</style>