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 }}); } }