peng
6 天以前 6863a4a507d5c78d617a30d93f35279ad97b79f3
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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
<template>
  <div>
    <Card>
      <Form
        ref="searchForm"
        @keydown.enter.native="handleSearch"
        :model="searchForm"
        inline
        :label-width="70"
        class="search-form"
      >
        <Form-item label="套餐名称" prop="comboName">
          <Input
            type="text"
            v-model="searchForm.comboName"
            clearable
            @on-clear="handleSearch"
            @on-change="handleSearch"
            style="width: 160px"
          />
        </Form-item>
        <Form-item label="分类" prop="tagId">
          <Select
            v-model="searchForm.tagId"
            placeholder="请选择分类"
            clearable
            style="width: 160px"
            filterable
          >
            <Option
              v-for="tag in tagList"
              :key="tag.id"
              :label="tag.tagName"
              :value="tag.id"
            >
              {{ tag.tagName }}
            </Option>
          </Select>
        </Form-item>
        <Form-item label="启用状态" prop="status">
          <Select
            v-model="searchForm.status"
            placeholder="全部状态"
            clearable
            style="width: 160px"
          >
            <Option value="ENABLE">启用</Option>
            <Option value="DISABLE">禁用</Option>
          </Select>
        </Form-item>
        <Button
          @click="handleSearch"
          type="primary"
          icon="ios-search"
          class="search-btn"
        >搜索
        </Button
        >
      </Form>
 
      <Row class="operation padding-row">
        <Button @click="openAdd" type="info">添加</Button>
        <Button @click="delBatch" type="error">批量删除</Button>
      </Row>
 
      <Modal
        v-model="modelShow"
        :title="modelTitle"
        width="800"
      >
        <Form ref="form" :model="form" :label-width="70" :rules="rules">
          <Row :gutter="24">
            <Col span="12">
              <FormItem label="套餐名字" prop="comboName">
                <Input v-model="form.comboName" autocomplete="off"/>
              </FormItem>
            </Col>
            <Col span="12">
              <FormItem label="备注" prop="remark">
                <Input v-model="form.remark" autocomplete="off"/>
              </FormItem>
            </Col>
          </Row>
          <Row :gutter="24">
            <Col span="12">
              <FormItem label="人数" prop="num">
                <Input v-model="form.num" autocomplete="off"/>
              </FormItem>
            </Col>
            <Col span="12">
              <FormItem label="套餐封面" prop="coverImg">
                <Upload
                  v-if="!coverTempUrl"
                  :before-upload="(file) => handleBeforeUpload(file, 'cover')"
                  :format="['jpg','jpeg','png','gif']"
                  :max-size="20480"
                  action=""
                  accept="image/*"
                >
                  <Button icon="ios-cloud-upload-outline">上传封面图片</Button>
                </Upload>
                <div v-else class="upload-file-info">
                  <img :src="coverTempUrl" alt="封面图片" class="preview-image-limit">
                  <Button type="text" @click="handleRemove('cover')">删除</Button>
                </div>
              </FormItem>
            </Col>
          </Row>
          <Row :gutter="24">
            <Col span="12">
              <FormItem label="原价" prop="orginPrice">
                <Input v-model="form.orginPrice" autocomplete="off"/>
              </FormItem>
            </Col>
            <Col span="12">
              <FormItem label="私厨分类" prop="tagId">
                <Select
                  v-model="form.tagId"
                  placeholder="请选择分类"
                  clearable
                  style="width: 100%"
                  filterable
                >
                  <Option
                    v-for="tag in tagList"
                    :key="tag.id"
                    :label="tag.tagName"
                    :value="tag.id"
                  >
                    {{ tag.tagName }}
                  </Option>
                </Select>
              </FormItem>
            </Col>
          </Row>
          <Row :gutter="24">
            <Col span="12">
              <FormItem label="启用状态" prop="status">
                <Select v-model="form.status" placeholder="请选择状态">
                  <Option value="ENABLE">启用</Option>
                  <Option value="DISABLE">禁用</Option>
                </Select>
              </FormItem>
            </Col>
            <Col span="12">
              <FormItem label="排序" prop="sort">
                <Input v-model="form.sort" autocomplete="off"/>
              </FormItem>
            </Col>
          </Row>
          <Row :gutter="24">
            <Col span="24">
              <FormItem label="已选商品" :label-width="70">
                <div style="height: 100px;overflow: auto;" v-if="form.goods">
                  <div style="display: flex;
                          align-items: center;justify-content: flex-start;border: 1px solid gray;margin-top: 10px;
                          padding: 10px;border-radius: 20px">
                    <div>
                      <img :src="getThumbnailUrl(form.goods.thumbnail)"
                           style="width: 80px;height: 80px">
                    </div>
                    <div style="display: flex;flex-direction: column;margin-left: 20px">
                      <div style="font-size: 1.5em;font-weight: bold">{{ form.goods.goodsName }}</div>
                      <div style="color: #ff6f6f">
                        ¥{{ form.goods.price }}
                      </div>
                    </div>
                    <div style="margin-left: 10px">
                      <Button type="error" size="small" @click="removeGoods">移除</Button>
                    </div>
                  </div>
                </div>
                <div v-else style="color: #999; padding: 10px;">
                  暂无已选商品
                </div>
              </FormItem>
            </Col>
          </Row>
          <Row :gutter="24">
            <Col span="24">
              <FormItem label="绑定商品" :label-width="70">
                <Input v-model="searchGoodsForm.keyword" style="width:200px" @on-change="searchGoodsList"></Input>
                <div style="height: 300px;overflow: auto;">
                  <div v-for="item in goodsData" :key="item.id" style="display: flex;
                          align-items: center;justify-content: flex-start;border: 1px solid gray;margin-top: 10px;
                          padding: 10px;border-radius: 20px" @click="chooseGoods(item.id)">
                    <div>
                      <img :src="getThumbnailUrl(item.thumbnail)" style="width: 80px;height: 80px">
                    </div>
                    <div style="display: flex;flex-direction: column;margin-left: 20px">
                      <div style="font-size: 1.5em;font-weight: bold">{{ item.goodsName }}</div>
                      <div style="color: #ff6f6f">
                        ¥{{ item.price }}
                      </div>
                    </div>
                  </div>
                </div>
                <Page
                  :current="searchGoodsForm.pageNumber"
                  :total="goodsTotal"
                  :page-size="searchGoodsForm.pageSize"
                  @on-change="goodsChangePage"
                  @on-page-size-change="goodsChangePageSize"
                  :page-size-opts="[10, 20, 50]"
                  size="small"
                  show-total
                ></Page>
              </FormItem>
            </Col>
          </Row>
        </Form>
        <div slot="footer">
          <Button type="text" @click="modelClose">取消</Button>
          <Button type="primary" :loading="submitLoading" @click="saveOrUpdate">提交</Button>
        </div>
      </Modal>
 
      <Table
        :loading="loading"
        border
        :columns="columns"
        :data="data"
        ref="table"
        sortable="custom"
        @on-sort-change="changeSort"
        @on-selection-change="showSelect"
      >
        <template slot-scope="{ row, index }" slot="action">
          <Button type="info" size="small" style="margin-right: 5px" @click="openEdit(row)">编辑</Button>
          <Button
            v-if="row.status === 'DISABLE'"
            type="success"
            size="small"
            style="margin-right: 5px"
            @click="enableGoods(row)"
          >启用</Button>
          <Button
            v-if="row.status === 'ENABLE'"
            type="warning"
            size="small"
            style="margin-right: 5px"
            @click="disableGoods(row)"
          >禁用</Button>
          <Button type="error" size="small" @click="delById(row)">删除</Button>
        </template>
      </Table>
 
      <Row type="flex" justify="end" class="mt_10">
        <Page
          :current="searchForm.pageNumber"
          :total="total"
          :page-size="searchForm.pageSize"
          @on-change="changePage"
          @on-page-size-change="changePageSize"
          :page-size-opts="[10, 20, 50]"
          size="small"
          show-total
          show-elevator
          show-sizer
        ></Page>
      </Row>
    </Card>
  </div>
