import {AfterViewInit, Component, OnInit, ViewEncapsulation} from '@angular/core'; import {FieldType} from '@ngx-formly/material'; import {AllService} from '../../../all.service'; import {ActivatedRoute} from '@angular/router'; import {saveAs} from 'file-saver/dist/FileSaver'; import {FormGroup} from '@angular/forms'; import {FormlyFieldConfig} from '@ngx-formly/core'; @Component({ selector: 'jhi-step-summary-funeral', templateUrl: './step-summary-funeral.component.html', styleUrls: ['./step-summary-funeral.component.scss'], encapsulation: ViewEncapsulation.None }) export class StepSummaryFuneralComponent extends FieldType implements OnInit { loans = {}; documents: Array = []; showModalDocument = false; checkboxAreChecked = []; canShowNextStep = false; dateMax = this.calculateDate({year: 1}); dateMin = this.calculateDate({month: 1}); errorCapital = false; formSubmited = false; form = new FormGroup({}); fields: FormlyFieldConfig[] = [ { fieldGroup: [ { key: 'insurance_effective_date', type: 'datepicker', className: 'summary-date', templateOptions: { label: 'Date d\'effet : ', required: true, minDate: this.dateMin, maxDate: this.dateMax, monthOnly: true, options: [], change: (field) => { if (!field.formControl.errors) { this.model['recalculate'] = true; } else { this.model['insurance_effective_date'] = null; this.model['recalculate'] = false; } } } }, ] }]; constructor(public allService: AllService, private route: ActivatedRoute ) { super(); } ngOnInit(): void { super.ngOnInit(); const projectid = this.route.snapshot.paramMap.get('projectid'); const customerid = this.route.snapshot.paramMap.get('customerid'); const productid = this.route.snapshot.paramMap.get('productid'); const brokerid = this.route.snapshot.paramMap.get('brokerid'); this.allService.customerVariableService.getFuneralSummaryByCustomerAndProjectId(customerid, projectid).toPromise().then((response) => { this.loans = response.body; }); this.allService.productTemplateService.getAllPreContractualsByProductAndSource(productid, brokerid).toPromise().then((response) => { this.documents = response.body; if (this.documents.length === 0) { this.canShowNextStep = true; } }); this.allService.getShowModalDocumentFromSummaryObserVable().subscribe((showModal) => { this.showModalDocument = showModal; }); this.allService.getSubmitedStepAsObserVable().subscribe((data) => { this.formSubmited = data; }); if (!this.model['contract_capital']) { this.allService.currentStepCustomError = true; this.errorCapital = true; } } calculateDate(toAdd) { const today = new Date(); const dateFormDay = this.addDays(today, (toAdd.day ? toAdd.day : 0)); const d = '01'; const dateFormMonth = this.addMonth(today, (toAdd.month ? toAdd.month : 0)); let mm = null; mm = dateFormMonth.getMonth() + 1; if (today.getMonth() === 11) { toAdd.year = 1; } const yyyy = today.getFullYear() + (toAdd.year ? toAdd.year : 0); if (mm < 10) { mm = '0' + mm; } return d + '/' + mm + '/' + yyyy; } addDays(date, days) { const newDate = new Date(date); newDate.setDate(newDate.getDate() + days); return newDate; } addMonth(date, months) { const newDate = new Date(date); newDate.setMonth(newDate.getMonth() + months); return newDate; } refuse() { this.showModalDocument = false; } downloadFile(filePath): void { const pathEncoded = encodeURIComponent(filePath); this.allService.fileStorageManagerService.downloadDocumentByPath(pathEncoded).subscribe( blob => { saveAs(blob, filePath); }, () => { alert('Une erreur est survenue durant le téléchargement'); }); } toggleCheck(event, index) { this.checkboxAreChecked[index] = event.target.checked; this.canShowNextStep = this.checkboxAreChecked.length === this.documents.length && this.checkboxAreChecked.every(element => element); } nextStep() { this.allService.currentStepCustomError = false; this.allService.stepsSubject.next('next'); } setCapital(value): void { if (value) { this.allService.currentStepCustomError = false; this.errorCapital = false; } else { this.allService.currentStepCustomError = true; this.errorCapital = true; } this.model['contract_capital'] = value; } closeIfOutside(e: any): void { if (e.target.id === 'modalDocuments') { this.showModalDocument = false; } } }