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
/* 备品智能化 add by dzk
   OPD计划备品明细页面功能
   日历-新建事件页面功能重构
*/
 
import { LightningElement ,api, wire, track } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { CurrentPageReference } from 'lightning/navigation';
import initPRE  from '@salesforce/apex/LexOPDPlanProdController.initPRE';
import getEditPermission  from '@salesforce/apex/LexOPDPlanProdController.getEditPermission';
import getProductData  from '@salesforce/apex/LexOPDPlanProdController.getProductData';
import { CloseActionScreenEvent } from 'lightning/actions';
import initOriginal  from '@salesforce/apex/LexOPDPlanProdController.initOriginal';
import getPicklistValues  from '@salesforce/apex/LexOPDPlanProdController.getPicklistValues';
import getNowDateTime  from '@salesforce/apex/lexCreateEventButton.getNowDateTime';
import getEndDateTime  from '@salesforce/apex/lexCreateEventButton.getEndDateTime';
import saveEvent  from '@salesforce/apex/lexCreateEventButton.saveEvent';
import saveLogic  from '@salesforce/apex/lexCreateEventButton.saveLogic';
import saveProductData  from '@salesforce/apex/LexOPDPlanProdController.saveProductData';
import getUserJobCategory  from '@salesforce/apex/lexCreateEventButton.getUserJobCategory';
import getOppQuoteItems  from '@salesforce/apex/lexCreateEventButton.getOppQuoteItems';
import lwcCSS from '@salesforce/resourceUrl/lwcCSS';    
import { NavigationMixin } from 'lightning/navigation';
import { encodeDefaultFieldValues } from 'lightning/pageReferenceUtils';
// 事件补充 事件相关新增方法 start
import initEvent from '@salesforce/apex/lexCreateEventButton.initEvent';
// 事件补充 事件相关新增方法 end
 
export default class lexOPDPlanProd extends NavigationMixin(LightningElement) {
    @api recordId;
    @track SearchModelName = '';
    @track data;
    @track edit;
    @track originalOpd;
    @track oppEventData;
    @track IsOPDPlan = false;
    @track showEventfalg = false;
    @track showEvent = false;
    @track isLoading = false;
    @track showOPDPlan = false;
    @track showproductfalg = false;
    @track dataProduct = [];
    @track selectDeleteProductLine = [];
    @track selectCreateProductLine = [];
    @track visit = '病院';
    @track ActivityPurposeFSE = '';
    @track PurposeTypeFSE = '';
    @track NoOppReason = '';
    @api theIndex;
    @track isStartDis = false;
    @track isEndDis = false;
    @track isVisitDis = false;
    @track isActivityPurposeDis = false;
    @track isSubjectDis = false;
    @track isPurposeTypeDis = false;
    @track isAccDis = true;
    @track isConDis = true;
    @track isOppDis = true;
    @track isProjectDis = true;
    @track isNoOppDis = false;
    @track isOpdPlanDis = false;
    @track isServiceDis = true;
    @track showSpinner = false;
    @track filter = '';
    @track visitorPalceObjName = '';
    @track visitorPalceFields = ['Account.Name'];
    @track opportunityFields = ['Opportunity.Name'];
    @track mcFields = ['Maintenance_Contract__c.Name'];
    @track infrastructureProjectFields = ['Infrastructure_Project__c.Name'];
    @track Related_Opportunity1_ID = '';
 
    @track contactId1 = '';
    @track objectName = 'Contact';
    @track fields=['Contact.Name','Contact.AWS_Data_Id__c'];
    @track StartTime;
    @track EndTime;
    @track Subject;
    @track visible = false;
    @track showServicefalg = false;
    @track IsFSE = false;
    @track columns = [
        {
            label: '计划借出备品',
            fieldName: 'Name',
            type: 'text',
            cellAttributes: {
                alignment: 'left'
            },
            hideDefaultActions: true,
        },
        {
            label: '借出数量',
            fieldName: 'Quantity',
            type: 'number',
            cellAttributes: {
                alignment: 'left'
            },
            hideDefaultActions: true,
        },
        {
            label: '产品代码',
            fieldName: 'ProductCode',
            type: 'text',
            cellAttributes: {
                alignment: 'left'
            },
            hideDefaultActions: true,
        },
        {
            label: '产品型号',
            fieldName: 'ProductModel',
            type: 'text',
            cellAttributes: {
                alignment: 'left'
            },
            hideDefaultActions: true,
            // headerAttributes: { class: 'custom-header-cell' }
        },
   
    ];
 
