zhangwencui
6 小时以前 eea8cb3bbe6379755410dffb774bd14e389612c2
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
<template>
  <view class="emergency-plan-detail">
    <!-- 使用通用页面头部组件 -->
    <PageHeader :title="isEdit ? '编辑应急预案' : '新增应急预案'"
                @back="goBack" />
    <!-- 表单区域 -->
    <u-form :model="form"
            label-width="110"
            :rules="rules"
            ref="formRef">
      <!-- 应急预案编码 -->
      <u-form-item label="应急预案编码"
                   border-bottom
                   required
                   prop="planCode">
        <up-input v-model="form.planCode"
                  placeholder="请输入应急预案编码"
                  clearable />
      </u-form-item>
      <!-- 应急预案名称 -->
      <u-form-item label="应急预案名称"
                   required
                   border-bottom
                   prop="planName">
        <up-input v-model="form.planName"
                  placeholder="请输入应急预案名称"
                  clearable />
      </u-form-item>
      <!-- 发布生效时间 -->
      <u-form-item label="发布生效时间"
                   required
                   border-bottom
                   prop="publishTime">
        <up-input v-model="form.publishTime"
                  placeholder="请选择发布生效时间"
                  readonly
                  @click="showTime = true" />
        <template #right>
          <up-icon name="arrow-right"
                   @click="showTime = true"></up-icon>
        </template>
      </u-form-item>
      <!-- 预案类型 -->
      <u-form-item label="预案类型"
                   prop="planType"
                   required
                   border-bottom>
        <u-input v-model="emergencyPlanTypeLabel"
                 placeholder="请选择预案类型"
                 @click="showPlanTypeActionSheet = true"
                 readonly />
        <template #right>
          <up-icon name="arrow-right"
                   @click="showPlanTypeActionSheet = true"></up-icon>
        </template>
      </u-form-item>
      <u-form-item label="核心责任人"
                   prop="coreResponsorUserId"
                   required
                   border-bottom>
        <u-input v-model="form.coreResponsorUserName"
                 placeholder="请选择核心责任人"
                 @click="showUserActionSheet = true"
                 readonly />
        <template #right>
          <up-icon name="arrow-right"
                   @click="showUserActionSheet = true"></up-icon>
        </template>
      </u-form-item>
      <!-- 备注 -->
      <u-form-item label="备注"
                   prop="remark">
        <up-input v-model="form.remark"
                  placeholder="请输入备注"
                  type="textarea"
                  rows="3"
                  clearable />
      </u-form-item>
      <!-- 适用范围 -->
      <u-form-item label="适用范围"
                   required
                   prop="applyScope">
        <view class="checkbox-group">
          <u-checkbox-group v-model="form.applyScope"
                            @change="handleApplyScopeChange">
            <u-checkbox shape="circle"
                        size="32rpx"
                        class="checkbox-item"
                        v-for="(item, index) in applyScopeOptions"
                        :key="index"
                        :label="item.label"
                        :name="item.value">
            </u-checkbox>
          </u-checkbox-group>
        </view>
      </u-form-item>
      <!-- 应急处置步骤 -->
      <view class="exec-steps-container">
        <view class="steps-header">
          <text class="steps-title">处置步骤列表</text>
          <text class="steps-count">共 {{ execStepsList.length }} 个步骤</text>
        </view>
        <view class="steps-list">
          <view v-for="(step, index) in execStepsList"
                :key="index"
                class="exec-step-item">
            <view class="delete-btn"
                  @click="removeExecStep(index)">
              <u-icon name="close"
                      color="#fff"
                      size="16" />
            </view>
            <view class="step-number">
              {{ index + 1 }}
            </view>
            <view class="step-content">
              <view class="step-row">
                <text class="step-label">步骤名称:</text>
                <u-textarea v-model="step.step"
                            placeholder="请输入步骤名称"
                            clearable
                            border-bottom
                            class="step-input" />
              </view>
              <view class="step-row">
                <text class="step-label">处置措施:</text>
                <u-textarea v-model="step.description"
                            placeholder="请输入具体的应急处置措施"
                            type="textarea"
                            clearable
                            class="step-textarea" />
              </view>
            </view>
          </view>
        </view>
        <u-button type="primary"
                  @click="addExecStep"
                  class="add-step-btn">
          <text>添加步骤</text>
        </u-button>
      </view>
    </u-form>
    <!-- 发布生效时间选择器 -->
    <up-datetime-picker :show="showTime"
                        v-model="currentTime"
                        @confirm="handleDateConfirm"
                        @cancel="showTime = false"
                        mode="date" />
    <!--预案类型选择器 -->
    <up-action-sheet :show="showPlanTypeActionSheet"
                     :actions="emergencyPlanTypeOptions"
                     @select="handlePlanTypeConfirm"
                     title="选择预案类型" />
    <!--核心责任人选择器 -->
    <up-action-sheet :show="showUserActionSheet"
                     :actions="userList"
                     @select="handleUserConfirm"
                     title="选择核心责任人" />
  </view>
  <!-- 底部按钮 -->
  <view class="bottom-buttons">
    <u-button type="default"
              size="default"
              @click="goBack"
              class="bottom-btn">
      取消
    </u-button>
    <u-button type="primary"
              size="default"
              @click="submitForm"
              class="bottom-btn">
      保存
    </u-button>
  </view>