</template>
 
<script>
import JsonExcel from "vue-json-excel";
import {deleteVideoTagById, getVideoTags, editVideoTag, addVideoTag, deleteVideoTagByIds} from "@/api/videoTag";
import {addKitchenType, deleteBatch, delKitchenType, editKitchenType, getKitchenGoodsPage, addKitchenGoods, editKitchenGoods, delKitchenGoods, deleteGoodsBatch, getKitchenTagList, editKitchenGoodsStaus} from "@/api/kitchen";
import {videoGoodsEsPage, recommend} from "@/api/videoTag";
import {getSts} from "@/api/file";
import {uploadFileByLmk} from "@/api/common";
 
export default {
  name: "KitchenCustomize",
  components: {
    "download-excel": JsonExcel,
    uploadPicInput: () => import('@/components/lili/upload-pic-input.vue'),
  },
  data() {
    return {
      // 保存加载
      submitLoading: false,
      // 新增修改表单
      form: {
        id: '',
        goodsId: '', // 商品id - 选择商品后自动填充
        skuId: '', // skuId - 选择商品后自动填充
        comboName: '', // 套餐名字
        remark: '', // 备注
        num: '', // 人数
        coverImg: '', // 封面图片
        orginPrice: '', // 原价
        tagId: '', // 标签id
        status: '', // 启用状态
        sort: '', // 排序
        goods: null, // 绑定商品
      },
      rules: {
        comboName: [
          {
            required: true, message: "套餐名字不能为空", trigger: "blur",
            validator: (rule, value, callback) => {
              if (value === null || value === '') {
                callback(new Error('套餐名字不能为空'));
              } else {
                callback();
              }
            }
          }
        ],
        remark: [
          {
            required: true, message: "备注不能为空", trigger: "blur",
            validator: (rule, value, callback) => {
              if (value === null || value === '') {
                callback(new Error('备注不能为空'));
              } else {
                callback();
              }
            }
          }
        ],
        num: [
          {
            required: true, message: "人数不能为空", trigger: "blur",
            validator: (rule, value, callback) => {
              if (value === null || value === '') {
                callback(new Error('人数不能为空'));
              } else {
                callback();
              }
            }
          }
        ],
        coverImg: [
          {
            required: true, message: "封面图片不能为空", trigger: "change",
            validator: (rule, value, callback) => {
              // 只在新增时验证,编辑时如果已有图片则不需要上传新图片
              if (!this.form.id && !value && !this.coverFile) {
                callback(new Error('请上传封面图片'));
              } else {
                callback();
              }
            }
          }
        ],
        orginPrice: [
          {
            required: true, message: "原价必须是数字", trigger: "blur",
            validator: (rule, value, callback) => {
              if (value === null || value === '') {
                callback(new Error('原价必须是数字'));
              } else {
                // 检查是否为数字
                const numValue = Number(value);
                if (isNaN(numValue)) {
                  callback(new Error('原价必须是数字'));
                } else if (numValue < 0.01 || numValue > 9999999.99) {
                  callback(new Error('原价必须在0.01到9999999.99之间'));
                } else {
                  callback();
                }
              }
            }
          }
        ],
        tagId: [
          {
            required: true, message: "标签id不能为空", trigger: "change",
            validator: (rule, value, callback) => {
              if (value === null || value === '') {
                callback(new Error('标签id不能为空'));
              } else {
                callback();
              }
            }
          }
        ],
        status: [
          {
            required: true, message: "启用状态不能为空", trigger: "change",
            validator: (rule, value, callback) => {
              if (value === null || value === '') {
                callback(new Error('启用状态不能为空'));
              } else {
                callback();
              }
            }
          }
        ],
        sort: [
          {
            required: true, message: "排序不能为空", trigger: "blur",
            validator: (rule, value, callback) => {
              if (value === null || value === '') {
                callback(new Error('排序不能为空'));
              } else {
                callback();
              }
            }
          }
        ],
      },
      modelShow: false, // 弹窗显隐
      modelTitle: '', // 弹窗title
      // 表格的表头以及内容
      fields: {
        商品ID: "goodsId",
        套餐名字: "comboName",
        人数: "num",
        原价: "orginPrice",
        启用状态: "status",
        排序: "sort",
        操作: "action"
      },
      loading: true, // 表单加载状态
      searchForm: {
        // 搜索框初始化对象
        pageNumber: 1, // 当前页数
        pageSize: 10, // 页面大小
        comboName: '', // 套餐名称
        tagId: '', // 标签ID
        status: '', // 启用状态
      },
      selectDate: null,
      tagList: [], // 标签列表
      columns: [
        {
          type: 'selection',
          width: 60,
          align: 'center'
        },
                {
          title: "商品名称",
          key: "goodsName",
          minWidth: 150,
          tooltip: true,
        },
                {
          title: "商品图片",
          key: "thumbnail",
          width: 120,
          align: 'center',
          render: (h, params) => {
            const thumbnail = params.row.thumbnail;
            if (thumbnail) {
              // 如果是数字类型,拼接endpoint
              let imgUrl = thumbnail;
              if (typeof thumbnail === 'number') {
                imgUrl = this.endpoint + '/' + thumbnail;
              } else if (typeof thumbnail === 'string' && !thumbnail.startsWith('http')) {
                // 如果是字符串但不包含http,可能是ID,拼接endpoint
                imgUrl = this.endpoint + '/' + thumbnail;
              }
              return h('img', {
                attrs: {
                  src: imgUrl,
                  alt: '商品图片',
                  style: 'width: 50px; height: 50px; object-fit: cover; border-radius: 4px'
                },
                style: {
                  width: '50px',
                  height: '50px',
                  objectFit: 'cover',
                  borderRadius: '4px',
                  cursor: 'pointer'
                },
                on: {
                  click: () => {
                    // 点击图片可以预览大图
                    window.open(imgUrl, '_blank');
                  }
                }
              });
            } else {
              return h('span', '暂无图片');
            }
          }
        },
                {
          title: "套餐名字",
          key: "comboName",
          minWidth: 70,
          tooltip: true,
        },
        {
          title: "套餐封面",
          key: "coverImg",
          width: 120,
          align: 'center',
          render: (h, params) => {
            const coverImg = params.row.coverImg;
            if (coverImg) {
              // 如果是数字类型,拼接endpoint
              let imgUrl = coverImg;
              if (typeof coverImg === 'number') {
                imgUrl = this.endpoint + '/' + coverImg;
              } else if (typeof coverImg === 'string' && !coverImg.startsWith('http')) {
                // 如果是字符串但不包含http,可能是ID,拼接endpoint
                imgUrl = this.endpoint + '/' + coverImg;
              }
              return h('img', {
                attrs: {
                  src: imgUrl,
                  alt: '套餐封面',
                  style: 'width: 50px; height: 50px; object-fit: cover; border-radius: 4px'
                },
                style: {
                  width: '50px',
                  height: '50px',
                  objectFit: 'cover',
                  borderRadius: '4px',
                  cursor: 'pointer'
                },
                on: {
                  click: () => {
                    // 点击图片可以预览大图
                    window.open(imgUrl, '_blank');
                  }
                }
              });
            } else {
              return h('span', '暂无图片');
            }
          }
        },
 
        {
          title: "人数",
          key: "num",
          width: 100
        },
        {
          title: "原价",
          key: "orginPrice",
          width: 100
        },
          {
          title: "现价",
          key: "price",
          width: 100
        },
        {
          title: "启用状态",
          key: "status",
          width: 100,
          align: 'center',
          render: (h, params) => {
            const status = params.row.status;
            const color = status === 'ENABLE' ? 'success' : status === 'DISABLE' ? 'default' : 'warning';
            const text = status === 'ENABLE' ? '启用' : status === 'DISABLE' ? '禁用' : '未知';
            return h('Tag', {
              props: {
                color: color
              }
            }, text);
          }
        },
        {
          title: "排序",
          key: "sort",
          width: 100
        },
                {
          title: "备注",
          key: "remark",
          width: 100
        },
        {
          title: "操作",
          key: "action",
          slot: "action",
          align: "center",
          fixed: "right",
          width: 300,
        },
      ],
      data: [], // 表单数据
      total: 0, // 表单数据总数
      selectCount: 0, // 已选数量
      selectList: [], // 已选数据列表
      // 商品选择相关数据
      goodsData: [], // 商品列表
      goodsTotal: 0, // 商品总数
      endpoint: '', // 文件访问端点
      searchGoodsForm: {
        // 商品搜索框初始化对象
        pageNumber: 1, // 当前页数
        pageSize: 10, // 页面大小
        keyword: '',
        searchFromSelfStore: false
      },
      // 图片上传相关
      coverFile: null,
      coverTempUrl: null,
    };
  },
  methods: {
    showSelect(e) {
      this.selectList = e.map(d => d.id);
      this.selectCount = e.length;
    },
    // 排序
    changeSort(e) {
      this.searchForm.sort = e.key;
      this.searchForm.order = e.order;
      if (e.order == "normal") {
        this.searchForm.order = "";
      }
      this.getDataList();
    },
    // 批量删除
    delBatch() {
      if (this.selectCount <= 0) {
        this.$Message.warning("您还未选择要删除的数据");
        return;
      }
      this.$Modal.confirm({
        title: "确认删除",
        content: "您确认要删除所选的 " + this.selectCount + " 条数据?",
        loading: true,
        onOk: () => {
          deleteGoodsBatch(this.selectList).then(res => {
            this.$Modal.remove();
            if (res.code == 200) {
              this.$Message.success("删除成功");
              this.selectList = [];
              this.selectCount = 0;
              this.getDataList();
            }
          });
        }
      });
    },
    // id删除
    delById(row) {
      this.$Modal.confirm({
        title: "确认删除",
        content: "您确认要删除: " + row.comboName + " ?",
        loading: true,
        onOk: () => {
          delKitchenGoods(row.id).then(res => {
            this.$Modal.remove();
            if (res.code == 200) {
              this.$Message.success("删除成功");
              this.getDataList();
            }
          });
        }
      });
    },
    // 打开新增
    openAdd() {
      this.modelTitle = "新增标签"
      this.modelShow = true
      this.form = {
        id: '',
        goodsId: '', // 商品id
        skuId: '', // skuId
        comboName: '', // 套餐名字
        remark: '', // 备注
        num: '', // 人数
        coverImg: '', // 封面图片
        orginPrice: '', // 原价
        tagId: '', // 标签id
        status: '', // 启用状态
        sort: '', // 排序
        goods: null, // 绑定商品
      }
      // 清空图片上传状态
      this.coverFile = null;
      this.coverTempUrl = null;
      this.searchGoodsList(); // 加载商品列表
    },
    // 打开修改
    openEdit(row) {
      this.modelTitle = "修改标签"
      // 初始化表单
      this.form = {
        id: '',
        goodsId: '',
        skuId: '',
        comboName: '',
        remark: '',
        num: '',
        coverImg: '',
        orginPrice: '',
        tagId: '',
        status: '',
        sort: '',
        goods: null,
      };
      // 使用Object.assign合并数据,保留row中的goods信息
      this.form = Object.assign(this.form, row);
      this.modelShow = true
      // 如果有封面图片URL,则设置预览
      if (row.coverImg) {
        this.coverTempUrl = row.coverImg;
      }
      // 如果row中包含商品信息,直接使用
      if (this.form.goodsId && this.form.skuId) {
        // 直接使用row中的商品信息创建goods对象用于显示
        this.form.goods = {
          goodsId: this.form.goodsId,
          goodsSkuId: this.form.skuId,
          goodsName: this.form.goodsName || this.form.comboName || '',
          thumbnail: this.form.thumbnail || this.form.goodsThumbnail || this.form.coverImg || '',
          price: this.form.price || this.form.orginPrice || '',
        };
        // 如果封面图片为空,且商品有缩略图,则使用商品缩略图
        if (!this.form.coverImg && this.form.goods.thumbnail) {
          // 如果thumbnail是数字ID,需要拼接成URL
          if (typeof this.form.goods.thumbnail === 'number') {
            this.form.coverImg = this.endpoint + '/' + this.form.goods.thumbnail;
          } else {
            this.form.coverImg = this.form.goods.thumbnail;
          }
        }
      }
      // 加载商品列表
      this.searchGoodsList();
      console.log(this.form)
    },
    // 新增或修改
    async saveOrUpdate() {
      this.$refs.form.validate(valid => {
        try {
          if (valid) {
            this.submitLoading = true
            // 先上传封面图片,但不阻塞后续操作
            this.uploadFileByLmk().then(() => {
              // 处理商品绑定数据
              let formData = { ...this.form };
              // 如果没有选择商品,则设置为null
              if (!formData.goods) {
                delete formData.goods;
              }
              if (this.form.id) {
                // 修改
                editKitchenGoods(formData).then(res => {
                  if (res.code == 200) {
                    this.$Message.success("修改成功");
                    this.modelClose()
                    this.getDataList()
                  }
                })
              } else {
                // 新增
                addKitchenGoods(formData).then(res => {
                  if (res.code == 200) {
                    this.$Message.success("添加成功");
                    this.modelClose()
                    this.getDataList()
                  }
                })
              }
            }).catch(() => {
              // 即使上传失败,也要继续执行保存逻辑
              let formData = { ...this.form };
              // 如果没有选择商品,则设置为null
              if (!formData.goods) {
                delete formData.goods;
              }
              if (this.form.id) {
                // 修改
                editKitchenGoods(formData).then(res => {
                  if (res.code == 200) {
                    this.$Message.success("修改成功");
                    this.modelClose()
                    this.getDataList()
                  }
                })
              } else {
                // 新增
                addKitchenGoods(formData).then(res => {
                  if (res.code == 200) {
                    this.$Message.success("添加成功");
                    this.modelClose()
                    this.getDataList()
                  }
                })
              }
            });
          }
        } finally {
          this.submitLoading = false
        }
      });
    },
    // 关闭弹窗
    modelClose() {
      this.submitLoading = false
      this.modelShow = false
      // 重置图片上传状态
      this.coverFile = null;
      this.coverTempUrl = null;
      this.form.coverImg = '';
    },
    // 初始化数据
    init() {
      this.getDataList();
    },
    // 分页 改变页码
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getDataList();
    },
    // 分页 改变页数
    changePageSize(v) {
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = v;
      this.getDataList();
    },
    // 搜索
    handleSearch() {
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      this.getDataList();
    },
    // 起止时间从新赋值
    selectDateRange(v) {
      if (v) {
        this.searchForm.startDate = v[0];
        this.searchForm.endDate = v[1];
      }
    },
    // 商品选择相关方法
    chooseGoods(id) {
      const goods = this.goodsData.find(item => {
        return item.id === id;
      })
      if (goods) {
        // 只选择一个商品,直接替换当前商品
        this.form.goods = {
          goodsId: goods.goodsId,
          goodsSkuId: goods.id,
          goodsName: goods.goodsName,
          thumbnail: goods.thumbnail,
          price: goods.price,
        }
        // 自动填充商品ID和SKU ID
        this.form.goodsId = goods.goodsId;
        this.form.skuId = goods.id;
        // 如果封面图片为空,自动使用商品缩略图
        if (!this.form.coverImg) {
          // 如果thumbnail是数字ID,需要拼接成URL
          if (typeof goods.thumbnail === 'number') {
            this.form.coverImg = this.endpoint + '/' + goods.thumbnail;
          } else {
            this.form.coverImg = goods.thumbnail;
          }
        }
      }
    },
    removeGoods() {
      this.form.goods = null;
      this.form.goodsId = '';
      this.form.skuId = '';
    },
    // 获取缩略图URL
    getThumbnailUrl(thumbnail) {
      if (!thumbnail) {
        return '';
      }
      // 如果是数字类型,拼接endpoint
      if (typeof thumbnail === 'number') {
        return this.endpoint + '/' + thumbnail;
      }
      // 如果是字符串且包含http,直接返回
      if (typeof thumbnail === 'string') {
        if (thumbnail.startsWith('http')) {
          return thumbnail;
        } else {
          // 如果是字符串但不包含http,可能是ID,拼接endpoint
          return this.endpoint + '/' + thumbnail;
        }
      }
      return thumbnail;
    },
    searchGoodsList() {
      this.searchGoodsForm.pageNumber = 1;
      return this.getGoodsDataList();
    },
    // 获取商品列表数据
    getGoodsDataList() {
      let search = this.searchGoodsForm;
      if (search.pageNumber > 0) {
        search.pageNumber = search.pageNumber - 1;
      }
      return new Promise((resolve, reject) => {
        videoGoodsEsPage(search).then((res) => {
          if (res.code == 200) {
            this.goodsData = res.data;
            getSts().then(res => {
              this.endpoint = res.data.endpoint
            })
            this.goodsTotal = res.total;
            resolve(res);
          } else {
            reject(res);
          }
        }).catch(error => {
          reject(error);
        });
      });
    },
    // 分页 改变页码
    goodsChangePage(v) {
      this.searchGoodsForm.pageNumber = v;
      this.getGoodsDataList();
    },
    // 分页 改变页数
    goodsChangePageSize(v) {
      this.searchGoodsForm.pageNumber = 1;
      this.searchGoodsForm.pageSize = v;
      this.getGoodsDataList();
    },
    // 上传文件
    async uploadFileByLmk() {
      if (this.coverFile) {
        this.submitLoading = true;
        const formData = new FormData();
        formData.append('file', this.coverFile);
 
        await uploadFileByLmk(formData).then(res => {
          this.submitLoading = false;
          if (res.code === 200) {
            this.form.coverImg = res.data.fileKey;
          } else {
            this.$Message.error(res.msg || '上传失败');
          }
        }).catch(() => {
          this.submitLoading = false;
          this.$Message.error('上传失败');
        });
      }
    },
    // 处理图片上传前
    handleBeforeUpload(file, type) {
      if ("cover" === type) {
        this.coverFile = file;
        this.coverTempUrl = URL.createObjectURL(file);
        return false;
      }
    },
    // 删除图片
    handleRemove(type) {
      if ('cover' === type) {
        this.coverFile = null;
        this.coverTempUrl = null;
        this.form.coverImg = '';
      }
    },
    // 启用厨房商品
    enableGoods(row) {
      this.$Modal.confirm({
        title: "确认启用",
        content: "您确认要启用厨房商品: " + row.comboName + " ?",
        loading: true,
        onOk: () => {
          const params = {
            id: row.id,
            status: 'ENABLE'
          };
          editKitchenGoodsStaus(params.id, params.status).then(res => {
            this.$Modal.remove();
            if (res.code == 200) {
              this.$Message.success("启用成功");
              this.getDataList();
            }
          });
        }
      });
    },
    // 禁用厨房商品
    disableGoods(row) {
      this.$Modal.confirm({
        title: "确认禁用",
        content: "您确认要禁用厨房商品: " + row.comboName + " ?",
        loading: true,
        onOk: () => {
          const params = {
            id: row.id,
            status: 'DISABLE'
          };
          editKitchenGoodsStaus(params.id, params.status).then(res => {
            this.$Modal.remove();
            if (res.code == 200) {
              this.$Message.success("禁用成功");
              this.getDataList();
            }
          });
        }
      });
    },
    // 获取列表数据
    getDataList() {
      this.loading = true;
      getKitchenGoodsPage(this.searchForm).then((res) => {
        console.log(res)
        this.loading = false;
        if (res.code == 200) {
          this.data = res.data;
          this.total = res.total;
        }
      });
      this.total = this.data.length;
      this.loading = false;
    },
    // 获取标签列表
    getTagList() {
      getKitchenTagList().then(res => {
        if (res.code === 200) {
          this.tagList = res.data;
        }
      });
    },
  },
  mounted() {
    this.init();
    // 初始化商品列表
    this.getGoodsDataList();
    // 初始化标签列表
    this.getTagList();
  },
};
</script>
<style lang="scss" scoped>
.export {
  margin: 10px 20px 10px 0;
}
 
.export-excel-wrapper {
  display: inline;
}
 
.order-tab {
  width: 950px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #f0f0f0;
  padding: 0 10px;
  margin-bottom: 10px;
 
  div {
    text-align: center;
    padding: 4px 12px;
    border-radius: 4px;
    cursor: pointer;
  }
 
  .current {
    background-color: #ffffff;
  }
}
 
.upload-file-info {
  display: flex;
  align-items: center;
  margin-top: 10px;
}
 
.upload-file-info img {
  max-width: 200px;
  max-height: 200px;
  margin-right: 10px;
  border-radius: 4px;
}
 
.preview-image-limit{
  max-width: 100%;
  max-height: 200px;
  object-fit: contain;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  display: block;
}</style>