    @track columnProduct = [
        {label: '计划借出备品',fieldName: 'Name',type: 'text',cellAttributes: {alignment: 'left'},hideDefaultActions: true},
        {label: '借出数量',fieldName: 'Quantity',type: 'number',cellAttributes: {alignment: 'left'},hideDefaultActions: true},
        {label: '产品代码',fieldName: 'ProductCode',type: 'text',cellAttributes: {alignment: 'left'},hideDefaultActions: true},
        {label: '产品型号',fieldName: 'ProductModel',type: 'text',cellAttributes: {alignment: 'left'},hideDefaultActions: true},
    ];
 
    // 事件补充 参数 start
    @api event_in;
    @api taskId;
    @api accid;
    @api cid;
    @api oid;
    @api mid;
    @api source;
    @api isDelay;
    @api isBefore;
    @track eveData;
    // 事件补充 参数 end
    @track allDisable = false;      //20240228 sx  add   询价置灰
    connectedCallback(){
        this.columnProduct = [
            {label: '计划借出备品',fieldName: 'Name',type: 'text',cellAttributes: {alignment: 'left'},hideDefaultActions: true},
            {label: '借出数量',fieldName: 'Quantity',type: 'number',cellAttributes: {alignment: 'left'},hideDefaultActions: true},
            {label: '产品代码',fieldName: 'ProductCode',type: 'text',cellAttributes: {alignment: 'left'},hideDefaultActions: true},
            {label: '产品型号',fieldName: 'ProductModel',type: 'text',cellAttributes: {alignment: 'left'},hideDefaultActions: true},
        ];
    }
 
    // 事件补充 初始化事件 start
    @wire(initEvent, { eventid: '$recordId', taskId: '$taskId', accid: '$accid', cid: '$cid', oid: '$oid', mid: '$mid', source: '$source'})
    wiredEvent({error, data}) {
        console.log('isBefore is ' + this.isBefore);
        console.log('recordid is ' + this.recordId);
        console.log('event data is ' + JSON.stringify(data));
        if (data) {
            // 原始数据记录
            console.log('data: ' + data);
            this.eveData = data.eventData; 
            // 画面字段初始化
            this.StartTime = data.eventData.StartDateTime;
            console.log('data.StartDateTime: ' + this.eveData.StartDateTime);
            this.EndTime = data.eventData.EndDateTime;
            console.log('this.visit before init set:' + this.visit);
            if (data.eventData.Activity_Type2__c) {
                this.visit = data.eventData.Activity_Type2__c;
            }
            console.log('this.visit after init set:' + this.visit);
            if (data.eventData.Activity_PurposeFSE__c) {
                this.ActivityPurposeFSE = data.eventData.Activity_PurposeFSE__c
            } else {
                this.ActivityPurposeFSE = data.eventData.Activity_PurposeEscFSE__c;
            }
            this.filter = data.eventData.whatid__c;
            if (data.eventData.Activity_PurposeFSE__c) {
                this.PurposeTypeFSE = data.eventData.Purpose_TypeFSE__c;
            } else {
                this.PurposeTypeFSE = data.eventData.Purpose_TypeEscFSE__c;
            }
            this.Subject = data.eventData.Subject;
            console.log('this.Subject after init set:' + this.Subject);
            this.contactId1 = data.eventData.Visitor1_ID__c;
            this.Related_Opportunity1_ID = data.eventData.Related_Opportunity1_ID__c;
            console.log('oppid:' + data.eventData.Related_Opportunity1_ID__c);
            console.log('this.Related_Opportunity1_ID after init set:' + this.Related_Opportunity1_ID);
            this.Related_Service1_ID = data.eventData.Related_Service1_ID__c;
            this.NoOppReason = data.eventData.noOpp_Reason__c;
            if (data.eventData.OPDPlan_Flag__c) {
                this.IsOPDPlan = data.eventData.OPDPlan_Flag__c;
            }
            //20240229 sx  事件修复bug start
            if(data.eventData.Opd_Plan__c){
                this.IsOPDPlan = true;
                this.showEventfalg = true;
                this.getOppQuoteItemsExecute();
            }
            //20240229 sx  事件修复bug end
            this.Infrastructure_Project1 = data.eventData.Related_Opportunity4_ID__c;
            // alldisbale增加
            this.allDisable = data.allDisable;   //20240228 sx  add   询价置灰
            if (data.allDisable) {
                console.log('all disable');
                this.isStartDis = true;
                this.isEndDis = true;
                this.isVisitDis = true;
                this.isActivityPurposeDis = true;
                this.isSubjectDis = true;
                this.isPurposeTypeDis = true;
                this.isAccDis = true;
                this.isConDis = true;
                this.isOppDis = true;
                this.isProjectDis = true;
                this.isNoOppDis = true;
                this.isOpdPlanDis = true;
                this.isServiceDis = true;
            }
            if (data.eventData.OPDPlan_Flag__c) {
                console.log('opd flag');
                this.isVisitDis = true;
                this.isAccDis = true;
                this.isOpdPlanDis = true;
            }
            if (data.isDisActivityPurposeFSE) {
                this.isPurposeTypeDis = true;
            }
            // 所有编辑时都不允许修改拜访区分、科室
            if(this.recordId || this.isDelay ==='true' || this.taskId){
                console.log('is modify');
                this.isVisitDis = true;
                this.isAccDis = true;
                this.isOppDis = true;
            }
            if (this.mid !== undefined) {
                this.isServiceDis = true;
            }
 
 
            if(this.Related_Opportunity1_ID) {
                this.isNoOppDis = true;
            }
 
            getOppQuoteItems({ records: '',recordId: this.Related_Opportunity1_ID }).then(result => {
                // console.log('getOppQuoteItems处理结果:', result.length);
                if(this.Related_Opportunity1_ID && this.Related_Opportunity1_ID !== ''){ //zzm 20240426 不关联询价时不能选择OPD计划bug修复
                    if(!result || result.length === 0 ){
                        this.isOpdPlanDis = true;
                        this.IsOPDPlan = false;
                        this.showEventfalg = false;
                    }else{
                        //20240228 sx  opd是否能勾选start
                        if(this.recordId){
                            this.isOpdPlanDis = true;
                        }else{
                            this.isOpdPlanDis = false;
                        }
                        //20240228 sx  opd是否能勾选end
                    }
                }else{
                    if(this.visit === '病院' 
                    && this.isDelay!='true' && this.isDelay!=true && this.isBefore!='true' && this.isBefore!=true && this.allDisable!=true){ //zzm 20240228 延期和提前不能更改OPD计划
                        this.isOpdPlanDis = false;
                    } else {
                        this.isOpdPlanDis = true;                    //zzm 20240228 延期和提前不能更改OPD计划
                    }
                }
            }).catch(error => {
                // 处理错误
                console.error('getOppQuoteItems发生错误:', error);
        
            });
        } else if (error) {
            console.log('initEvent错误------' + JSON.stringify(error));
        }
    }
    // 事件补充 初始化事件 end
 