</template>
 
<script setup>
  import { ref, onMounted, computed } from "vue";
  import { onShow } from "@dcloudio/uni-app";
  import PageHeader from "@/components/PageHeader.vue";
  import {
    safeContingencyPlanAdd,
    safeContingencyPlanUpdate,
  } from "@/api/safeProduction/emergencyPlanReview";
  import { userListNoPage } from "@/api/system/user";
  import { useDict } from "@/utils/dict";
  import dayjs from "dayjs";
  // 替换 toast 方法
  defineOptions({ name: "emergency-plan-detail" });
  const showToast = message => {
    uni.showToast({
      title: message,
      icon: "none",
    });
  };
 
  // 表单引用
  const formRef = ref();
 
  // 表单数据
  const form = ref({
    id: "",
    planCode: "",
    planName: "",
    publishTime: "",
    planType: "",
    coreResponsorUserId: "",
    coreResponsorUserName: "",
    remark: "",
    applyScope: [],
    execSteps: "",
  });
 
  // 应急处置步骤列表
  const execStepsList = ref([]);
 
  // 日期范围
  const minDate = new Date("2000-01-01");
  const maxDate = new Date("2030-12-31");
  const currentTime = ref(Date.now());
  // 用户列表
  const userList = ref([]);
 
  // 应急预案类型选项
  const { emergency_plan_type } = useDict("emergency_plan_type");
  const emergencyPlanTypeOptions = computed(() => {
    return (
      emergency_plan_type?.value.map(item => ({
        value: item.value,
        name: item.label,
      })) || []
    );
  });
 
  // 应急预案类型标签
  const emergencyPlanTypeLabel = ref("");
 
  // 适用范围选项
  const applyScopeOptions = [
    { value: "all", label: "全体员工" },
    { value: "manager", label: "管理层" },
    { value: "hr", label: "人事部门" },
    { value: "finance", label: "财务部门" },
    { value: "tech", label: "技术部门" },
  ];
 
  // 是否为编辑模式
  const isEdit = ref(false);
 
  // ActionSheet 显示状态
  const showPlanTypeActionSheet = ref(false);
  const showUserActionSheet = ref(false);
  const showTime = ref(false);
 
  // 初始化数据
  const initData = () => {
    const emergencyPlan = uni.getStorageSync("emergencyPlan") || {};
    if (emergencyPlan.id) {
      // 编辑模式
      isEdit.value = true;
      form.value = {
        id: emergencyPlan.id,
        planCode: emergencyPlan.planCode || "",
        planName: emergencyPlan.planName || "",
        publishTime: emergencyPlan.publishTime || "",
        planType: emergencyPlan.planType || "",
        coreResponsorUserId: emergencyPlan.coreResponsorUserId || "",
        coreResponsorUserName: emergencyPlan.coreResponsorUserName || "",
        remark: emergencyPlan.remark || "",
        applyScope: emergencyPlan.applyScope
          ? emergencyPlan.applyScope.split(",")
          : [],
        execSteps: emergencyPlan.execSteps || "",
      };
      currentTime.value = new Date(emergencyPlan.publishTime).getTime();
      // 设置预案类型标签
      const planTypeItem = emergencyPlanTypeOptions.value.find(
        item => item.value === emergencyPlan.planType
      );
      emergencyPlanTypeLabel.value = planTypeItem ? planTypeItem.name : "";
      console.log(form.value.applyScope, form.value.applyScope);
      // 初始化应急处置步骤
      initExecSteps(emergencyPlan.execSteps);
    } else {
      // 新增模式
      isEdit.value = false;
      form.value = {
        planCode: "",
        planName: "",
        publishTime: new Date().toISOString().split("T")[0],
        planType: "",
        coreResponsorUserId: "",
        coreResponsorUserName: "",
        remark: "",
        applyScope: [],
        execSteps: "",
      };
      emergencyPlanTypeLabel.value = "";
      execStepsList.value = [];
      addExecStep();
    }
  };
  const handleApplyScopeChange = e => {
    // form.value.applyScope = e;
    console.log(e, "e");
    console.log(form.value.applyScope, "form.value.applyScope");
  };
 
  // 初始化应急处置步骤
  const initExecSteps = execSteps => {
    if (execSteps) {
      try {
        execStepsList.value = JSON.parse(execSteps);
      } catch (e) {
        execStepsList.value = [];
      }
    } else {
      execStepsList.value = [];
    }
    if (execStepsList.value.length === 0) {
      addExecStep();
    }
  };
 
  // 添加应急处置步骤
  const addExecStep = () => {
    const stepNumber = execStepsList.value.length + 1;
    execStepsList.value.push({
      step: `步骤${stepNumber}`,
      description: "",
    });
  };
 
  // 删除应急处置步骤
  const removeExecStep = index => {
    if (execStepsList.value.length > 1) {
      execStepsList.value.splice(index, 1);
    } else {
      showToast("至少保留一个步骤");
    }
  };
 
  // 获取用户列表
  const getUserList = () => {
    userListNoPage()
      .then(res => {
        userList.value = res.data.map(item => ({
          value: item.userId,
          name: item.nickName,
        }));
      })
      .catch(() => {
        showToast("获取用户列表失败");
      });
  };
 
  // 日期选择确认
  const handleDateConfirm = e => {
    form.value.publishTime = dayjs(e.value).format("YYYY-MM-DD");
    showTime.value = false;
  };
 
  // 预案类型选择确认
  const handlePlanTypeConfirm = e => {
    form.value.planType = e.value;
    const selectedType = emergencyPlanTypeOptions.value.find(
      item => item.value === e.value
    );
    if (selectedType) {
      emergencyPlanTypeLabel.value = selectedType.name;
    }
    showPlanTypeActionSheet.value = false;
  };
 
  // 用户选择确认
  const handleUserConfirm = e => {
    form.value.coreResponsorUserId = e.value;
    const selectedUser = userList.value.find(user => user.value === e.value);
    if (selectedUser) {
      form.value.coreResponsorUserName = selectedUser.name;
    }
    showUserActionSheet.value = false;
  };
 
  // 返回上一页
  const goBack = () => {
    uni.navigateBack();
  };
 
  // 表单验证规则
  const rules = {
    planCode: [
      {
        required: true,
        message: "请输入应急预案编码",
        trigger: ["submit", "blur"],
      },
    ],
    planName: [
      {
        required: true,
        message: "请输入应急预案名称",
        trigger: ["submit", "blur"],
      },
    ],
    publishTime: [
      {
        required: true,
        message: "请选择发布生效时间",
        trigger: ["submit", "change"],
      },
    ],
    planType: [
      {
        required: true,
        message: "请选择预案类型",
        trigger: ["submit", "change"],
      },
    ],
    coreResponsorUserId: [
      {
        required: true,
        message: "请选择核心责任人",
        trigger: ["submit", "change"],
      },
    ],
    applyScope: [
      {
        required: true,
        message: "请选择适用范围",
        trigger: ["submit", "change"],
      },
    ],
  };
 
  // 提交表单
  const submitForm = async () => {
    // 验证表单必填项
    if (!formRef.value) return;
 
    const valid = await formRef.value.validate();
    if (!valid) {
      return;
    }
 
    // 验证应急处置步骤
    for (let i = 0; i < execStepsList.value.length; i++) {
      const step = execStepsList.value[i];
      if (!step.step || !step.step.trim()) {
        showToast(`第${i + 1}条步骤的"步骤"不能为空`);
        return;
      }
      if (!step.description || !step.description.trim()) {
        showToast(`第${i + 1}条步骤的"措施"不能为空`);
        return;
      }
    }
 
    // 将应急处置步骤转换为JSON字符串
    form.value.execSteps = JSON.stringify(execStepsList.value);
 
    // 处理适用范围
    form.value.applyScope = form.value.applyScope.join(",");
 
    showLoadingToast("保存中...");
 
    try {
      if (isEdit.value) {
        // 编辑模式
        const res = await safeContingencyPlanUpdate(form.value);
        if (res.code === 200) {
          showToast("更新成功");
          setTimeout(() => {
            uni.navigateBack();
          }, 1000);
        } else {
          showToast(res.msg || "更新失败");
        }
      } else {
        // 新增模式
        const res = await safeContingencyPlanAdd(form.value);
        if (res.code === 200) {
          showToast("添加成功");
          setTimeout(() => {
            uni.navigateBack();
          }, 1000);
        } else {
          showToast(res.msg || "添加失败");
        }
      }
    } catch (error) {
      console.error("提交失败:", error);
      showToast("提交失败,请重试");
    } finally {
      closeToast();
    }
  };
 
  // 显示加载提示
  const showLoadingToast = message => {
    uni.showLoading({
      title: message,
      mask: true,
    });
  };
 
  // 关闭提示
  const closeToast = () => {
    uni.hideLoading();
  };
 
  onMounted(() => {
    initData();
    getUserList();
  });
 
  onShow(() => {
    initData();
  });
