-
admin
7 天以前 9ee8fbbecf45f58fd2469d9049b7938e363d9a4f
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<template>
    <view class="container">
        <uni-section :title="isEdit ? '修改工时' : '录入工时'" type="line">
            <view class="example">
                <!-- 基础用法,不包含校验规则 -->
                <uni-forms labelWidth="75px" ref="baseForm" :modelValue="baseFormData" :rules="rules">
                    <uni-forms-item label="项目名称" required name="projectCode">
                        <view class="project-select" @click="showProjectSelector">
                            <uni-easyinput
                                v-model="selectedProjectText"
                                placeholder="请选择项目"
                                readonly
                            />
                        </view>
                        <project-selector
                            ref="projectSelector"
                            v-model="baseFormData.projectCode"
                            @change="handleProjectChange"
                        />
                    </uni-forms-item>
                    <uni-forms-item label="项目阶段" required name="projectPhase">
                        <uni-data-select
                          v-model="baseFormData.projectPhase"
                          :localdata="phaseSelect"
                          @change="change"
                        ></uni-data-select>
                    </uni-forms-item>
                    <uni-forms-item label="工种" required name="workType">
                        <uni-data-select
                          v-model="baseFormData.workType"
                          :localdata="typeSelect"
                          @change="change"
                        ></uni-data-select>
                    </uni-forms-item>
                    <uni-forms-item label="工时" required name="workTime">
                        <uni-easyinput @input="validateWorkTime" v-model="baseFormData.workTime" placeholder="请输入工时" />
                    </uni-forms-item>
                    <uni-forms-item label="工作详情">
                        <uni-easyinput type="textarea" v-model="baseFormData.details" placeholder="请编辑工作详情" />
                    </uni-forms-item>
                    <button form-type="submit" type="primary" @click="handleSubmit('baseForm')">提交</button>
                    
                </uni-forms>
            </view>
        </uni-section>
    </view>
