spring
7 天以前 add06adc5d974ac685cb637c48f2455034c8a52f
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<template>
  <view class="danger-investigation-detail">
    <PageHeader :title="isEdit ? '编辑培训' : '新增培训'"
                @back="goBack" />
    <u-form @submit="handleSubmit"
            ref="formRef"
            label-width="110">
      <!-- 培训信息 -->
      <u-cell-group title="培训信息">
        <u-form-item label="课程编号"
                     prop="courseCode"
                     border-bottom>
          <u-input v-model="form.courseCode"
                   placeholder="系统自动生成"
                   readonly />
        </u-form-item>
        <u-form-item label="培训日期"
                     prop="trainingDate"
                     required
                     border-bottom>
          <u-input v-model="form.trainingDate"
                   placeholder="请选择培训日期"
                   @click="showTrainingDatePicker"
                   readonly />
          <template #right>
            <up-icon name="arrow-right"
                     @click="showTrainingDatePicker"></up-icon>
          </template>
        </u-form-item>
        <u-form-item label="开始时间"
                     prop="openingTime"
                     required
                     border-bottom>
          <u-input v-model="form.openingTime"
                   placeholder="请选择开始时间"
                   @click="showOpeningTimePicker"
                   readonly />
          <template #right>
            <up-icon name="arrow-right"
                     @click="showOpeningTimePicker"></up-icon>
          </template>
        </u-form-item>
        <u-form-item label="结束时间"
                     prop="endTime"
                     required
                     border-bottom>
          <u-input v-model="form.endTime"
                   placeholder="请选择结束时间"
                   @click="showEndTimePicker"
                   readonly />
          <template #right>
            <up-icon name="arrow-right"
                     @click="showEndTimePicker"></up-icon>
          </template>
        </u-form-item>
        <u-form-item label="培训目标"
                     prop="trainingObjectives"
                     border-bottom>
          <u-input v-model="form.trainingObjectives"
                   placeholder="请输入培训目标" />
        </u-form-item>
        <u-form-item label="参加对象"
                     prop="participants"
                     border-bottom>
          <u-input v-model="form.participants"
                   placeholder="请输入参加对象" />
        </u-form-item>
        <u-form-item label="培训内容"
                     prop="trainingContent"
                     required
                     border-bottom>
          <u-textarea v-model="form.trainingContent"
                      placeholder="请输入培训内容"
                      :maxlength="200"
                      count
                      :autoHeight="true" />
        </u-form-item>
        <u-form-item label="培训讲师"
                     prop="trainingLecturer"
                     required
                     border-bottom>
          <u-input v-model="form.trainingLecturer"
                   placeholder="请输入培训讲师" />
        </u-form-item>
        <u-form-item label="项目学分"
                     prop="projectCredits"
                     border-bottom>
          <u-input v-model="form.projectCredits"
                   placeholder="请输入项目学分"
                   type="number" />
        </u-form-item>
        <u-form-item label="培训方式"
                     prop="trainingMode"
                     border-bottom>
          <u-input v-model="trainingModeName"
                   placeholder="请选择培训方式"
                   @click="showTrainingModeSheet"
                   readonly />
          <template #right>
            <up-icon name="arrow-right"
                     @click="showTrainingModeSheet"></up-icon>
          </template>
        </u-form-item>
        <u-form-item label="培训地点"
                     prop="placeTraining"
                     border-bottom>
          <u-input v-model="form.placeTraining"
                   placeholder="请输入培训地点" />
        </u-form-item>
        <u-form-item label="课时"
                     prop="classHour"
                     required
                     border-bottom>
          <u-input v-model="form.classHour"
                   placeholder="请输入课时"
                   type="number" />
        </u-form-item>
      </u-cell-group>
      <!-- 提交按钮 -->
      <view class="footer-btns">
        <u-button class="cancel-btn"
                  @click="goBack">取消</u-button>
        <u-button class="sign-btn"
                  type="primary"
                  @click="handleSubmit"
                  :loading="loading">{{ isEdit ? '保存修改' : '提交' }}</u-button>
      </view>
    </u-form>
    <!-- 时间选择器 -->
    <up-datetime-picker :show="trainingDateVisible"
                        mode="date"
                        v-model="nowDate"
                        @confirm="handleTrainingDateConfirm"
                        @cancel="trainingDateVisible = false"
                        title="选择培训日期" />
    <u-datetime-picker :show="openingTimeVisible"
                       mode="time"
                       @confirm="handleOpeningTimeConfirm"
                       @cancel="openingTimeVisible = false"
                       title="选择开始时间" />
    <u-datetime-picker :show="endTimeVisible"
                       mode="time"
                       @confirm="handleEndTimeConfirm"
                       @cancel="endTimeVisible = false"
                       title="选择结束时间" />
    <!-- 培训方式选择器 -->
    <up-action-sheet :show="trainingModeSheetVisible"
                     :actions="trainingModeOptions"
                     @select="handleTrainingModeSelect"
                     @close="trainingModeSheetVisible = false"
                     title="选择培训方式" />
  </view>
