<template>
|
<div>
|
<el-dialog
|
v-model="dialogVisible"
|
:title="title"
|
width="500"
|
:close-on-click-modal="false"
|
:before-close="handleClose"
|
> <el-form
|
ref="formRef"
|
style="max-width: 600px; margin: 0 auto"
|
:model="formData"
|
:rules="rules"
|
label-width="120px"
|
>
|
<el-form-item label="煤种名称" prop="coal">
|
<el-input
|
v-model="formData.coal"
|
placeholder="请输入煤种名称"
|
/>
|
</el-form-item> <el-form-item label="维护人姓名" prop="maintainerId">
|
<el-input
|
:value="userStore.name || ''"
|
placeholder="维护人姓名"
|
disabled
|
/>
|
</el-form-item> <el-form-item label="维护日期" prop="maintenanceDate">
|
<el-input
|
:value="getCurrentDate()"
|
placeholder="维护日期"
|
disabled
|
/>
|
</el-form-item>
|
|
<el-form-item class="dialog-footer">
|
<el-button v-if="addOrEdit === 'edit'" @click="resetForm">重置</el-button>
|
<el-button v-if="addOrEdit === 'add'" @click="cancelForm">取消</el-button>
|
<el-button type="primary" @click="submitForm">
|
确定
|
</el-button>
|
</el-form-item>
|
</el-form>
|
</el-dialog>
|
</div>
|
</template>
|
|
<script setup>
|
import { ref, watch, defineProps, reactive, onMounted } from 'vue'
|
import { addOrEditCoalInfo } from '@/api/basicInformation/coal'
|
import useUserStore from '@/store/modules/user'
|
|
const userStore = useUserStore()
|
|
const props = defineProps({
|
beforeClose: {
|
type: Function,
|
default: () => {}
|
},
|
form: {
|
type: Object,
|
default: () => ({})
|
},
|
addOrEdit: {
|
type: String,
|
default: 'add'
|
},
|
title: {
|
type: String,
|
default: ''
|
},
|
})
|
const copyForm = defineModel("copyForm", {
|
required: true,
|
type: Object,
|
});
|
// 在组件挂载时获取用户信息
|
onMounted(async () => {
|
// 如果store中没有用户信息,则获取用户信息
|
if (!userStore.name) {
|
try {
|
await userStore.getInfo()
|
// 自动填充维护人ID
|
if (props.addOrEdit === 'add') {
|
formData.value.maintainerId = userStore.id
|
}
|
} catch (error) {
|
console.error('获取用户信息失败:', error)
|
}
|
} else {
|
// 自动填充维护人ID
|
if (props.addOrEdit === 'add') {
|
formData.value.maintainerId = userStore.id
|
}
|
}
|
})
|
|
const emit = defineEmits(['submit', 'handleBeforeClose','update:coalDialogFormVisible'])
|
// 表单引用
|
const formRef = ref(null)
|
// 表单数据
|
const formData = ref({ ...props.form })
|
// 弹窗可见性
|
const dialogVisible = defineModel("coalDialogFormVisible",{required:true,type:Boolean})
|
|
// 监听外部传入的表单数据变化
|
watch(() => props.form, (newVal) => {
|
formData.value = { ...newVal }
|
// 如果是新增模式,设置维护人
|
if (props.addOrEdit === 'add' && userStore.id) {
|
formData.value.maintainerId = userStore.id
|
}
|
}, { deep: true })
|
|
// 监听内部弹窗状态变化
|
watch(() => dialogVisible.value, (newVal) => {
|
emit('update:coalDialogFormVisible', newVal)
|
})
|
|
// 提交表单
|
const submitForm = async () => {
|
if (!formRef.value) return
|
await formRef.value.validate(async (valid, fields) => {
|
if (valid) {
|
delete formData.value.createTime
|
delete formData.value.updateTime
|
delete formData.value.maintainerName // 删除显示用的字段,只保留ID
|
|
// 确保maintainerId有值
|
if (!formData.value.maintainerId) {
|
formData.value.maintainerId = userStore.id
|
}
|
|
// 设置维护日期
|
formData.value.maintenanceDate = getCurrentDate()
|
|
let result = await addOrEditCoalInfo({
|
...formData.value,
|
})
|
let obj = {
|
title: props.title,
|
result,
|
}
|
emit('submit', obj)
|
}
|
})
|
}
|
// 取消表单
|
const cancelForm = () => {
|
emit('update:coalDialogFormVisible', false)
|
formData.value = {}
|
}
|
// 重置表单
|
const resetForm = () => {
|
if (!formRef.value) return
|
formData.value = JSON.parse(JSON.stringify(copyForm.value));
|
// formRef.value.resetFields()
|
}
|
// 关闭弹窗
|
const handleClose = () => {
|
// 触发父组件的关闭函数
|
emit("handleBeforeClose")
|
emit('update:coalDialogFormVisible', false)
|
}
|
const rules = reactive({
|
supplierName: [
|
{ required: true, message: "请输入供货商名称", trigger: "blur" },
|
],
|
identifyNumber: [
|
{ required: true, message: "请正确输入纳税人识别号", trigger: "blur" },
|
{ min: 17, max: 20, message: "请输入17-20位纳税人识别号", trigger: "blur" },
|
],
|
});
|
// 获取当前日期并格式化为 YYYY-MM-DD
|
function getCurrentDate() {
|
const today = new Date();
|
const year = today.getFullYear();
|
const month = String(today.getMonth() + 1).padStart(2, '0'); // 月份从0开始
|
const day = String(today.getDate()).padStart(2, '0');
|
return `${year}-${month}-${day}`;
|
}
|
</script>
|
|
<style lang="scss" scoped>
|
.dialog-footer {
|
display: flex;
|
margin-top: 20px;
|
flex-direction: column;
|
align-items: flex-end;
|
}
|
</style>
|