zhangwencui
9 小时以前 34af9a2cd41fb88985d05d57e9fa425bc98990cc
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
<template>
  <view class="account-detail">
    <PageHeader :title="pageTitle" @back="goBack" />
    <up-form :model="form" label-width="110">
      <up-form-item label="隐患描述" required>
        <up-textarea v-model="form.hazardDesc" placeholder="请输入" auto-height />
      </up-form-item>
      <up-form-item label="隐患等级" required>
        <up-input :model-value="form.hazardLevel" placeholder="请选择" readonly @click="showLevelSheet = true" />
        <template #right>
          <up-icon name="arrow-right" @click="showLevelSheet = true"></up-icon>
        </template>
      </up-form-item>
      <up-form-item label="隐患位置" required>
        <up-input v-model="form.hazardLocation" placeholder="请输入" clearable />
      </up-form-item>
    </up-form>
 
    <FooterButtons :loading="loading" confirmText="保存" @cancel="goBack" @confirm="handleSubmit" />
 
    <up-action-sheet
      :show="showLevelSheet"
      title="选择隐患等级"
      :actions="levelActions"
      @select="onSelectLevel"
      @close="showLevelSheet = false"
    />
  </view>
</template>
 
<script setup>
  import { computed, ref } from "vue";
  import { onLoad } from "@dcloudio/uni-app";
  import PageHeader from "@/components/PageHeader.vue";
  import FooterButtons from "@/components/FooterButtons.vue";
  import { addInspectionHazard, updateInspectionHazard } from "@/api/safeProduction/lineInspection";
 
  const loading = ref(false);
  const inspectionId = ref(null);
 
  const form = ref({
    id: null,
    inspectionId: null,
    hazardDesc: "",
    hazardLevel: "",
    hazardLocation: "",
  });
 
  const showLevelSheet = ref(false);
  const levelActions = [
    { name: "一般", value: "一般" },
    { name: "重大", value: "重大" },
  ];
 
  const pageTitle = computed(() => (form.value.id ? "编辑隐患" : "上报隐患"));
 
  const goBack = () => {
    uni.navigateBack();
  };
 
  const onSelectLevel = action => {
    form.value.hazardLevel = action.value;
    showLevelSheet.value = false;
  };
 
  const handleSubmit = () => {
    const hazardDesc = String(form.value.hazardDesc || "").trim();
    if (!hazardDesc) {
      uni.showToast({ title: "请输入隐患描述", icon: "none" });
      return;
    }
    const hazardLevel = String(form.value.hazardLevel || "").trim();
    if (!hazardLevel) {
      uni.showToast({ title: "请选择隐患等级", icon: "none" });
      return;
    }
    const hazardLocation = String(form.value.hazardLocation || "").trim();
    if (!hazardLocation) {
      uni.showToast({ title: "请输入隐患位置", icon: "none" });
      return;
    }
 
    const payload = {
      ...form.value,
      inspectionId: inspectionId.value,
      hazardDesc,
      hazardLevel,
      hazardLocation,
    };
 
    loading.value = true;
    const api = payload.id ? updateInspectionHazard : addInspectionHazard;
    api(payload)
      .then(() => {
        uni.showToast({ title: "保存成功", icon: "success" });
        uni.$emit("lineInspection:hazardsRefresh");
        goBack();
      })
      .catch(() => {
        uni.showToast({ title: "保存失败", icon: "none" });
      })
      .finally(() => {
        loading.value = false;
      });
  };
 
  onLoad(options => {
    if (options?.data) {
      try {
        const obj = JSON.parse(decodeURIComponent(options.data));
        inspectionId.value = obj.inspectionId;
        if (obj?.row) {
          form.value = { ...form.value, ...(obj.row || {}) };
        }
      } catch (e) {}
    }
  });
</script>
 
<style scoped lang="scss">
  @import "@/static/scss/form-common.scss";
</style>