</template>
 
<script setup>
  // 替换 toast 方法
  defineOptions({ name: "danger-investigation-detail" });
  const showToast = message => {
    uni.showToast({ title: message, icon: "none" });
  };
 
  import { ref, onMounted } from "vue";
  import PageHeader from "@/components/PageHeader.vue";
  import {
    safeTrainingAdd,
    safeTrainingUpdate,
  } from "@/api/safeProduction/safetyTrainingAssessment";
  import { onLoad } from "@dcloudio/uni-app";
  import { useDict } from "@/utils/dict";
  import dayjs from "dayjs";
 
  // 获取字典数据
  const { safe_training_methods } = useDict("safe_training_methods");
 
  // 表单数据
  const form = ref({
    courseCode: "", // 课程编号
    trainingDate: "", // 培训日期
    openingTime: "", // 开始时间
    endTime: "", // 结束时间
    trainingObjectives: "", // 培训目标
    participants: "", // 参加对象
    trainingContent: "", // 培训内容
    trainingLecturer: "", // 培训讲师
    projectCredits: "", // 项目学分
    trainingMode: "", // 培训方式
    placeTraining: "", // 培训地点
    classHour: "", // 课时
  });
 
  // 页面状态
  const loading = ref(false);
  const formRef = ref(null);
  const isEdit = ref(false);
 
  // 培训方式选择器
  const trainingModeSheetVisible = ref(false);
  const trainingModeName = ref("");
  const trainingModeOptions = ref([]);
 
  // 时间选择器
  const trainingDateVisible = ref(false);
  const openingTimeVisible = ref(false);
  const endTimeVisible = ref(false);
  const nowDate = ref(new Date());
 
  const showTrainingDatePicker = () => {
    trainingDateVisible.value = true;
  };
 
  const showOpeningTimePicker = () => {
    openingTimeVisible.value = true;
  };
 
  const showEndTimePicker = () => {
    endTimeVisible.value = true;
  };
 
  const handleTrainingDateConfirm = e => {
    form.value.trainingDate = dayjs(e.value).format("YYYY-MM-DD");
    nowDate.value = e.value;
    trainingDateVisible.value = false;
  };
 
  const handleOpeningTimeConfirm = e => {
    console.log(e);
    form.value.openingTime = e.value;
    openingTimeVisible.value = false;
  };
 
  const handleEndTimeConfirm = e => {
    form.value.endTime = e.value;
    endTimeVisible.value = false;
  };
 
  // 显示培训方式选择器
  const showTrainingModeSheet = () => {
    trainingModeSheetVisible.value = true;
  };
 
  // 处理培训方式选择
  const handleTrainingModeSelect = item => {
    form.value.trainingMode = item.value;
    trainingModeName.value = item.name;
    trainingModeSheetVisible.value = false;
  };
 
  // 返回上一页
  const goBack = () => {
    uni.removeStorageSync("safetyTraining");
    uni.navigateBack();
  };
 
  // 提交表单
  const handleSubmit = async () => {
    if (!form.value.trainingDate) {
      showToast("请选择培训日期");
      return;
    }
 
    if (!form.value.openingTime) {
      showToast("请选择开始时间");
      return;
    }
 
    if (!form.value.endTime) {
      showToast("请选择结束时间");
      return;
    }
    if (!form.value.trainingContent) {
      showToast("请输入培训内容");
      return;
    }
 
    if (!form.value.trainingLecturer) {
      showToast("请输入培训讲师");
      return;
    }
 
    if (!form.value.classHour) {
      showToast("请输入课时");
      return;
    }
    if (
      form.value.projectCredits &&
      (isNaN(Number(form.value.projectCredits)) ||
        Number(form.value.projectCredits) <= 0)
    ) {
      showToast("学分必须是大于0的数字");
      return;
    }
    form.value.openingTime = form.value.openingTime + ":00";
    form.value.endTime = form.value.endTime + ":00";
 
    if (
      form.value.classHour &&
      (isNaN(Number(form.value.classHour)) || Number(form.value.classHour) <= 0)
    ) {
      showToast("课时必须是大于0的数字");
      return;
    }
 
    try {
      loading.value = true;
 
      // 使用安全浅拷贝
      const source =
        form.value && typeof form.value === "object" ? form.value : {};
      const submitData = {};
      Object.keys(source).forEach(k => {
        submitData[k] = source[k];
      });
 
      if (isEdit.value) {
        const { code } = await safeTrainingAdd(submitData);
        if (code === 200) {
          showToast("修改成功");
          setTimeout(() => {
            goBack();
          }, 500);
        } else {
          loading.value = false;
          showToast("修改失败,请重试");
        }
      } else {
        const { code } = await safeTrainingAdd(submitData);
        if (code === 200) {
          showToast("新增成功");
          setTimeout(() => {
            goBack();
          }, 500);
        } else {
          loading.value = false;
          showToast("新增失败,请重试");
        }
      }
    } catch (e) {
      loading.value = false;
      console.error("提交失败:", e);
      showToast("提交失败,请重试");
    }
  };
 
  onLoad(() => {
    // 编辑培训时,从本地存储获取数据
    const safetyTraining = uni.getStorageSync("safetyTraining");
    if (safetyTraining.id) {
      form.value = safetyTraining;
      nowDate.value = dayjs(form.value.trainingDate).toDate();
      form.value.openingTime = form.value.openingTime
        ? form.value.openingTime.slice(0, 5)
        : "";
      form.value.endTime = form.value.endTime
        ? form.value.endTime.slice(0, 5)
        : "";
      isEdit.value = true;
    } else {
      isEdit.value = false;
      // 默认培训日期为今天
      form.value.trainingDate = dayjs().format("YYYY-MM-DD");
    }
  });
 
  onMounted(() => {
    // 初始化培训方式选项
    if (safe_training_methods && Array.isArray(safe_training_methods.value)) {
      trainingModeOptions.value =
        safe_training_methods.value.map(item => ({
          value: item.value,
          name: item.label,
        })) || [];
    } else {
      trainingModeOptions.value = [];
    }
 
    // 设置已选值的显示文本
    if (form.value.trainingMode) {
      const modeItem = trainingModeOptions.value.find(
        item => String(item.value) === String(form.value.trainingMode)
      );
      trainingModeName.value = modeItem ? modeItem.name : "";
    }
  });
</script>
 
<style scoped lang="scss">
  @import "@/static/scss/form-common.scss";
 
  .danger-investigation-detail {
    min-height: 100vh;
    background: #f8f9fa;
    padding-bottom: 100px;
  }
 
  .footer-btns {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    background: #fff;
    display: flex;
    justify-content: space-around;
    align-items: center;
    padding: 0.75rem 0;
    box-shadow: 0 -0.125rem 0.5rem rgba(0, 0, 0, 0.05);
    z-index: 1000;
  }
 
  .cancel-btn {
    font-weight: 400;
    font-size: 1rem;
    color: #666;
    background: #f5f5f5;
    border: 1px solid #ddd;
    width: 45%;
    height: 2.5rem;
    border-radius: 2.5rem;
  }
 
  .sign-btn {
    font-weight: 500;
    font-size: 1rem;
    color: #fff;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    width: 45%;
    height: 2.5rem;
    border-radius: 2.5rem;
  }
</style>