    //页面计划出借备品数据
    @wire(initPRE, { recordId: '$recordId' })
    wiredPREItems({error,data}) {
        this.data = data;
        console.log('event_in:' + this.event_in);
        if(this.recordId !== undefined){
            console.log('this.recordId-----------' + this.recordId);
            if(this.event_in == 'event'){
                this.showEvent = true;
                if (data !== undefined) {
                    if (data.length > 0) {
                        this.IsOPDPlan = true;
                        this.showEventfalg = true;
                        this.dataEvent = data;
                    }
                }
            }else{
                this.showOPDPlan = true; 
            }
 
            this.columns = [
                {
                    label: '计划借出备品',
                    fieldName: 'Name',
                    type: 'text',
                    cellAttributes: {
                        alignment: 'left'
                    },
                    hideDefaultActions: true,
                },
                {
                    label: '借出数量',
                    fieldName: 'Quantity',
                    type: 'number',
                    cellAttributes: {
                        alignment: 'left'
                    },
                    hideDefaultActions: true,
                },
                {
                    label: '报价编码',
                    fieldName: 'QuoteNo',
                    type: 'text',
                    cellAttributes: {
                        alignment: 'left'
                    },
                    hideDefaultActions: true,
                },
                {
                    label: '产品代码',
                    fieldName: 'ProductCode',
                    type: 'text',
                    cellAttributes: {
                        alignment: 'left'
                    },
                    hideDefaultActions: true,
                },
                {
                    label: '产品型号',
                    fieldName: 'ProductModel',
                    type: 'text',
                    cellAttributes: {
                        alignment: 'left'
                    },
                    hideDefaultActions: true,
 
                },
            ];
            this.columnProduct = [
                {label: '计划借出备品',fieldName: 'Name',type: 'text',cellAttributes: {alignment: 'left'},hideDefaultActions: true},
                {label: '借出数量',fieldName: 'Quantity',type: 'number',cellAttributes: {alignment: 'left'},hideDefaultActions: true},
                {label: '产品代码',fieldName: 'ProductCode',type: 'text',cellAttributes: {alignment: 'left'},hideDefaultActions: true},
                {label: '产品型号',fieldName: 'ProductModel',type: 'text',cellAttributes: {alignment: 'left'},hideDefaultActions: true},
            ];
        }
    }
 
