Newer
Older
smartwell_front / src / views / home / rule / agreement / components / editDialog.vue
<!--
  Description: 产品管理-新建编辑
  Author: 李亚光
  Date: 2024-07-15
 -->
<script lang="ts" setup name="EditAgreement">
import type { FormRules } from 'element-plus'
import { ElMessage, ElMessageBox } from 'element-plus'
import { addAgreement, editAgreement } from '@/api/home/rule/agreement'
import { validateIPPort } from '@/utils/validate'
const emits = defineEmits(['refresh'])
const dataFormRef = ref()
const dialogFormVisible = ref(false) // 对话框是否显示
const dialogStatus = ref('') // 对话框类型:create,update
const dataForm = ref({
  protocolName: '', // 协议名称
  protocolCode: '', // 设备型号
  version: '', // 协议版本
  ip: '', // 上传地址
  description: '', // 备注
  id: '',
  ts: '',
}) // 表单
const textMap: { [key: string]: string } = {
  edit: '编辑',
  add: '新增',
} // 表头显示标题
const validateIp = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('上传地址不能为空'))
  }
  else if (!validateIPPort(value)) {
    callback(new Error('地址不合法'))
  }
  else {
    callback()
  }
}
const rules: FormRules = {
  protocolName: [{ required: true, message: '协议名称不能为空', trigger: ['blur', 'change'] }],
  protocolCode: [{ required: true, message: '协议编码不能为空', trigger: ['blur', 'change'] }],
  version: [{ required: true, message: '协议版本不能为空', trigger: ['blur', 'change'] }],
  ip: [{ required: true, validator: validateIp, trigger: ['blur', 'change'] }],
} // 前端校验规则

// 重置表单
const resetForm = () => {
  dataForm.value = {
    protocolName: '', // 协议名称
    protocolCode: '', // 设备型号
    version: '', // 协议版本
    ip: '', // 上传地址
    description: '', // 备注
    id: '',
    ts: '',
  }
}

// 初始化对话框
const initDialog = (dialogStatusValue: string, row: any) => {
  dialogStatus.value = dialogStatusValue
  dialogFormVisible.value = true
  if (dialogStatus.value === 'add') { // 如果是新增,清除验证
    resetForm()
    // nextTick(() => {
    //   dataFormRef.value.clearValidate()
    // })
    dataFormRef.value?.resetFields()
  }
  else if (dialogStatus.value === 'edit') { // 如果是修改,将row中数据填写到输入框中
    dataForm.value = {
      ...JSON.parse(JSON.stringify(row)),
    }
  }
}
defineExpose({
  initDialog,
})

// 新增数据
const createData = () => {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      addAgreement(dataForm.value).then((response) => {
        if (response.code === 200) {
          ElMessage({
            message: '添加成功',
            type: 'success',
          })
          // 通知父组件刷新状态
          dialogFormVisible.value = false
          setTimeout(() => {
            emits('refresh')
          })
        }
      }).catch((_) => { // 异常情况,loading置为false
      })
    }
  })
}

// 修改数据
const updateData = () => {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      editAgreement(dataForm.value).then((response) => {
        if (response.code === 200) {
          ElMessage({
            message: '修改成功',
            type: 'success',
          })
          setTimeout(() => {
            emits('refresh')
          })
          dialogFormVisible.value = false
        }
      }).catch((_) => { // 异常情况,loading置为false
      })
    }
  })
}

// 保存数据
const saveData = () => {
  if (dialogStatus.value === 'edit') {
    updateData()
  }
  else if (dialogStatus.value === 'add') {
    createData()
  }
}
const cancel = () => {
  dialogFormVisible.value = false
}
</script>

<template>
  <el-dialog v-model="dialogFormVisible" :title="`${textMap[dialogStatus]}协议`" append-to-body>
    <el-form ref="dataFormRef" :rules="rules" :model="dataForm" label-position="right" label-width="80px">
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="协议名称" prop="protocolName">
            <el-input
              v-model.trim="dataForm.protocolName" type="text" placeholder="协议名称" style="width: 100%;"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="协议编码" prop="protocolCode">
            <el-input
              v-model.trim="dataForm.protocolCode" type="text" placeholder="协议编码" style="width: 100%;"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="协议版本" prop="version">
            <el-input v-model.trim="dataForm.version" type="text" placeholder="协议版本" style="width: 100%;" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上传地址" prop="ip">
            <el-input v-model.trim="dataForm.ip" type="text" placeholder="上传地址" style="width: 100%;" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item label="备注" prop="description">
            <el-input v-model.trim="dataForm.description" type="text" placeholder="备注" style="width: 100%;" clearable />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="saveData">
          确认
        </el-button>
        <el-button @click="cancel">
          取消
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
.el-dialog {
  width: 700px;
}

.el-select {
  width: 100%;
}
</style>