gaoluyang
8 天以前 e449a5408265e4bd1f6c66f5be28a42efac444ee
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
<template>
  <view class="account-detail">
    <PageHeader :title="pageTitle"
                @back="goBack" />
    <view class="form-container">
      <up-form ref="formRef"
               :model="form"
               :rules="rules"
               label-width="110"
               input-align="right"
               error-message-align="right">
        <u-cell-group title="加油卡信息"
                      class="form-section">
          <up-form-item label="加油卡号"
                        prop="cardNumber"
                        required>
            <up-input v-model="form.cardNumber"
                      placeholder="请输入加油卡号"
                      clearable />
          </up-form-item>
          <up-form-item label="所属部门"
                        prop="deptId"
                        required>
            <FormPicker v-model="form.deptId"
                        type="select"
                        title="选择所属部门"
                        :options="deptOptions"
                        placeholder="请选择所属部门"
                        @change="onDeptChange" />
          </up-form-item>
          <up-form-item label="发卡单位"
                        prop="issuer"
                        required>
            <up-input v-model="form.issuer"
                      placeholder="请输入发卡单位"
                      clearable />
          </up-form-item>
          <!-- 期初余额只在新增时可填,编辑时余额由充值/加油记录维护 -->
          <up-form-item v-if="!cardId"
                        label="期初余额"
                        prop="initialBalance">
            <up-input v-model="form.initialBalance"
                      type="number"
                      placeholder="请输入期初余额"
                      clearable />
          </up-form-item>
          <up-form-item label="状态"
                        prop="status"
                        required>
            <FormPicker v-model="form.status"
                        type="select"
                        title="选择状态"
                        :options="STATUS_OPTIONS"
                        placeholder="请选择状态" />
          </up-form-item>
          <up-form-item label="备注"
                        prop="remark">
            <up-textarea v-model="form.remark"
                         placeholder="请输入备注"
                         auto-height />
          </up-form-item>
        </u-cell-group>
      </up-form>
    </view>
 
    <FooterButtons :loading="loading"
                   confirmText="保存"
                   @cancel="goBack"
                   @confirm="handleSubmit" />
  </view>
</template>
 
<script setup>
  import { computed, onMounted, ref } from "vue";
  import { onLoad } from "@dcloudio/uni-app";
  import FooterButtons from "@/components/FooterButtons.vue";
  import PageHeader from "@/components/PageHeader.vue";
  import FormPicker from "@/components/FormPicker.vue";
  import { getDept } from "@/api/collaborativeApproval/approvalProcess";
  import { loadDetailData } from "@/utils/detailCache";
  import {
    addFuelCard,
    listFuelCardPage,
    updateFuelCard,
  } from "@/api/inventoryManagement/vehicle";
 
  const STATUS_OPTIONS = [
    { name: "正常", value: "ACTIVE" },
    { name: "注销", value: "CANCELLED" },
  ];
 
  const formRef = ref();
  const loading = ref(false);
  const cardId = ref("");
  const deptOptions = ref([]);
 
  const form = ref({
    id: undefined,
    cardNumber: "",
    deptId: "",
    deptName: "",
    issuer: "",
    initialBalance: 0,
    status: "ACTIVE",
    remark: "",
  });
 
  const rules = {
    cardNumber: [{ required: true, message: "请输入加油卡号", trigger: "blur" }],
    deptId: [{ required: true, message: "请选择所属部门", trigger: "change" }],
    issuer: [{ required: true, message: "请输入发卡单位", trigger: "blur" }],
    status: [{ required: true, message: "请选择状态", trigger: "change" }],
  };
 
  const pageTitle = computed(() => (cardId.value ? "编辑加油卡" : "新增加油卡"));
 
  const goBack = () => uni.navigateBack();
 
  const onDeptChange = (value, item) => {
    form.value.deptName = item?.name || "";
  };
 
  const loadDepts = async () => {
    try {
      const res = await getDept();
      deptOptions.value = (res?.data || []).map(item => ({
        name: item.deptName,
        value: item.deptId,
      }));
    } catch (error) {
      deptOptions.value = [];
    }
  };
 
  // 加油卡没有单条详情接口,从列表里按 id 捞
  const fetchCard = async id => {
    const res = await listFuelCardPage({ current: -1, size: -1 });
    const rows = res?.data?.records || res?.records || res?.data || [];
    const found = (Array.isArray(rows) ? rows : []).find(
      item => String(item.id) === String(id)
    );
    return { data: found };
  };
 
  const loadDetail = async () => {
    if (!cardId.value) return;
    let data = null;
    try {
      data = await loadDetailData({
        cacheKey: "fuelCardDetail",
        id: cardId.value,
        fetcher: fetchCard,
      });
    } catch (error) {
      data = null;
    }
    if (!data) {
      uni.showToast({ title: "获取加油卡详情失败", icon: "none" });
      return;
    }
    form.value = { ...form.value, ...data, id: data.id };
  };
 
  const handleSubmit = async () => {
    const valid = await formRef.value.validate().catch(() => false);
    if (!valid) return;
 
    loading.value = true;
    const action = cardId.value ? updateFuelCard : addFuelCard;
    action({ ...form.value, id: cardId.value || undefined })
      .then(() => {
        uni.showToast({ title: "保存成功", icon: "success" });
        setTimeout(() => uni.navigateBack(), 300);
      })
      .catch(() => {
        uni.showToast({ title: "保存失败", icon: "none" });
      })
      .finally(() => {
        loading.value = false;
      });
  };
 
  onLoad(options => {
    if (options?.id) {
      cardId.value = options.id;
      form.value.id = options.id;
    }
  });
 
  onMounted(async () => {
    await loadDepts();
    if (cardId.value) await loadDetail();
  });
</script>
 
<style scoped lang="scss">
  @import "@/static/scss/form-common.scss";
 
  .account-detail {
    min-height: 100vh;
    background: #f8f9fa;
    padding-bottom: 100px;
  }
 
  .form-container {
    padding: 12px 12px 0;
  }
 
  .form-section {
    margin-bottom: 12px;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 2px 10px rgba(15, 35, 95, 0.05);
  }
 
  :deep(.u-cell-group__title) {
    padding: 14px 18px 10px !important;
    font-size: 15px !important;
    font-weight: 600 !important;
    color: #22324d !important;
    background: #f8fbff !important;
  }
</style>