    //当前用户是否拥有编辑OPD计划权限
    @wire(getEditPermission, { recordId: '$recordId' })
    wiredEditPermission({error,data}) {
        if (data) {
            this.edit = data;
        } else if (error) {
            console.log('getEditPermission错误------' + JSON.stringify(error));
        }
    }
 
    @wire(initOriginal, { recordId: '$recordId' })
    wiredOriginalItems({error,data}) {
        if (data) {
            this.OriginalOpd = data.OriginalOpdPlanApplication__c;
            
        } else if (error) {
            console.log('init------' + JSON.stringify(error));
        }
    }
    //产品选择复选框勾选
    handleRowSelect(event) {
        const selectedRows = event.detail.selectedRows;
        this.selectCreateProductLine = selectedRows;
        // 在这里处理选中的行数据
    }
 
    //计划备品复选框勾选
    handleRowData(event) {
        const selectedRows = event.detail.selectedRows;
        this.selectDeleteProductLine = selectedRows;
    }
 
 
    //展开数据查询页
    //显示符合条件的奥林巴斯产品
    SelectProducts(event){
        this.showproductfalg = true;
        // let trimmedString = this.SearchModelName.trim();
        console.log('recordId-----------' + JSON.stringify(this.data));
        //zzm 20240226 事件产品bug修复
        getProductData({ recordId: this.recordId,event_in:this.event_in, records: JSON.stringify(this.data), productmodel: this.SearchModelName }).then(result => {
            // 处理 Apex 方法的返回结果
            this.dataProduct = result;
        }).catch(error => {
           // 处理错误
            console.error('发生错误:', error);
        });
    }
 
 
    getOppQuoteItemsExecute(){
        getOppQuoteItems({ records: JSON.stringify(this.dataEvent),recordId: this.Related_Opportunity1_ID }).then(result => {
            console.log('getOppQuoteItems处理结果:', result.length);
            if(this.isNoOppDis === true && this.Related_Opportunity1_ID !== '' && this.Related_Opportunity1_ID){ //zzm 20240228 延期和提前不能更改OPD计划
                if(result.length === 0 ){
                    this.isOpdPlanDis = true;
                    this.IsOPDPlan = false;
                    this.showEventfalg = false;
                    // this.Alert('询价下不存在报价或符合借出条件的报价行项目为空,不允许勾选OPD计划。','warning');
                    alert('询价下不存在报价或符合借出条件的报价行项目为空,不允许勾选OPD计划。');
                }else{
                    //20240228 sx  opd是否能勾选start
                    if(this.recordId){
                        this.isOpdPlanDis = true;
                    }else{
                        this.isOpdPlanDis = false;
                    }
                    //20240228 sx  opd是否能勾选end
                    this.dataEvent = result;
                    this.data = result;
                    this.oppEventData = result;
                }
            }else{
                if(this.visit === '病院'
                && this.isDelay!='true' && this.isDelay!=true && this.isBefore!='true' && this.isBefore!=true && this.allDisable!=true){ //zzm 20240228 延期和提前不能更改OPD计划
                    this.isOpdPlanDis = false;
                } else {
                    this.isOpdPlanDis = true;                //zzm 20240228 延期和提前不能更改OPD计划
                }
                this.oppEventData = result;
                this.dataEvent = result;
                this.data = result;
            }
        }).catch(error => {
            // 处理错误
            console.error('getOppQuoteItems发生错误:', error);
    
        });
    }
    