</script>
 
<style scoped lang="scss">
  @import "@/static/scss/form-common.scss";
  .emergency-plan-detail {
    min-height: 100vh;
    background: #f8f9fa;
    padding-bottom: 100px;
  }
 
  .form-section {
  }
 
  .checkbox-group {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
  }
 
  .checkbox-item {
    margin-right: 16px;
  }
 
  .select-container {
    position: relative;
    width: 100%;
  }
 
  .select-container .up-input {
    width: 100%;
    border: 1px solid #e4e7ed;
    border-radius: 8px;
    padding: 12px 16px;
    background-color: #ffffff;
  }
 
  .exec-steps-container {
    padding: 20px;
    background-color: #fff;
  }
 
  .steps-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding-bottom: 12px;
    border-bottom: 1px solid #e4e7ed;
  }
 
  .steps-title {
    font-size: 16px;
    font-weight: 600;
    color: #303133;
  }
 
  .steps-count {
    font-size: 14px;
    color: #909399;
  }
 
  .steps-list {
    margin-bottom: 20px;
  }
 
  .exec-step-item {
    position: relative;
    display: flex;
    margin-bottom: 16px;
    padding: 16px;
    background-color: #ffffff;
    border: 1px solid #e4e7ed;
    border-radius: 8px;
    transition: all 0.3s ease;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
  }
 
  .exec-step-item:hover {
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
    border-color: #409eff;
    transform: translateY(-1px);
  }
 
  .delete-btn {
    position: absolute;
    top: -25rpx;
    right: -25rpx;
    width: 50rpx;
    height: 50rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    font-size: 20px;
    border-radius: 50%;
    background-color: red;
    border: none;
    z-index: 10;
  }
 
  .delete-btn:hover {
    transform: scale(1.1);
    box-shadow: 0 3px 6px rgba(245, 108, 108, 0.4);
  }
 
  .step-number {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    margin-right: 16px;
    background-color: #ecf5ff;
    color: #409eff;
    font-size: 14px;
    font-weight: 600;
    border-radius: 50%;
    flex-shrink: 0;
  }
 
  .step-content {
    flex: 1;
    min-width: 0;
  }
 
  .step-row {
    display: flex;
    align-items: flex-start;
    margin-bottom: 12px;
  }
 
  .step-row:last-child {
    margin-bottom: 0;
  }
 
  .step-label {
    display: inline-block;
    width: 80px;
    font-size: 14px;
    color: #606266;
    margin-right: 12px;
    flex-shrink: 0;
    line-height: 36px;
  }
 
  .step-input {
    flex: 1;
    min-width: 0;
  }
 
  .step-input input {
    font-size: 14px;
    color: #303133;
  }
 
  .step-textarea {
    flex: 1;
    min-width: 0;
  }
 
  .step-textarea textarea {
    font-size: 14px;
    color: #303133;
    min-height: 80px;
    line-height: 1.5;
  }
 
  .add-step-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 44px;
    line-height: 44px;
    font-size: 14px;
    border-radius: 8px;
    transition: all 0.3s ease;
    gap: 8px;
  }
 
  .add-step-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
  }
 
  .add-step-btn text {
    font-size: 14px;
  }
 
  .bottom-buttons {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    padding: 16px 20px;
    background: #ffffff;
    border-top: 1px solid #f0f0f0;
    gap: 16px;
  }
 
  .bottom-btn {
    flex: 1;
  }
</style>