</template>
<script>
    import { getDicts } from '@/api/dema/dictData.js'
    import { appAddWorkHourInfo, appUpdateWorkHourInfo } from '@/api/dema/workHourInfo.js'
    import ProjectSelector from '@/components/ProjectSelector/index.vue'
    export default {
        components: {
            ProjectSelector
        },
        data() {
            return {
                // 基础表单数据
                baseFormData: {
                    projectCode: '',
                    projectName: '',
                    projectPhase: '',
                    workType: '',
                    workTime: '',
                    details: '',
                    workHourId: '',
                },
                selectedProjectText: '',
                typeSelect: [
                  // { value: '机械设计', text: "机械设计" },
                  // { value: '电气设计', text: "电气设计" },
                  // { value: '软件', text: "软件" },
                  // { value: '软件', text: "视觉" },
                  // { value: '电气调试', text: "电气调试" },
                  // { value: '电工', text: "电工" },
                  // { value: '钳工', text: "钳工" },
                  // { value: '现场经理', text: "现场经理" },
                ],
                phaseSelect: [
                  // { value: '场内设计', text: "场内设计" },
                  // { value: '场内装配', text: "场内装配" },
                  // { value: '场内调试', text: "场内调试" },
                  // { value: '场外装配', text: "场外装配" },
                  // { value: '场外调试', text: "场外调试" },
                  // { value: '试生产', text: "试生产" },
                  // { value: '陪产', text: "陪产" },
                  // { value: '终验收', text: "终验收" },
                ],
                codeSelect: [
                  { value: '042湖州智芯', text: "042湖州智芯" },
                  { value: '040春风动力', text: "040春风动力" },
                ],
                rules: {
                    projectCode: {
                        rules: [{
                            required: true,
                            errorMessage: '请选择项目'
                        }]
                    },
                    projectPhase: {
                        rules: [{
                            required: true,
                            errorMessage: '请选择项目阶段'
                        }]
                    },
                    workType: {
                        rules: [{
                            required: true,
                            errorMessage: '请选择工种'
                        }]
                    },
                    workTime: {
                        rules: [{
                            required: true,
                            errorMessage: '请输入工时'
                        }, {
                            format: 'number',
                            errorMessage: '工时必须是数字'
                        }, {
                            validateFunction: (rule, value, data, callback) => {
                                if (value > 24) {
                                    callback('工时不能大于24小时');
                                }
                                return true;
                            }
                        }]
                    },
                },
                isEdit: false, // 是否是编辑模式
                recordId: '', // 记录ID
            }
        },
        onLoad(options) {
            // 处理传入的参数
            if (options.isEdit === 'true') {
                this.isEdit = true;
                this.recordId = options.id;
                
                // 获取工种和项目阶段的文本显示
                // const workTypeText = this.typeSelect.find(item => item.value === Number(options.workType))?.text || '';
                // const phaseText = this.phaseSelect.find(item => item.value === Number(options.projectPhase))?.text || '';
                
                // 填充表单数据
                this.baseFormData = {
                    workHourId: options.workHourId,
                    projectCode: options.projectCode,
                    projectName: options.projectName,
                    projectPhase: options.projectPhase, // 确保转换为数字
                    workType: options.workType, // 确保转换为数字
                    workTime: options.workTime,
                    details: options.details || ''
                };
                
                // 设置项目选择器的显示文本
                this.selectedProjectText = options.projectName;
                
                // 打印调试信息
                console.log('工种:', workTypeText, '项目阶段:', phaseText);
                console.log('表单数据:', this.baseFormData);
            }
        },
        created() {
            this.initGetDicts()
        },
        methods: {
            initGetDicts(value){
                getDicts("work_type").then(response => {
                    this.typeSelect = response.data.map(item => ({
                        value: item.dictValue,
                        text: item.dictValue
                    }));
                });
                getDicts("project_phase").then(response => {
                    this.phaseSelect = response.data.map(item => ({
                        value: item.dictValue,
                        text: item.dictValue
                    }));
                });
            },
            validateWorkTime(value) {
                // // 移除非数字字符
                // this.baseFormData.workTime = value.replace(/[^\d.]/g, '');
                // // 限制最大值为24
                // if (Number(this.baseFormData.workTime) > 24) {
                //     this.baseFormData.workTime = '24';
                // }
                // 只允许数字和小数点,且小数点只能出现一次
                let newValue = value.replace(/[^\d.]/g, '');
                // 确保只有一个小数点
                if (newValue.split('.').length > 2) {
                    newValue = newValue.replace(/\.+/g, '.');
                }
                // 限制最大值为24
                if (Number(newValue) > 24) {
                    newValue = '24';
                }
                // 更新表单数据
                this.baseFormData.workTime = newValue;
            },
            change(){                
            },
            handleBack() {
                uni.navigateBack({
                    delta: 1
                });
            },
            handleReset() {
                this.$refs.baseForm.resetFields();
            },
            handleSubmit(ref) {
                this.$refs.baseForm.validate().then(valid => {
                    if (valid) {
                        if(this.baseFormData.workTime > 8 && this.baseFormData.details === ''){
                            this.$modal.msgError("工时超过8小时时,必须填写工作详情");
                            return false
                        }
                        const submitData = {
                            ...this.baseFormData,
                            // workTime: Number(this.baseFormData.workTime) // 旧代码
                            workTime: parseFloat(this.baseFormData.workTime) // 修改为 parseFloat 以保留小数
                        };
                            
                        // 如果是编辑模式,添加ID
                        if (this.isEdit) {
                            submitData.id = this.recordId;
                        }
                        
                        // 根据模式选择API
                        const api = this.isEdit ? appUpdateWorkHourInfo : appAddWorkHourInfo;
                        const successMsg = this.isEdit ? '修改成功' : '新增成功';
                        
                        api(submitData).then(response => {
                            this.$modal.msgSuccess(successMsg);
                            setTimeout(() => {
                                if(successMsg === '新增成功'){
                                    this.$tab.navigateBack();
                                }else{
                                    // 返回上一页并刷新列表
                                    const pages = getCurrentPages();
                                    const prevPage = pages[pages.length - 2];
                                    if (prevPage && prevPage.$vm) {
                                        // 调用上一页的刷新方法
                                        prevPage.$vm.getList();
                                        this.$tab.navigateBack();
                                    }
                                }
                                
                            }, 700);
                        }).catch(error => {
                            this.$modal.msgError(this.isEdit ? "修改失败" : "提交失败:" + error.message);
                        });
                    }
                }).catch(errors => {
                    console.log('表单验证失败:', errors);
                    let errorMsg = '';
                    if (Array.isArray(errors)) {
                        errorMsg = errors.map(error => error.errorMessage).join('\n');
                    } else {
                        errorMsg = '请填写必填项';
                    }
                    this.$modal.msgError(errorMsg);
                });
                
            },
        
            showProjectSelector() {
                this.$refs.projectSelector.show()
            },
            handleProjectChange(project) {
                this.selectedProjectText = project.text
                this.baseFormData.projectCode = project.value
                this.baseFormData.projectName = project.text;
            }
        }
    }
</script>
 
<style>
    .uni-form-item .title {
        padding: 20rpx 0;
    }
    .example {
            padding: 15px;
            background-color: #fff;
        }
    
        .segmented-control {
            margin-bottom: 15px;
        }
    
        .button-group {
            margin-top: 15px;
            display: flex;
            justify-content: space-around;
        }
    
        .form-item {
            display: flex;
            align-items: center;
        }
    
        .button {
            display: flex;
            align-items: center;
            height: 35px;
            margin-left: 10px;
        }
        .title {
                font-size: 14px;
                font-weight: bold;
                margin: 20px 0 5px 0;
            }
        
            .data-pickerview {
                height: 400px;
                border: 1px #e5e5e5 solid;
            }
        
             .popper__arrow {
            top: -6px;
            left: 50%;
            margin-right: 3px;
            border-top-width: 0;
            border-bottom-color: #EBEEF5;
        }
         .popper__arrow {
            top: -6px;
            left: 50%;
            margin-right: 3px;
            border-top-width: 0;
            border-bottom-color: #EBEEF5;
        }
        .project-select {
          position: relative;
          width: 100%;
        }
 
        .project-select :deep(.uni-easyinput__content) {
          cursor: pointer;
          background-color: #f5f5f5;
        }
</style>