    //删除备品数据
    DeletetProduct(event) {
        if(this.selectDeleteProductLine.length == 0) {
            alert('请选择可以操作的行');
            // this.Alert('请选择可以操作的行','error');
        } else{    
            if(this.showEvent == false){
                for (var i = 0; i < this.selectDeleteProductLine.length; i++) {
                    var selectQuoteNo = this.selectDeleteProductLine[i].QuoteNo;
                    console.log('this.OriginalOpd-----------' + this.OriginalOpd);
                    if(selectQuoteNo != null && (this.OriginalOpd === null || this.OriginalOpd === '' || this.OriginalOpd === undefined)) {
                        // this.Alert('选中的备品已经绑定报价,不允许删除。','error');
                        alert('选中的备品已经绑定报价,不允许删除。');
                        // this.Alert('选中的备品已经绑定报价,不允许删除。');
                        return;
                    }
                }       
                const dataDelete = [];
              
                this.data.forEach(item => {
                    dataDelete.push(item);
                }); 
                this.selectDeleteProductLine.forEach(item => {
                    const index = dataDelete.findIndex(data => data.ProductCode === item.ProductCode);
                    if(index !== -1){
                        dataDelete.splice(index,1);
                        this.data = dataDelete
                        this.selectDeleteProductLine = [];
                    }
                });
            }
            else{
                
                for(var de = 0; de < this.selectDeleteProductLine.length; de++) {
                   for(var event = 0; event < this.dataEvent.length; event++) {
                        if(this.selectDeleteProductLine[de].ProductCode == this.dataEvent[event].ProductCode){
                            var itemsToKeep = this.dataEvent.filter(item => !this.selectDeleteProductLine.some(selectedItem => selectedItem.ProductCode === item.ProductCode));
                        }
                    } 
                }
                var quoteNum = 0;
                itemsToKeep.forEach((row,index) => {
                    if(row.QuoteNo !== null && row.QuoteNo !== '' && row.QuoteNo !== undefined){
                        quoteNum++;
                    }
                });
                if(quoteNum === 0 && (this.Related_Opportunity1_ID !== '' && this.Related_Opportunity1_ID !== null && this.Related_Opportunity1_ID !== undefined)) {
                    alert('有询价时,至少存在一个报价产品。');
                    // this.Alert('有询价时,至少存在一个报价产品。','error');
                    return;
                }else{
                    this.dataEvent = itemsToKeep;
                    this.data = itemsToKeep;
                }
                this.selectDeleteProductLine = [];
            }  
        }
    }
 
    //关闭产品查询页
    //清空特定查询条件数据
    CancelProductPage() {
        if(this.event_in !== 'event'){
            this.showOPDPlan = true; 
            this.showproductfalg = false;
        }else{
            this.showEventfalg = true; 
            this.showproductfalg = false;
        }
        this.dataProduct = null;
        this.SearchModelName = null;
    }
 
    //产品型号输入框获取输入值
    ProductModelChange(event){
        if(this.SearchModelName != event.target.value){
            this.SearchModelName = event.target.value;
        }
    }
 
     @track dataEvent = [];
    
     //追加备品数据
    AddProduct() {
        const dataAdd = [];
        var newEventDataArrayTmp = [...this.dataEvent];
        if(this.selectCreateProductLine.length == 0){
            // this.Alert('请选择可以操作的行','error');
            alert('请选择可以操作的行');
            return;
        }
        this.data.forEach(item => {
            dataAdd.push(item);
        }); 
        console.log('dataAdd:' + dataAdd.length);
        console.log('showEvent:' + this.showEvent);
        console.log('selectCreateProductLine:' + this.selectCreateProductLine.length);
        this.selectCreateProductLine.forEach(item => {
            console.log('item:' + item);
            if(this.showEvent == true){
                newEventDataArrayTmp.push(item);
                // this.dataEvent.push(item);
                console.log('this.oppEventData-----------' + JSON.stringify(this.oppEventData));
                if(this.oppEventData === null || this.oppEventData === '' || this.oppEventData === undefined){
                    
                }else{
                    // console.log('this.oppEventData-----------' + JSON.stringify(this.oppEventData));
                    for (let i = 0; i < this.dataEvent.length; i++) {
                        let productCode1 = this.dataEvent[i].ProductCode;
                        let matchingItem = this.oppEventData.find(item => item.ProductCode === productCode1);
                        if (matchingItem) {
                            this.dataEvent[i].QuoteNo = matchingItem.QuoteNo;
                            this.dataEvent[i].QuoteId = matchingItem.QuoteId;
                        }
}
                }
                
                const newEventDataArray = [...this.dataEvent];
                this.dataEvent = newEventDataArray;
                this.dataEvent = newEventDataArrayTmp;
                this.data = this.dataEvent; 
            }else{
                dataAdd.push(item);
                this.data = dataAdd;
            }
            const index = this.dataProduct.findIndex(data => data.ProductCode === item.ProductCode);
            if(index !== -1){
                this.dataProduct.splice(index,1);
                const newDataArray = [...this.dataProduct];
                this.dataProduct = newDataArray;
             }
              // this.Alert('追加成功','success');
 
           
        });
        alert('追加成功');
    }
 
