gaoluyang
2026-11-03 2d8b1a28fa5c8b852f0d0078bac4894bfb4abed5
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
<script setup lang="ts">
  import { computed, reactive, ref } from "vue";
  import { onLoad } from "@dcloudio/uni-app";
  import {
    createPurchase,
    getAccountSimpleList,
    getPurchase,
    getPurchaseInvoiceSimpleList,
    getPurchaseOrderPage,
    getSimpleUserList,
    updatePurchase,
    type AccountSimple,
    type PurchaseInvoiceSimple,
    type PurchaseItem,
    type PurchaseKind,
    type PurchaseRecord,
    type UserSimple,
  } from "../../../api/purchase";
  import { getMaterialPage, type MaterialItem } from "../../../api/basic-data";
  import {
    getSupplierSimpleList,
    type SupplierItem,
  } from "../../../api/supplier";
  import { useAuth } from "../../../stores/auth";
  import { materialOptionText } from "../../../utils/material";
 
  const props = defineProps<{ kind: PurchaseKind }>();
  const { state } = useAuth();
  const id = ref<number>();
  const saving = ref(false);
  const suppliers = ref<SupplierItem[]>([]);
  const materials = ref<MaterialItem[]>([]);
  const orders = ref<PurchaseRecord[]>([]);
  const invoices = ref<PurchaseInvoiceSimple[]>([]);
  const accounts = ref<AccountSimple[]>([]);
  const users = ref<UserSimple[]>([]);
  const form = reactive<PurchaseRecord>({
    items: [],
    blobIds: [],
    attachmentList: [],
  });
  const title = computed(
    () =>
      ({
        request: "采购申请",
        order: "采购订单",
        invoice: "采购来票",
        payment: "采购付款",
      }[props.kind])
  );
  const withItems = computed(() =>
    ["request", "order", "payment"].includes(props.kind)
  );
  const materialOptions = computed(() => materials.value.map(item => materialOptionText(item, "请选择产品")));
  const userLabel = (user: UserSimple) =>
    user.nickname || user.username || `用户 #${user.id}`;
  const today = () => {
    const date = new Date();
    const pad = (value: number) => String(value).padStart(2, "0");
    return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(
      date.getDate()
    )}`;
  };
  const dateTime = (value?: string) =>
    value ? (value.includes(" ") ? value : `${value} 00:00:00`) : value;
  const message = (error: unknown) =>
    error instanceof Error ? error.message : "操作失败";
  function num(value: string) {
    const result = Number(value);
    return Number.isFinite(result) ? result : 0;
  }
  function requestItemAmount(item: PurchaseItem) {
    return (item.productPrice || 0) * (item.count || 0);
  }
  function productText(item: PurchaseItem) {
    return materialOptionText({
      productName: item.productName,
      productCode: item.productCode,
      productSpecification: item.productSpecification,
      productUnitName: item.productUnitName,
    }, "请选择产品");
  }
  function orderItemAmounts(item: PurchaseItem) {
    const productAmount = (item.productPrice || 0) * (item.count || 0);
    const taxAmount = productAmount * ((item.taxPercent || 0) / 100);
    return { productAmount, taxAmount, totalAmount: productAmount + taxAmount };
  }
  function updateOrderAmounts() {
    if (props.kind !== "order") return;
    const gross = (form.items || []).reduce(
      (sum, item) => sum + orderItemAmounts(item).totalAmount,
      0
    );
    const discount = gross * ((form.discountPercent || 0) / 100);
    form.discountPrice = discount;
    form.totalPrice = gross - discount;
  }
  function addItem() {
    form.items!.push({
      count: 1,
      productPrice: 0,
      taxPercent: 0,
      paymentPrice: 0,
      paidPrice: 0,
      qcCheckFlag: false,
    });
    updateOrderAmounts();
  }
  function removeItem(index: number) {
    form.items!.splice(index, 1);
    updateOrderAmounts();
  }
  async function selectSupplier(event: { detail: { value: string } }) {
    const supplier = suppliers.value[Number(event.detail.value)];
    form.supplierId = supplier?.id;
    form.supplierName = supplier?.name;
    if (props.kind === "payment" && state.accessToken && supplier?.id) {
      invoices.value = await getPurchaseInvoiceSimpleList(
        state.accessToken,
        supplier.id
      );
      form.invoiceId = undefined;
      form.items = [];
      form.totalPrice = 0;
      form.paymentPrice = 0;
    }
  }
  function selectOrder(event: { detail: { value: string } }) {
    const order = orders.value[Number(event.detail.value)];
    if (!order) return;
    form.purchaseOrderId = order.id;
    form.order = { id: order.id, no: order.no };
    form.supplierId = order.supplierId;
    form.supplierName = order.supplierName;
  }
  function selectInvoice(event: { detail: { value: string } }) {
    const invoice = invoices.value[Number(event.detail.value)];
    if (!invoice) return;
    const remaining = invoice.remainingInvoicePrice || 0;
    form.invoiceId = invoice.id;
    form.invoice = {
      no: invoice.no,
      invoiceNo: invoice.invoiceNo,
      remainingInvoicePrice: remaining,
    };
    form.items = [
      {
        bizType: 13,
        bizId: invoice.id,
        bizNo: invoice.no,
        totalPrice: invoice.price || remaining,
        paidPrice: Math.max((invoice.price || remaining) - remaining, 0),
        paymentPrice: remaining,
      },
    ];
    form.totalPrice = remaining;
    updatePaymentPrice();
  }
  function selectAccount(event: { detail: { value: string } }) {
    const account = accounts.value[Number(event.detail.value)];
    form.accountId = account?.id;
    form.accountName = account?.name;
  }
  function selectUser(event: { detail: { value: string } }) {
    const user = users.value[Number(event.detail.value)];
    form.financeUserId = user?.id;
    form.financeUserName = user ? userLabel(user) : undefined;
  }
  function selectMaterial(event: { detail: { value: string } }, index: number) {
    const material = materials.value[Number(event.detail.value)];
    const item = form.items![index];
    if (!material) return;
    item.productId = material.id;
    item.productName = material.name;
    item.productCode = material.code;
    item.productSpecification = material.specification;
    item.productUnitId = material.unitMeasureId;
    item.productUnitName = material.unitMeasureName;
    item.productPrice = material.purchasePrice || 0;
    updateOrderAmounts();
  }
  function updatePaymentPrice() {
    const total = (form.items || []).reduce(
      (sum, item) => sum + (item.paymentPrice || 0),
      0
    );
    form.totalPrice = total;
    form.paymentPrice = total - (form.discountPrice || 0);
  }
  async function load() {
    if (!state.accessToken) return;
    try {
      const tasks: Promise<unknown>[] = [
        getSupplierSimpleList(state.accessToken),
        getMaterialPage(state.accessToken, {
          pageNo: 1,
          pageSize: 100,
          status: 0,
        }),
      ];
      if (props.kind === "invoice")
        tasks.push(
          getPurchaseOrderPage(state.accessToken, {
            pageNo: 1,
            pageSize: 100,
            status: 20,
          })
        );
      if (props.kind === "order" || props.kind === "payment")
        tasks.push(getAccountSimpleList(state.accessToken));
      if (props.kind === "payment")
        tasks.push(getSimpleUserList(state.accessToken));
      const [supplierData, materialData, ...extras] = await Promise.all(tasks);
      suppliers.value = supplierData as SupplierItem[];
      materials.value = (materialData as { list: MaterialItem[] }).list;
      let extraIndex = 0;
      if (props.kind === "invoice")
        orders.value = (extras[extraIndex++] as { list: PurchaseRecord[] }).list;
      if (props.kind === "order" || props.kind === "payment")
        accounts.value = extras[extraIndex++] as AccountSimple[];
      if (props.kind === "payment")
        users.value = extras[extraIndex++] as UserSimple[];
      if (id.value) {
        Object.assign(
          form,
          await getPurchase(state.accessToken, props.kind, id.value)
        );
        form.blobIds =
          form.attachmentList?.map(item => item.id) || form.blobIds || [];
        if (props.kind === "payment" && form.supplierId)
          invoices.value = await getPurchaseInvoiceSimpleList(
            state.accessToken,
            form.supplierId
          );
      } else {
        if (props.kind === "request") form.requestTime = today();
        else if (props.kind === "order") {
          form.orderTime = today();
          const account = accounts.value.find(item => item.defaultStatus);
          form.accountId = account?.id;
          form.accountName = account?.name;
        } else if (props.kind === "invoice") form.invoiceTime = today();
        else {
          form.paymentTime = today();
          form.discountPrice = 0;
          const account = accounts.value.find(item => item.defaultStatus);
          form.accountId = account?.id;
          form.accountName = account?.name;
        }
        if (withItems.value && props.kind !== "payment") addItem();
      }
    } catch (error) {
      uni.showToast({ title: message(error), icon: "none" });
    }
  }
  function payload(): PurchaseRecord {
    updateOrderAmounts();
    if (props.kind === "request") {
      return {
        no: form.no,
        requestTime: dateTime(form.requestTime),
        supplierId: form.supplierId,
        discountPercent: form.discountPercent,
        depositPrice: form.depositPrice,
        requestReason: form.requestReason,
        blobIds: form.blobIds,
        remark: form.remark,
        items: form.items?.map(item => ({
          productId: item.productId,
          productUnitId: item.productUnitId,
          productPrice: item.productPrice,
          count: item.count,
          taxPercent: item.taxPercent,
          demandTime: item.demandTime,
          qcCheckFlag: item.qcCheckFlag,
          remark: item.remark,
        })),
      };
    }
    const data: PurchaseRecord = {
      ...form,
      items: form.items?.map(item => ({ ...item })),
    };
    data.requestTime = dateTime(data.requestTime);
    data.orderTime = dateTime(data.orderTime);
    data.invoiceTime = dateTime(data.invoiceTime);
    data.paymentTime = dateTime(data.paymentTime);
    data.items?.forEach(item => {
      item.demandTime = dateTime(item.demandTime);
      if (props.kind === "order")
        item.totalPrice = orderItemAmounts(item).totalAmount;
    });
    return data;
  }
  async function save() {
    if (!state.accessToken || saving.value) return;
    if (props.kind === "invoice" && !form.purchaseOrderId)
      return uni.showToast({ title: "请选择采购订单", icon: "none" });
    if (
      props.kind === "invoice" &&
      (!form.invoiceNo || !form.price || form.price <= 0)
    )
      return uni.showToast({ title: "请填写发票号码和有效金额", icon: "none" });
    if (
      props.kind === "payment" &&
      (!form.supplierId ||
        !form.invoiceId ||
        !form.accountId ||
        form.discountPrice === undefined ||
        !form.items?.length)
    )
      return uni.showToast({
        title: "请补全供应商、来票、账户、优惠和付款明细",
        icon: "none",
      });
    if ((props.kind === "request" || props.kind === "order") && !form.supplierId)
      return uni.showToast({ title: "请选择供应商", icon: "none" });
    if (props.kind === "order" && (form.depositPrice || 0) > (form.totalPrice || 0))
      return uni.showToast({ title: "支付订金不能大于优惠后金额", icon: "none" });
    if (withItems.value && !form.items?.length)
      return uni.showToast({ title: "请至少添加一条明细", icon: "none" });
    if (props.kind === "request" || props.kind === "order") {
      const invalidIndex = form.items?.findIndex(
        item => !item.productId || !item.count || item.count <= 0
      );
      if (invalidIndex !== undefined && invalidIndex >= 0)
        return uni.showToast({
          title: `请补全第 ${invalidIndex + 1} 条明细的物料和数量`,
          icon: "none",
        });
    }
    if (props.kind === "payment") {
      const amount = form.items![0].paymentPrice || 0;
      const remaining = form.invoice?.remainingInvoicePrice || 0;
      if (amount <= 0 || amount > remaining || (form.paymentPrice || 0) < 0)
        return uni.showToast({ title: "付款金额或优惠金额不正确", icon: "none" });
    }
    saving.value = true;
    try {
      const data = payload();
      if (id.value)
        await updatePurchase(state.accessToken, props.kind, {
          ...data,
          id: id.value,
        });
      else await createPurchase(state.accessToken, props.kind, data);
      uni.showToast({ title: "保存成功", icon: "success" });
      setTimeout(() => uni.navigateBack(), 400);
    } catch (error) {
      uni.showToast({ title: message(error), icon: "none" });
    } finally {
      saving.value = false;
    }
  }
  onLoad(query => {
    id.value = query.id ? Number(query.id) : undefined;
    load();
  });
</script>
 
<template>
  <view class="page">
    <view class="form-title">
      <text>{{ id ? '编辑' : '新增' }}{{ title }}</text>
      <text class="form-subtitle">带 * 的项目为必填项</text>
    </view>
    <view class="card">
      <view class="card-title">基础信息</view>
      <view class="field-grid">
        <view v-if="kind === 'request' || kind === 'order'"
              class="field">
          <text>单据编号</text>
          <view class="readonly">{{ form.no || '系统自动生成' }}</view>
        </view>
        <view v-if="kind === 'request' || kind === 'order' || kind === 'payment'"
              class="field required">
          <text>供应商</text>
          <picker :range="suppliers"
                  range-key="name"
                  @change="selectSupplier">
            <view class="picker-value"><text>{{ form.supplierName || '请选择供应商' }}</text><text>›</text></view>
          </picker>
        </view>
        <view v-if="kind === 'request'"
              class="field required">
          <text>申请时间</text>
          <picker mode="date"
                  :value="form.requestTime"
                  @change="form.requestTime = $event.detail.value">
            <view class="picker-value"><text>{{ form.requestTime || '请选择申请时间' }}</text><text>›</text></view>
          </picker>
        </view>
        <template v-if="kind === 'request'">
          <view class="field">
            <text>优惠率(%)</text>
            <input :value="String(form.discountPercent || '')"
                   type="digit"
                   placeholder="请输入优惠率"
                   @input="form.discountPercent = num($event.detail.value)" />
          </view>
          <view class="field">
            <text>定金</text>
            <input :value="String(form.depositPrice || '')"
                   type="digit"
                   placeholder="请输入定金"
                   @input="form.depositPrice = num($event.detail.value)" />
          </view>
        </template>
        <template v-if="kind === 'order'">
          <view class="field required">
            <text>订单时间</text>
            <picker mode="date"
                    :value="form.orderTime"
                    @change="form.orderTime = $event.detail.value">
              <view class="picker-value"><text>{{ form.orderTime || '请选择订单时间' }}</text><text>›</text></view>
            </picker>
          </view>
          <view class="field">
            <text>结算账户</text>
            <picker :range="accounts"
                    range-key="name"
                    @change="selectAccount">
              <view class="picker-value"><text>{{ form.accountName || '请选择结算账户' }}</text><text>›</text></view>
            </picker>
          </view>
          <view class="field">
            <text>支付订金</text>
            <input :value="String(form.depositPrice || '')"
                   type="digit"
                   placeholder="请输入支付订金"
                   @input="form.depositPrice = num($event.detail.value)" />
          </view>
          <view class="field">
            <text>优惠率(%)</text>
            <input :value="String(form.discountPercent || '')"
                   type="digit"
                   placeholder="请输入优惠率"
                   @input="form.discountPercent = num($event.detail.value); updateOrderAmounts()" />
          </view>
          <view class="field">
            <text>付款优惠</text>
            <view class="readonly">¥ {{ (form.discountPrice || 0).toFixed(2) }}</view>
          </view>
          <view class="field">
            <text>优惠后金额</text>
            <view class="readonly emphasis">¥ {{ (form.totalPrice || 0).toFixed(2) }}</view>
          </view>
        </template>
        <template v-if="kind === 'invoice'">
          <view class="field required">
            <text>采购订单</text>
            <picker :range="orders"
                    range-key="no"
                    @change="selectOrder">
              <view class="picker-value"><text>{{ form.order?.no || '请选择已审核订单' }}</text><text>›</text></view>
            </picker>
          </view>
          <view class="field">
            <text>供应商</text>
            <view class="readonly">{{ form.supplierName || '选择订单后自动带出' }}</view>
          </view>
          <view class="field required"><text>发票号码</text><input v-model="form.invoiceNo"
                   placeholder="请输入发票号码" /></view>
          <view class="field"><text>发票抬头</text><input v-model="form.invoiceTitle"
                   placeholder="请输入发票抬头" /></view>
          <view class="field required"><text>来票金额(元)</text><input :value="String(form.price || '')"
                   type="digit"
                   placeholder="请输入来票金额"
                   @input="form.price = num($event.detail.value)" /></view>
          <view class="field"><text>来票日期</text>
            <picker mode="date"
                    :value="form.invoiceTime"
                    @change="form.invoiceTime = $event.detail.value">
              <view class="picker-value"><text>{{ form.invoiceTime || '请选择来票日期' }}</text><text>›</text></view>
            </picker>
          </view>
        </template>
        <template v-if="kind === 'payment'">
          <view class="field"><text>付款单号</text>
            <view class="readonly">{{ form.no || '系统自动生成' }}</view>
          </view>
          <view class="field required"><text>关联来票</text>
            <picker :range="invoices"
                    range-key="invoiceNo"
                    @change="selectInvoice">
              <view class="picker-value"><text>{{ form.invoice?.invoiceNo || form.invoice?.no || '请先选择供应商' }}</text><text>›</text></view>
            </picker>
          </view>
          <view class="field required"><text>付款账户</text>
            <picker :range="accounts"
                    range-key="name"
                    @change="selectAccount">
              <view class="picker-value"><text>{{ form.accountName || '请选择结算账户' }}</text><text>›</text></view>
            </picker>
          </view>
          <view class="field"><text>财务人员</text>
            <picker :range="users"
                    range-key="nickname"
                    @change="selectUser">
              <view class="picker-value"><text>{{ form.financeUserName || '请选择财务人员' }}</text><text>›</text></view>
            </picker>
          </view>
          <view class="field"><text>合计付款</text>
            <view class="readonly">¥ {{ (form.totalPrice || 0).toFixed(2) }}</view>
          </view>
          <view class="field"><text>优惠金额</text><input :value="String(form.discountPrice || 0)"
                   type="digit"
                   @input="form.discountPrice = num($event.detail.value); updatePaymentPrice()" /></view>
          <view class="field"><text>实际付款</text>
            <view class="readonly emphasis">¥ {{ (form.paymentPrice || 0).toFixed(2) }}</view>
          </view>
          <view class="field"><text>付款时间</text>
            <picker mode="date"
                    :value="form.paymentTime"
                    @change="form.paymentTime = $event.detail.value">
              <view class="picker-value"><text>{{ form.paymentTime || '请选择付款日期' }}</text><text>›</text></view>
            </picker>
          </view>
        </template>
      </view>
      <view v-if="kind === 'request'"
            class="field full-field"><text>申请理由</text><textarea v-model="form.requestReason"
                  placeholder="请输入申请理由" ></textarea></view>
      <view class="field full-field"><text>备注</text><textarea v-model="form.remark"
                  placeholder="请输入备注" ></textarea></view>
    </view>
    <view v-if="withItems"
          class="card">
      <view class="section"><text>{{ kind === 'payment' ? '付款明细' : kind === 'request' ? '申请明细' : '采购产品清单' }}</text><button v-if="kind !== 'payment'"
                @click="addItem">添加{{ kind === 'request' ? '申请' : '采购' }}产品</button></view>
      <view v-for="(item, index) in form.items"
            :key="item.id || index"
            class="item">
        <view class="item-head"><text>明细 {{ index + 1 }}</text><button v-if="kind !== 'payment'"
                  class="danger"
                  @click="removeItem(index)">删除</button></view>
        <template v-if="kind !== 'payment'">
          <view class="item-grid">
            <view class="item-field wide"><text>产品名称 <text class="required">*</text></text>
              <picker :range="materialOptions"
                      @change="selectMaterial($event, index)">
                <view class="picker-value"><text>{{ productText(item) }}</text><text>›</text></view>
              </picker>
            </view>
            <view class="item-field"><text>单位</text>
              <view class="readonly">{{ item.productUnitName || '--' }}</view>
            </view>
            <view class="item-field"><text>数量 <text class="required">*</text></text><input :value="String(item.count || '')"
                     type="digit"
                     placeholder="请输入数量"
                     @input="item.count = num($event.detail.value); updateOrderAmounts()" /></view>
            <view class="item-field"><text>{{ kind === 'request' ? '单价(参考价)' : '产品单价' }}</text><input :value="String(item.productPrice || '')"
                     type="digit"
                     placeholder="请输入单价"
                     @input="item.productPrice = num($event.detail.value); updateOrderAmounts()" /></view>
            <view class="item-field"><text>税率(%)</text><input :value="String(item.taxPercent || 0)"
                     type="digit"
                     @input="item.taxPercent = num($event.detail.value); updateOrderAmounts()" /></view>
            <view class="item-field"><text>{{ kind === 'request' ? '预估金额' : '金额' }}</text>
              <view class="readonly">¥ {{ kind === 'request' ? requestItemAmount(item).toFixed(2) : orderItemAmounts(item).productAmount.toFixed(2) }}</view>
            </view>
            <view v-if="kind === 'order'"
                  class="item-field"><text>税额</text>
              <view class="readonly">¥ {{ orderItemAmounts(item).taxAmount.toFixed(2) }}</view>
            </view>
            <view v-if="kind === 'order'"
                  class="item-field"><text>含税金额</text>
              <view class="readonly emphasis">¥ {{ orderItemAmounts(item).totalAmount.toFixed(2) }}</view>
            </view>
            <view v-if="kind === 'request'"
                  class="item-field"><text>需求日期</text>
              <picker mode="date"
                      :value="item.demandTime"
                      @change="item.demandTime = $event.detail.value">
                <view class="picker-value"><text>{{ item.demandTime || '请选择需求日期' }}</text><text>›</text></view>
              </picker>
            </view>
            <view class="item-field"><text>是否质检</text>
              <view class="check"
                    @click="item.qcCheckFlag = !item.qcCheckFlag">{{ item.qcCheckFlag ? '是' : '否' }}</view>
            </view>
          </view>
          <view class="item-field full-item"><text>明细备注</text><textarea v-model="item.remark"
                      placeholder="请输入明细备注" ></textarea></view>
        </template>
        <template v-else>
          <view class="payment-item"><text>{{ form.invoice?.invoiceNo || form.invoice?.no || '未选择来票' }}</text><text>剩余可付:¥ {{ (form.invoice?.remainingInvoicePrice || 0).toFixed(2) }}</text>
            <view class="item-field"><text>本次付款金额</text><input :value="String(item.paymentPrice || '')"
                     type="digit"
                     placeholder="请输入本次付款金额"
                     @input="item.paymentPrice = num($event.detail.value); updatePaymentPrice()" /></view>
          </view>
        </template>
      </view>
      <view v-if="kind === 'request' || kind === 'order'"
            class="summary"><text>合计</text>
        <view><text>数量:{{ (form.items || []).reduce((sum, item) => sum + (item.count || 0), 0).toFixed(3) }}</text><text>金额:¥ {{ (form.items || []).reduce((sum, item) => sum + (kind === 'request' ? requestItemAmount(item) : orderItemAmounts(item).totalAmount), 0).toFixed(2) }}</text></view>
      </view>
    </view>
    <button class="save"
            :loading="saving"
            @click="save">保存{{ title }}</button>
  </view>
</template>
 
<style lang="scss" scoped>
  .page {
    min-height: 100vh;
    padding: 28rpx 30rpx 150rpx;
    box-sizing: border-box;
    background: #f5f6f7;
  }
  .form-title {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    padding: 8rpx 4rpx 22rpx;
    color: #1d2129;
    font-size: 34rpx;
    font-weight: 600;
  }
  .form-subtitle {
    color: #86909c;
    font-size: 22rpx;
    font-weight: 400;
  }
  .card {
    margin-bottom: 20rpx;
    overflow: hidden;
    background: #fff;
    border-radius: 16rpx;
  }
  .card-title,
  .section {
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 88rpx;
    padding: 0 24rpx;
    color: #1d2129;
    font-size: 29rpx;
    font-weight: 600;
    border-bottom: 1rpx solid #f2f3f5;
  }
  .field-grid,
  .item-grid {
    display: flex;
    flex-wrap: wrap;
    padding: 0 24rpx;
  }
  .field,
  .item-field {
    width: calc(50% - 10rpx);
    padding: 20rpx 0;
    box-sizing: border-box;
  }
  .field:nth-child(odd),
  .item-field:nth-child(odd) {
    margin-right: 20rpx;
  }
  .field > text,
  .item-field > text {
    display: block;
    margin-bottom: 12rpx;
    color: #4e5969;
    font-size: 24rpx;
    line-height: 34rpx;
  }
  .required > text::before {
    margin-right: 6rpx;
    color: #f53f3f;
    content: "*";
  }
  .field input,
  .field textarea,
  .item-field input,
  .item-field textarea,
  .readonly,
  .picker-value {
    width: 100%;
    min-height: 64rpx;
    padding: 0 16rpx;
    box-sizing: border-box;
    color: #1d2129;
    font-size: 26rpx;
    line-height: 64rpx;
    background: #f7f8fa;
    border-radius: 8rpx;
  }
  .picker-value,
  .readonly {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .readonly {
    color: #4e5969;
  }
  .emphasis {
    color: var(--color-primary, #165dff);
    font-weight: 600;
  }
  .full-field,
  .wide,
  .full-item {
    width: 100%;
    margin-right: 0 !important;
  }
  .full-field {
    padding: 0 24rpx 22rpx;
    box-sizing: border-box;
  }
  .full-field textarea,
  .full-item textarea {
    height: 128rpx;
    padding-top: 14rpx;
    line-height: 36rpx;
  }
  .item {
    margin: 20rpx 24rpx 0;
    padding: 0 20rpx 20rpx;
    background: #f7f8fa;
    border-radius: 12rpx;
  }
  .item-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 76rpx;
    color: #1d2129;
    font-size: 27rpx;
    font-weight: 600;
  }
  .item-grid {
    padding: 0;
  }
  .item-field {
    padding: 12rpx 0;
  }
  .item-field:nth-child(odd) {
    margin-right: 20rpx;
  }
  .item-field.full-item {
    padding-bottom: 0;
  }
  .check {
    min-height: 64rpx;
    padding: 0 16rpx;
    color: var(--color-primary, #165dff);
    font-size: 26rpx;
    line-height: 64rpx;
    background: #edf4ff;
    border-radius: 8rpx;
  }
  .payment-item {
    color: #4e5969;
    font-size: 25rpx;
  }
  .payment-item > text {
    display: block;
    padding-top: 14rpx;
  }
  .summary {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 24rpx;
    margin-top: 20rpx;
    color: #1d2129;
    font-size: 27rpx;
    font-weight: 600;
    background: #edf4ff;
  }
  .summary view {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    color: var(--color-primary, #165dff);
    font-size: 24rpx;
    font-weight: 400;
    line-height: 38rpx;
  }
  .section button,
  .item button {
    margin: 0;
    padding: 0 16rpx;
    color: var(--color-primary, #165dff);
    font-size: 23rpx;
    line-height: 52rpx;
    background: #edf4ff;
    border-radius: 8rpx;
  }
  .danger {
    color: #f53f3f !important;
  }
  .attachments {
    padding-bottom: 8rpx;
  }
  .attachment {
    display: flex;
    justify-content: space-between;
    padding: 18rpx 24rpx;
    color: var(--color-primary, #165dff);
    font-size: 25rpx;
    border-bottom: 1rpx solid #f2f3f5;
  }
  .attachment text:first-child {
    max-width: 78%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .empty {
    display: block;
    padding: 18rpx 24rpx;
    color: #86909c;
    font-size: 24rpx;
  }
  .save {
    position: fixed;
    right: 30rpx;
    bottom: calc(24rpx + env(safe-area-inset-bottom));
    left: 30rpx;
    z-index: 10;
    height: 84rpx;
    color: #fff;
    font-size: 30rpx;
    line-height: 84rpx;
    background: var(--color-primary, #165dff);
    border-radius: 14rpx;
  }
  .save::after,
  button::after {
    border: 0;
  }
</style>