张诺
2 天以前 dd159ea51a7a77bd8cc00c70c0e900f472fb3395
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<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>