    @track dataChecked = [];
    //查询符合条件的产品数据
    SelectProductItem(event){
        
        const modifiedData  = [];
        for (const change of this.selectCreateProductLine) {
             modifiedData.push({ Name: change.Name, 
                                Quantity: change.Quantity, 
                                ProductCode: change.ProductCode, 
                                FixtureModel: change.FixtureModel,
                                ProductModel: change.ProductModel,
                                _selected: true
                               });
        }
        //zzm 20240226 事件产品bug修复
        getProductData({ recordId: this.recordId,event_in:this.event_in, records: JSON.stringify(this.data), productmodel: this.SearchModelName }).then(result => {
            // 处理 Apex 方法的返回结果
            
            result.forEach(item => {
                const index = this.selectCreateProductLine.findIndex(data => data.ProductCode === item.ProductCode);
                if(index === -1){
                    modifiedData.push(item);
                }
            });
 
            this.dataProduct = modifiedData;
            this.applySelectedRows();
        }).catch(error => {
           // 处理错误
            console.error('getProductData发生错误:', error);
        });
    }
 
    applySelectedRows() {
    // 在新数据中标记已选中的行
        this.dataProduct = this.dataProduct.map(row => {
            if (this.selectCreateProductLine.some(selectedRow => selectedRow.ProductCode === row.ProductCode)) {
                row._selected = true;
            }
            return row;
        });
    }
 
    SavetProduct(event){
        saveProductData({ records: JSON.stringify(this.data) ,recordId: this.recordId}).then(result => {
            // 处理 Apex 方法的返回结果
            // this.Alert('保存成功','success');
            alert('保存成功');
            location.reload();
        }).catch(error => {
           // 处理错误
            console.error('saveProductData发生错误:', error);
        });
    }
    connectedCallback(){
        getNowDateTime().then(res=>{
            if (res!=null) {
                console.log('starttime:' + res);
                if (this.recordId) {
                } else {
                    this.StartTime = res;
                }
            }
        });
        getEndDateTime().then(res=>{
            if (res!=null) {
                console.log('endtime:' + res);
                if (this.recordId) {
                } else {
                    this.EndTime = res;
                }
            }
        });
        getUserJobCategory().then(res=>{
            if (res!=null) {
                if(res === '销售推广' && !this.allDisable && !this.isBefore && !this.isDelay){
                    this.isOppDis = false;
                }
                if(res === '销售服务'){
                    this.showServicefalg = true;
                    this.IsFSE = true;
                }
            }
        });
        this.visit = '病院';
        this.visitorPalceObjName = 'SearchVisitorPlace';
        this.isAccDis = false;
        this.isConDis = false;
        this.isOppDis = true;
        //2024 add by dzk 编辑时,延期不可修改询价
        if(this.isDelay === "true"){
            this.isOppDis = true;
        }
        
    }
    //询价
    handleOppertunity1Selection(event) {
        this.Related_Opportunity1_ID = event.detail.selectedId;
        if(this.Related_Opportunity1_ID !== '' && this.Related_Opportunity1_ID !== undefined && this.Related_Opportunity1_ID !== null){
            this.isNoOppDis = true;
            this.NoOppReason = '';
            this.getOppQuoteItemsExecute();
        }else{
            this.isNoOppDis = false;
            this.getOppQuoteItemsExecute();
        }
        this.checkVisitChange();
    }
 
    //访问对象
    handleAccSelection(event) {
        this.filter = event.detail.selectedId;
        if(this.filter !== null && this.filter !== '' && this.filter !== undefined){
            this.isOppDis = false;
        }else{
            this.isOppDis = true;
            this.Related_Opportunity1_ID = '';
        }
        this.changeAccId();
        this.checkVisitChange();
    }
 
    changeAccId(){
        //清空访问对象
        const childComponents = this.template.querySelectorAll('c-look-up-util-event');
        console.log(childComponents.length);
        if (childComponents.length > 0) {
            childComponents.forEach(childComponent => {
                childComponent.handleRemovePill();
            });
        }
        //清空其他查询组件
        const otherComponents = this.template.querySelectorAll('c-other-look-up-event');
        console.log(otherComponents.length);
        if (otherComponents.length > 0) {
            otherComponents.forEach(otherComponent => {
                console.log('清空其他查询组件:')
                console.log(otherComponent['uniqueKey'])
                if(otherComponent['uniqueKey'] !='AccountID') {
                    otherComponent.handleRemovePill();
                    
                } 
            });
        }
        this.visitDis = false;
        
    }
    //拜访区分列表值
    @track optionsActivityType2 = [];
    @wire(getPicklistValues, { objstr:'Event',fld:'Activity_Type2__c' })
    wiredType2PicklistValues({error,data}) {
        this.optionsActivityType2 = data;
    }
 
