Newer
Older
smartwell_front / src / views / home / ledger / pipeline / components / editDialog.vue
<!--
  Description: 管线管理新建编辑
  Author: 李亚光
  Date: 2024-09-02
 -->
<script lang="ts" setup name="EditPiePle">
import type { FormRules } from 'element-plus'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getDictByCode } from '@/api/system/dict'
import { addPieple, editPieple } from '@/api/home/ledger/pipeline'
const emits = defineEmits(['refresh'])
const dataFormRef = ref()
const dialogFormVisible = ref(false) // 对话框是否显示
const dialogStatus = ref('') // 对话框类型:create,update
const dataForm = ref({
  pipeCode: '', // 管线编号
  position: '', // 管线位置
  pressType: '', // 压力级制
  material: '', // 材质
  pipeDiameter: '', // 管径(mm)
  constructEra: '', // 建设年代
  deptid: '', // 单位
  manageType: '', // 管理方式
}) // 表单
const textMap: { [key: string]: string } = {
  edit: '编辑',
  add: '新增',
} // 表头显示标题
const rules: FormRules = {
  pipeCode: [{ required: true, message: '管线编号不能为空', trigger: ['blur', 'change'] }],
  position: [{ required: true, message: '管线位置不能为空', trigger: ['blur', 'change'] }],
  pressType: [{ required: true, message: '压力级制不能为空', trigger: ['blur', 'change'] }],
  deptid: [{ required: true, message: '管理单位不能为空', trigger: ['blur', 'change'] }],
  manageType: [{ required: true, message: '管理方式不能为空', trigger: ['blur', 'change'] }],
} // 前端校验规则

// 重置表单
const resetForm = () => {
  dataForm.value = {
    pipeCode: '', // 管线编号
    position: '', // 管线位置
    pressType: '', // 压力级制
    material: '', // 材质
    pipeDiameter: '', // 管径(mm)
    constructEra: '', // 建设年代
    deptid: '', // 单位
    manageType: '', // 管理方式
  }
}

// 初始化对话框
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) {
      addPieple(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) {
      editPieple(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
}

// 获取字典
const pressTypeList = ref<{ id: string; name: string; value: string }[]>([]) // 压力级制
const manageTypeList = ref<{ id: string; name: string; value: string }[]>([]) // 管理方式
const fetchDict = () => {
  // 压力级制
  getDictByCode('pressType').then((res) => {
    pressTypeList.value = res.data
  })
  // 管理方式
  getDictByCode('manageType').then((res) => {
    manageTypeList.value = res.data
  })
}
fetchDict()
</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="pipeCode">
            <el-input v-model.trim="dataForm.pipeCode" type="text" placeholder="管线编号" style="width: 100%;" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="管线位置" prop="position">
            <el-input v-model.trim="dataForm.position" 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="pressType">
            <el-select v-model="dataForm.pressType" placeholder="压力级制" clearable style="width: 100%;">
              <el-option v-for="item in pressTypeList" :key="item.id" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="材质" prop="material">
            <el-input v-model.trim="dataForm.material" 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="管径(mm)" prop="pipeDiameter">
            <el-input v-model.trim="dataForm.pipeDiameter" type="number" placeholder="管径(mm)" style="width: 100%;" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="建设年代" prop="constructEra">
            <el-input v-model.trim="dataForm.constructEra" type="number" 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="deptid">
            <dept-select v-model="dataForm.deptid" placeholder="管理单位" :clearable="true" style="width: 100%;" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产权单位" prop="deptid">
            <el-input v-model.trim="dataForm.deptid" 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="manageType">
            <el-select v-model="dataForm.manageType" placeholder="管理方式" clearable style="width: 100%;">
              <el-option v-for="item in manageTypeList" :key="item.id" :label="item.name" :value="item.value" />
            </el-select>
          </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>