liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
import { api, LightningElement, track, wire } from 'lwc';
import init from '@salesforce/apex/LwcDailyReportController.init';
import saveAllData from '@salesforce/apex/LwcDailyReportController.saveAllData';
import getDailyReport from '@salesforce/apex/LwcDailyReportController.getDailyReport';
import cancleApply from '@salesforce/apex/LwcDailyReportController.cancleApply';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { label } from 'c/labelUtility';
import { NavigationMixin } from 'lightning/navigation';
import lwcCSS from '@salesforce/resourceUrl/lwcCSS';
import {loadStyle} from 'lightning/platformResourceLoader';
// import { RefreshEvent } from 'lightning/refresh';
import { updateRecord } from 'lightning/uiRecordApi';
 
 
 
 
 
export default class LwcDailyReport extends NavigationMixin(LightningElement) {
    @track customLabel = label;
    @api recordId;
    reporter = '用户';
    @track report={};
    @track reportOwner={};
    @track profileName;
    @track batchUser;
    @track ativities=[];
    @track reportStatusFlg;
    @track reportReportFlg;
    @track statusMes;
    @track timeValid;
    @track currentUserId;
    @track allDisableFlg=true;
    @track cancelRequestFlg;
    @track beforeHour;
    @track beforeMinute;
    @track afterHour;
    @track afterMinute;
    @track reportedDate;
    @track isSalesService;
    @track showModal = true;
    @track showSpinner = false;
    @track reportOwnerUrl;
    //点击新建按钮的活动内容组件的下标
    @track contentIndex;
    //点击新建按钮的活动内容组件下面的lookUp组件的key
    @track lookUpKey;
    //点击新建按钮后成功创建返回的id
    @track conId;
    @track cancleApplyDis =true;
    //修改日报状态
    @track isChangeStatus = false;
    @track readReportFlag = false;
    @track Job_Category ;
    @track newLeadRecordType ;
    @track Submit_DateTime_New;
    @track Approved_DateTime;
    businessTrip = 'No';
    @track saveAndCloseDis=false;
    reportDateRead ;
    get businessTripOptions() {
        return [
            { label: 'Yes', value: 'Yes' },
            { label: 'No', value: 'No' }
        ];
    }
    connectedCallback(){
        Promise.all([
            loadStyle(this, lwcCSS)
        ]);
        console.log('父组件connectedCallback');
        this.dispatchEvent(new CustomEvent('recordChange'));
        this.initPage();
        
    }
    dateFormatUtils(dateTmp) {
        var year = dateTmp.getFullYear();
        var month =dateTmp.getMonth()+1;
        if(month < 10 ) {
            month = "0" +month;
        }
        var days =dateTmp.getDate();
        if(days < 10 ) {
            days = "0" +days;
        }
        var hours =dateTmp.getHours();
        if(hours < 10 ) {
            hours = "0" +hours;
        }
        var minutes =dateTmp.getMinutes();
        if(minutes < 10 ) {
            minutes = "0" +minutes;
        }
        var dateFormat = year +'/' +month +'/' +days + ' ' + hours +":" +minutes;
 
        console.log("dateTmp:" +dateFormat);
        return dateFormat;
    }
    initPage() {
        // this.dispatchEvent(new RefreshEvent());
        const inputPaddingRight = document.createElement('style');
         inputPaddingRight.innerText = ".input-padding-right  input { padding-right: 8px !important ;adding-left: 8px !important ;}";
         document.body.appendChild(inputPaddingRight);
 
        init({ recordId: this.recordId})
        .then(result => {
            console.log('init ');
            console.log(JSON.stringify(result));
            this.report =  JSON.parse(JSON.stringify(result)).report;
            this.currentUserId =  JSON.parse(JSON.stringify(result)).currentUserId;
            this.reportedDate =  this.report.Reported_Date__c;
            this.reportOwner =  JSON.parse(JSON.stringify(result)).reportOwner;
            this.profileName = JSON.parse(JSON.stringify(result)).profileName;
            console.log('reportOwner = ' + JSON.stringify(this.reportOwner));
            this.batchUser = JSON.parse(JSON.stringify(result)).batchUser;
            this.reportOwnerUrl = '/lightning/r/User/'+this.reportOwner.Id+'/view';
            this.reportStatusFlg = JSON.parse(JSON.stringify(result)).reportStatusFlg;
            // this.reportStatusFlg = true;
            this.reportReportFlg = JSON.parse(JSON.stringify(result)).reportReportFlg;
            this.Job_Category = JSON.parse(JSON.stringify(result)).Job_Category;
            this.newLeadRecordType = JSON.parse(JSON.stringify(result)).newLeadRecordType;
            
            // this.reportReportFlg = true;
            if(this.reportOwner.Job_Category__c==='销售服务'){
                this.isNotSalesService =  false;
            }else{
                this.isNotSalesService =  true;
            }
            if(this.report.Submit_DateTime_New__c) {
                this.Submit_DateTime_New = this.dateFormatUtils(new Date(this.report.Submit_DateTime_New__c));
            }
            if(this.report.Approved_DateTime__c) {
                this.Approved_DateTime = this.dateFormatUtils(new Date(this.report.Approved_DateTime__c));
            }
            console.log('--------------'+this.isNotSalesService);
            this.ativities =  JSON.parse(JSON.stringify(result)).returnEvents;
            this.beforeHour = JSON.parse(JSON.stringify(result)).beforeHour;
            this.beforeMinute = JSON.parse(JSON.stringify(result)).beforeMinute;
            this.afterHour = JSON.parse(JSON.stringify(result)).afterHour;
            this.afterMinute = JSON.parse(JSON.stringify(result)).afterMinute;
            if(this.report.Status__c == '申請中') {
                this.cancleApplyDis = false;
            }
            this.setAllDisableFlg();
            this.reportDateRead = this.report.Reported_Date__c;
        })
        .catch(error => {
            console.error('Error loading report: ', error);
        });
    }
    renderedCallback(){
        console.log('父组件renderedCallback');
        this.setAllDisableFlg();
        
    }
    setAllDisableFlg(){
        var today = new Date();
        var reportDate = new Date(this.report.Reported_Date__c);
        reportDate.setHours(0);
        var reportDateRead = new Date(this.reportDateRead);
        reportDateRead.setHours(0);
        console.log('setAllDisableFlg:' +reportDateRead);
        console.log('setAllDisableFlg:'+today);
        if(this.report.Status__c === '承認' || (this.report.Status__c === '申請中' && this.currentUserId !== this.reportOwner.ManagerId)){
            console.log('1');
            if(!this.isChangeStatus) {
                this.allDisableFlg = true;
            }
        }else if (reportDateRead > today&&this.report.Daily_Report_Data_Type__c!='一日休假'&&this.report.Daily_Report_Data_Type__c!='全天培训') {
        // }else if (this.report.Daily_Report_Data_Type__c!='一日休假'&&this.report.Daily_Report_Data_Type__c!='全天培训') {
            console.log('2');
            this.allDisableFlg = true;
        // }else if (this.stringIsEmpty(this.report.Id)) {
        //     console.log('3');
        //     console.log(JSON.stringify(this.report));
        //     this.allDisableFlg = true;
        }else if(!this.recordId){
             this.allDisableFlg = true;
        } else {
            console.log('allDisableFlg 4');
            this.allDisableFlg = false;
        }
        console.log('this.allDisableFlg:'+this.allDisableFlg);
        if(this.allDisableFlg){
            this.setNotVisible();
        }else{
            console.log('setAllDisableFlg setVisible start');
            this.setVisible();
        }
 
        this.showModal = false;
    }
    setVisible(){
        console.log('setVisible');
        const allInputsAndButtons = this.template.querySelectorAll('lightning-input, lightning-button,lightning-input-field');
        allInputsAndButtons.forEach(element => {
            if(!element.id.includes('cancleApply')) {
                element.disabled = false;
            } 
        });
        
        //处理子组件的显示
 
        const childComponents = this.template.querySelectorAll('c-active-content');
        if (childComponents.length > 0) {
            childComponents.forEach(childComponent => {
                childComponent.setVisible();
                childComponent.setVisitorPlaceDisabled();
            });
        }
        if(this.report.Status__c == '申請中' && !this.isChangeStatus) {
            this.cancleApplyDis = false;
        } else {
            this.cancleApplyDis = true;
        }
        
        console.log('----------zzzzz-----'+ this.report.Reported_Date__c);
        console.log('结束');
    }
    setNotVisible(){
        console.log('setNotVisible');
        const allInputsAndButtons = this.template.querySelectorAll('lightning-input, lightning-button,lightning-input-field');
        allInputsAndButtons.forEach(element => {
            if(element.id.includes('reportDateId')){
            }else if(element.id.includes('readDailyReportId')){
            }else if(element.id.includes('DailyReportDataType')){
            }else if(element.id.includes('cancleBtn')){ //返回首页按钮一直可点击 zzm 230805
            }else if(element.id.includes('cancleApply')) { //取消申请按钮单独处理
            }else{
                element.disabled = true;
            }
        });
        //处理子组件的显示
        console.log('this.allDisableFlg:'+this.allDisableFlg);
        const childComponents = this.template.querySelectorAll('c-active-content');
        console.log('childComponents.lenth:' +childComponents.length);
        if (childComponents.length > 0) {
            childComponents.forEach(childComponent => {
                childComponent.setNotVisible();
            });
        }
        console.log('setNotVisible 结束');
    }
    //读取日报
    readDailyReport(){
        console.log('readDailyReport');
        this.showSpinner = true;
        if (this.report.Reporter__c) {
        }else{
            console.log('2'+this.customLabel.Error_Message19);
 
            // 報告者を設定してください。
            this.showToast('error',this.customLabel.Error_Message19);
            this.showSpinner = false;
            return;
        }
    
        if (this.report.Reported_Date__c) {
        }else{
            console.log('1'+this.customLabel.Error_Message20);
            // 日付を設定してください。
            this.showToast('error',this.customLabel.Error_Message20);
            this.showSpinner = false;
            return;
        }
        console.log('----getDailyReport' + this.report.Reported_Date__c +':' +this.report.Reporter__c);
        console.log('----getDailyReport' + this.reportedDate+':' +this.report.Reporter__c);
        getDailyReport({reportDate:this.report.Reported_Date__c,reporter:this.report.Reporter__c})
            .then(result => {
                this.readReportFlag = true;  
                console.log(result);
                console.log(result.code);
                console.log(result.msg);
                if(result.code===200){
                    this.recordId = result.msg;
                   
                    // this[NavigationMixin.Navigate]({
                    //     type: 'standard__webPage',
                    //     attributes: {
                    //         // url: '/lightning/cmp/c__AuraDailyReport?c__passId='+result.msg
                    //         url: '/lightning/r/Daily_Report__c/'+result.msg+ '/view'
                    //     }
                    // });
                    // location.reload();
                     window.location.href ='/lightning/r/Daily_Report__c/'+this.recordId+ '/view'
                     // this.initPage();
                    // window.location.href = '/lightning/cmp/c__AuraDailyReport?c__passId='+result.msg;
                    this.showSpinner = false;
                }else{
                    this.showSpinner = false;
                    this.showToast('error',result.msg);
                }
            })
            .catch(error => {
                this.showSpinner = false;
                this.showToast('error',error);
            });
    }
    //字段变更
    fieldChange(event){
        
        const {value} = event.detail;
        const {fieldName} = event.target||event.currentTarget;
        let checked = event.target.value||event.currentTarget.value;
        if (value == undefined && checked != undefined) {
            this.report[fieldName] = checked;
        } else {
            this.report[fieldName] = value instanceof Array ? value[0] : value;
        }
        if(fieldName==='Reported_Date__c'){
            this.reportedDate = value instanceof Array ? value[0] : value;
        }
        if(fieldName==='Status__c') {
            this.isChangeStatus = true;
            this.statusMes = '';
            this.saveAndCloseDis = false;
        }
        if(fieldName==='Status__c'&&(this.report[fieldName]==='承認'||this.report[fieldName]==='非承認')){
            if(this.reportOwner.ManagerId !==this.currentUserId&&
            this.reportOwner.BuchangApprovalManager__c !== this.currentUserId&&
            this.reportOwner.BuchangApprovalManagerSales__c !== this.currentUserId){
                this.statusMes = this.customLabel.Error_Message4;
                this.saveAndCloseDis = true;
            }  
        }
        
        console.log('fieldChange:fieldName:' +fieldName);
        // if(fieldName==='Reported_Date__c'){
        //     this.setNotVisible();
        // }
        if(fieldName==='Daily_Report_Data_Type__c'){
            this.setAllDisableFlg();
        }
    }
    //行追加
    addEvent(){
        console.log('addEvent');
        let newActivity = {};
        // 向集合中添加新对象
        var today = new Date();
        newActivity = {
            ...newActivity,
            ['ActivityDateTime__c']: new Date(today.getFullYear(), today.getMonth(), today.getDate(), 8, 45, 0)
        };
        newActivity = {
            ...newActivity,
            ['Daily_Report__c']: this.reportedId
        };
        this.ativities.push(newActivity);
    }
    handleOpenPage(event){
        console.log("父组件handleOpenPage");
        this.showModal = true;
        this.contentIndex = event.detail.activeContentIndex;
        this.lookUpKey = event.detail.lookUpKey;
    }
    handleCloseModal(){
        this.showModal = false;
    }
    handleReturnId(event){
        console.log('父组件拿到返回的Id了');
        this.conId = event.detail.conId;
    }
    @track isValid;
    validFromEvnent(event){
        console.log('validFromEvnent');
        //验证父组件
        let isAllValid = true;
        let requiredFieldList = this.template.querySelectorAll('lightning-input-field');
        requiredFieldList.forEach(ele => {
            if ( ele.required == true && this.stringIsEmpty(ele.value)) {
                ele.reportValidity();
                isAllValid = false;
            }
        });
        console.log(isAllValid);
        if(isAllValid===false){
            this.showSpinner = false;
            this.isValid = false;
        }else{
            var flag = this.validTime();
            if(flag===false){
                this.showSpinner = false;
                this.isValid = false;
            }else{
                this.isValid = true;
            }
        }
        console.log('isValid:'+this.isValid);
        console.log('11111111111');
        var type = event.detail.type;
        // alert(value instanceof String);
        // if(type !== 'newOPDPlan' && type !== 'checkReport' && 
        //     type !== 'OPDOrSIS' && type !== 'NTCOrCDS' && type !== 'addLeadNew') {
        //     type = '';
        // }
        
        var index = event.detail.index;
        console.log('index:'+type);
        console.log('index:'+index);
        if(this.isValid){
            const childComponents = this.template.querySelectorAll('c-active-content');
            if (childComponents.length > 0) {
                childComponents.forEach(childComponent => {
                    let isValid = true;
                    //没有填写内容的报告一览 就不进行校验和保存操作
                    let isSave = childComponent.validSaveReport();
                    if(isSave) {
                        if(childComponent.theIndex===index){
                            childComponent.save(type);
                        }
                    } 
                    
                });
            }
        }
        setTimeout(() => {
                updateRecord({fields: { Id: this.recordId }});
            }, 3000); 
        
    }
    validTime(){
        console.log('validTime');
        var sHour = this.beforeHour;
        var sMinute = this.beforeMinute;
        var eHour = this.afterHour;
        var eMinute = this.afterMinute;
        if(this.stringIsEmpty(sHour)||this.stringIsEmpty(sMinute)||this.stringIsEmpty(eHour)||this.stringIsEmpty(eMinute)){
            // 勤務時間を設定してください。
            //请输入日期
            this.showToast('error','请输入日期');
            // this.timeValid = this.customLabel.Error_Message22;
            return false;
        }else{
            console.log("sHour:" +sHour);
            console.log("eHour:" +eHour);
            console.log("sMinute:" +sMinute);
            console.log("eMinute:" +eMinute);
            const regexPattern = /^[0-9]+$/;
            if (!regexPattern.test(sHour) || !regexPattern.test(sMinute) || !regexPattern.test(eHour) || !regexPattern.test(eMinute)) {
                // this.timeValid = this.customLabel.Error_Message39;//不正确的日期。
                this.showToast('error','不正确的日期。');
                return false;
            }
            // 範囲チェック
            if (sHour < 0 || sHour > 23 || sMinute < 0 || sMinute > 59 || eHour < 0 || eHour > 23 || eMinute < 0 || eMinute > 59) {
                // this.timeValid = this.customLabel.Error_Message39;
                this.showToast('error','不正确的日期。');
                return false;
            }
            // 逆転チェック 请设定开始日期之后的日期。
           
            else if (sHour > eHour || (sHour == eHour && sMinute > eMinute)) {
                // this.timeValid = this.customLabel.Error_Message2;
                this.showToast('error','请输入开始时间后的时间。');
                return false;
            }
            this.timeValid = '';
        }
    }
    saveAll(event) {
        let alertFlag = false;
        const childComponents = this.template.querySelectorAll('c-active-content');
        if (childComponents.length > 0) {
            childComponents.forEach(childComponent => {
                let childFlag = childComponent.setColor();
                if(childFlag) {
                    alertFlag = true;
                }
            });
        }
      
        if(alertFlag) {
            this.showToast('warning','拜访H层客户时,请注意收集病例数信息,按"更新客户信息"按钮登录');
        }
        console.log('saveAll');
        // const buttonName1 = event.currentTarget.name;
        const buttonName = event.target.name;
        console.log(buttonName);
        this.showSpinner = true;
        let allEvents = [];
        let isAllValid = true;
        //验证父组件
        let requiredFieldList = this.template.querySelectorAll('lightning-input-field');
        requiredFieldList.forEach(ele => {
            if ( ele.required == true && this.stringIsEmpty(ele.value)) {
                ele.reportValidity();
                isAllValid = false;
            }
        });
        if(isAllValid===false){
            this.showSpinner = false;
            return;
        }else{
            var flag = this.validTime();
            if(flag===false){
                this.showSpinner = false;
                return;
            }
        }
        //验证子组件以及获取子组件里面的值
 
        // const childComponents = this.template.querySelectorAll('c-active-content');
        if (childComponents.length > 0) {
            childComponents.forEach(childComponent => {
                const childProperty = childComponent.getActivityObj();
                console.log("c-active-content data:")
                // console.log(JSON.stringify(childProperty));
                let isValid = true;
                //没有填写内容的报告一览 就不进行校验和保存操作
                let isSave = childComponent.validSaveReport();
                console.log('event 是否需要保存。' +isSave);
                if(isSave && this.report.Status__c == '申請中') {
                    isValid = childComponent.validRequired();
                }
                if(isValid===false){
                    isAllValid = false;
                } 
                if(isSave && isValid) {
                    allEvents.push(childProperty);
                }
                
            });
        }
        console.log("c-active-content allEvents data:");
        console.log(JSON.stringify(allEvents));
        console.log('saveAll is allValid:' +isAllValid);
        
        if(isAllValid){
            saveAllData({ eventsStr: JSON.stringify(allEvents),reportStr:JSON.stringify(this.report),
                beforeHour:this.beforeHour,beforeMinute:this.beforeMinute
                ,afterHour:this.afterHour,afterMinute:this.afterMinute,reportedId:this.report.Id})
                .then(result => {
                    if(result.code===200){
                        this.showToast('success','保存成功');
                        this.showSpinner = false;
                        if(buttonName==='cancle'){
                            this.notSave();
                        }else{
                            //刷新页面
                            window.location.href ='/lightning/r/Daily_Report__c/'+this.recordId+ '/view'
                            // location.reload();
                        }
                    }else{
                        this.showSpinner = false;
                        this.showToast('error','保存失败,'+result.msg);
                    }
                })
                .catch(error => {
                    this.showSpinner = false;
                    this.showToast('error','保存失败..'+error);
                });
        }else{
            this.showSpinner = false;
            this.showToast('error','请检查页面是否有必填字段未填写!');
        }
    }
    beforeHourChange(event) {
        this.beforeHour = event.detail.value;
    }
    beforeMinuteChange(event) {
        this.beforeMinute = event.detail.value;
    }
    afterHourChange(event) {
        this.afterHour = event.detail.value;
    }
    afterMinuteChange(event) {
        this.afterMinute = event.detail.value;
    }
    cancleApply(){
        // if(this.showSpinner===true){
        //     this.showSpinner = false;
        // }else{
        //     this.showSpinner = true;
        // }
    }
    notSave(){
        window.location = '/lightning/o/Daily_Report__c/list?filterName=Recent';
    }
    // showToast(variant,mes) {
    //     const event = new ShowToastEvent({
    //         message: mes,
    //         variant: variant,
    //     });
    //     this.dispatchEvent(event);
    // }
 
    showToast(type,msg) {
        if(type == 'success'){
            const event = new ShowToastEvent({
                message: msg,
                variant: type
            });
            this.dispatchEvent(event);
        }else{
            const event = new ShowToastEvent({
                message: msg,
                variant: type,
                mode: 'sticky'
            });
            this.dispatchEvent(event);
        }
    }
    stringIsEmpty(str) {
        if (str === '' || str === undefined || str === null) {
            return true;
        } else {
            return false;
        }
    }
    cancleApplyJs() {
         this.showSpinner = true;
        cancleApply({recordId:this.recordId}).then(result=>{
            if(result === '1') {
                this.showToast('success','取消成功。');
                location.reload();
            } else {
                this.showToast('error','取消失败'+result);
            }
            this.showSpinner = false;
            //刷新页面
            
        });
 
    }
    updateRecordView(recordId) {
        updateRecord({fields: { Id: recordId }});
    }
}