Newer
Older
smartwell_front / src / views / home / well / components / editDialog.vue
liyaguang on 9 Jan 3 KB ‘设备运维’
<!--
  Description: 闸井监测-闸井编辑
  Author: 李亚光
  Date: 2024-09-05
 -->
<script lang="ts" setup name="EditProduct">
import type { FormRules } from 'element-plus'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getDictByCode } from '@/api/system/dict'
import { editWell } from '@/api/home/well/well'
const emits = defineEmits(['refresh'])
const dataFormRef = ref()
const dialogFormVisible = ref(false) // 对话框是否显示
const dialogStatus = ref('') // 对话框类型:create,update
const dataForm = ref<{ [key: string]: string }>({
  id: '',
  marker: '', // 使用状态
}) // 表单
const textMap: { [key: string]: string } = {
  edit: '编辑',
  add: '新增',
} // 表头显示标题
const rules: FormRules = {
  // tagNumber: [{ required: true, message: '闸井位号不能为空', trigger: ['blur', 'change'] }],
} // 前端校验规则

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

// 修改数据
const updateData = () => {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      const data = {
        id: '',
        marker: '', // 标签
      } as { [key: string]: string }
      for (const i in data) {
        data[i] = dataForm.value[i]
      }
      disabledBtn.value = true
      editWell(data).then((response) => {
        if (response.code === 200) {
          ElMessage({
            message: '修改成功',
            type: 'success',
          })
          setTimeout(() => {
            emits('refresh')
          })
          disabledBtn.value = false
          dialogFormVisible.value = false
        }
      }).catch((_) => { // 异常情况,loading置为false
      })
    }
  })
}

// 保存数据
const saveData = () => {
  if (dialogStatus.value === 'edit') {
    updateData()
  }
}
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="24">
          <el-form-item label="标签" prop="marker">
            <el-input
              v-model.trim="dataForm.marker" type="textarea" :rows="3" placeholder="标签" style="width: 100%;"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button v-loaing="disabledBtn" :disabled="disabledBtn" 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>