    //拜访目的(FSE外)列表值
    @track optionsActivityPurposeFSE = [];
    @wire(getPicklistValues, { objstr:'Event',fld:'Activity_PurposeEscFSE__c' })
    wiredEscFSEPicklistValues({error,data}) {
        this.optionsActivityPurposeFSE = data;
    }
 
    //活动区分FSE外列表值
    @track optionsActivityPurposeEscFSE = [];
    @wire(getPicklistValues, { objstr:'Event',fld:'Purpose_TypeEscFSE__c' })
    wiredTypeEscFSEPicklistValues({error,data}) {
        this.optionsActivityPurposeEscFSE = data;
    }
 
    //无询价申请理由列表值
    @track optionsNoOppReason = [];
    @wire(getPicklistValues, { objstr:'Event',fld:'noOpp_Reason__c' })
    wiredReasonPicklistValues({error,data}) {
        this.optionsNoOppReason = data;
    }
 
    handleStartTimeChange(event){
        this.StartTime = event.detail.value;
    } 
 
    handleEndTimeChange(event){
        this.EndTime = event.detail.value;
    } 
    handleSubjectChange(event){
        this.Subject = event.detail.value;
    }  
 
    handleActivityType2Change(event){
 
        this.isAccDis = true;
        this.isConDis = true;
        this.isOppDis = true;
        this.isProjectDis = true;
        this.isOpdPlanDis = true;
        this.isServiceDis = true;
        this.visit = event.detail.value;
        this.PurposeTypeFSE = '';
        this.ActivityPurposeFSE = '';
        this.NoOppReason = '';
        this.IsOPDPlan = false;
        this.filter = '';
        this.contactId1 = '';
        this.Related_Opportunity1_ID = '';
        this.Infrastructure_Project1 = '';
        this.Related_Service1_ID = '';
        this.showEventfalg = false;
        this.dataEvent = [];
        this.data = [];
        const otherComponents = this.template.querySelectorAll('c-other-look-up-event');
            if (otherComponents.length > 0) {
                otherComponents.forEach(otherComponent => {
                    console.log('清空访问场所组件:')
                    console.log(otherComponent['uniqueKey'])
                    if(otherComponent['uniqueKey'] =='AccountID') {
                        otherComponent.handleRemovePill();
                    } 
                });
            }
        if(this.visit === '病院'){
            this.visitorPalceObjName = 'SearchVisitorPlace';
            this.isAccDis = false;
            this.isConDis = false;
            // this.isOppDis = true;
            this.isNoOppDis = false;
            this.isOpdPlanDis = false;
            this.isServiceDis = false;
        }else if(this.visit === '販売店'){
            this.visitorPalceObjName = 'SearchVisitorPlace_Sales';
            this.isAccDis = false;
            // this.isOppDis = true;
            this.isNoOppDis = false;
        }else if(this.visit === '社内活動'){
            this.visitorPalceObjName = 'SearchVisitorPlace_Training';
            this.isAccDis = false;
            this.isNoOppDis = true;
        }else if(this.visit === '社外イベント'){
            this.visitorPalceObjName = 'SearchVisitorPlace_Campaign';
            this.visitorPalceFields = ['Campaign.Name'];
            this.isAccDis = false;
            this.isNoOppDis = true;
        }else {
            this.isAccDis = true;
            this.isNoOppDis = true;
        }
        this.filter = '';
    }
 
    handleActivityPurposeFSEChange(event){
        this.ActivityPurposeFSE = event.detail.value;
    } 
 
    handleActivityPurposeEscFSEChange(event){
        this.PurposeTypeFSE = event.detail.value;
 
        // if(this.visit === '基建项目'){
        if(this.PurposeTypeFSE === '基建项目'){ //zzm 240115 事件基建项目bug修复
            this.isProjectDis = false;
        }
    }
 
    handleNoOppReasonChange(event){
        this.NoOppReason = event.detail.value;
    }
 
    handleIsOPDPlanChange(event){
        this.IsOPDPlan = event.detail.checked;
        //zzm 20240131 勾选OPD计划时,提醒无询价的提示信息 start
        if(this.IsOPDPlan==true && !this.Related_Opportunity1_ID) {
           // this.Alert('您创建的OPD计划没有关联询价,需要审批到副总裁','error');
           alert('您创建的OPD计划没有关联询价,需要审批到副总裁');
        }
        //zzm 20240131 勾选OPD计划时,提醒无询价的提示信息 end
        this.showEventfalg = event.detail.checked;
 
        this.getOppQuoteItemsExecute();
 
    }
    
    handleNewRecord(event){
        const openPageEvent = new CustomEvent('openpage', {
            detail: { activeContentIndex:this.theIndex,lookUpKey:event.detail.key },
        });
        this.dispatchEvent(openPageEvent);
    }
 
    //访问对象
    handleSelection(event) {
        var key = event.detail.key;
        if(key==='AgencyContact1'){
            this.requiredVisitor = '';
            this.contactId1 = event.detail.selectedId;
        }
 
        this.checkVisitChange();
    }
 
 
 
    @track Infrastructure_Project1;
    //基建项目
    handleInfrastructureProject1Selection(event) {
        this.Infrastructure_Project1 = event.detail.selectedId;
        this.checkVisitChange();
    }
 
    //维修合同
    @track Related_Service1_ID;
    handleMCSelection(event) {
        this.Related_Service1_ID = event.detail.selectedId;
        this.checkVisitChange();
       
    }
 
    //设置拜访区分是否可点击
    checkVisitChange() {
        console.log('checkVisitChange start');
        var changeFlg = false;
        if(this.contactId1){
            changeFlg = true;
        }
        console.log('check visit change:' + changeFlg);
    
        this.isVisitDis = changeFlg;
    }
 
    SaveAndEventData(){
        const CreateAndSave = true;
        this.SaveOrCreateEventData(CreateAndSave);
    }
    SaveEventData() {
        const CreateAndSave = false;
        this.SaveOrCreateEventData(CreateAndSave);
    }   
    //追加备品数据
    SaveOrCreateEventData(CreateAndSave) {
        let recordId;
        console.log('this.visit--------' + this.Subject);
        let screenData = {
            "EventId" : this.recordId,
            "TaskId" : this.taskId,
            "StartTime" : this.StartTime,
            "EndTime" : this.EndTime,
            "ActivityType2" : this.visit,
            "PurposeTypeFSE" : this.PurposeTypeFSE,//活动区分
            "ActivityPurposeFSE" : this.ActivityPurposeFSE,//拜访目的
            "NoOppReason" : this.NoOppReason,
            "IsOPDPlan" : this.IsOPDPlan,
            "AccountId" : this.filter,
            "ContactId" : this.contactId1,
            "OpportunityId" : this.Related_Opportunity1_ID,
            "ProjectId" : this.Infrastructure_Project1, 
            "ServiceId" : this.Related_Service1_ID,
            "isDelay" : this.isDelay,
            "isBefore" : this.isBefore, 
            "Subject" : this.Subject
        };
        console.log('ActivityType2 :' + this.visit);
        console.log('ActivityPurposeFSE :' + this.ActivityPurposeFSE);
        console.log('====zzzm=====this.isBefore :' + this.isBefore);
        this.showSpinner = true;
        saveLogic({ records: JSON.stringify(screenData),products: JSON.stringify(this.dataEvent) }).then(result => {
            console.log('result:' + result);
            if(result === 'Success'){
                saveEvent({ records: JSON.stringify(screenData),products: JSON.stringify(this.dataEvent) }).then(result => {
                    if(result.indexOf('ERROR') >= 0){
                        // this.Alert(result,'error');
                        alert(result);
                        this.showSpinner = false;
                        return;
                    }else{
                        // this.Alert('保存成功','success');
                        alert('保存成功');
                        if(CreateAndSave === true){
                            window.open("/lightning/o/Event/new",'_self');
                        }else{
                            // DB202312502818 zzm 20240110 start
                            // window.open("/lightning/r/Event/"+result+"/view",'_self');
                            window.open("/lightning/o/Event/home",'_self');
                            // DB202312502818 zzm 20240110 end
                        }   
                    }
                    // 处理 Apex 方法的返回结果
                }).catch(error => {
                   // 处理错误
                    console.error('saveEvent发生错误:', error);
                    // this.Alert(result,'error');
                    alert(result);
                    this.showSpinner = false;
                    return;
                });
            }else{
                // this.Alert(result,'error');
                alert(result);
                this.showSpinner = false;
                return;
            }
            
         // 处理 Apex 方法的返回结果
        }).catch(error => {
           // 处理错误
            console.error('saveLogic发生错误:', error);
        });
 
    }
 
    CancelEventData(){
        this.showSpinner = true;
        window.open("/lightning/o/Event/home",'_self');
        // this.showSpinner = false;
    }
 
    Alert(msg,type) {
        const customEvent = new CustomEvent('customEventName', {
            detail: {
                message: msg,
                type: type,
            },
        });
        window.dispatchEvent(customEvent);
    }
}