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'; import * as events from 'events'; @Component({ selector: 'jhi-step-summary', templateUrl: './step-summary.component.html', styleUrls: ['./step-summary.component.scss'], encapsulation: ViewEncapsulation.None }) export class StepSummaryComponent extends FieldType implements OnInit { loans: Array = []; documents: Array = []; showModalDocument = false; checkboxAreChecked = []; canShowNextStep = false; dateMax = this.calculateDate({year: 1}); dateMin = this.calculateDate({day: 1}); lessThanThousand = false; environnementName = ''; productid = ''; 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, options: [], change: (field) => { const recalculate = 'recalculate'; if (!field.formControl.errors) { this.model[recalculate] = true; } else { const insuranceEffectiveDate = 'insurance_effective_date'; this.model[insuranceEffectiveDate] = null; this.model[recalculate] = false; } } } }, ] }]; isForever = false; isAxeria = 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'); this.productid = this.route.snapshot.paramMap.get('productid'); const brokerid = this.route.snapshot.paramMap.get('brokerid'); const productcollectionid = this.route.snapshot.paramMap.get('productcollectionid') ? this.route.snapshot.paramMap.get('productcollectionid') : ''; this.isForever = productcollectionid === 'foreverC1'; this.isAxeria = productcollectionid === 'axeria-premium-crd'; this.allService.customerVariableService.getSummaryByCustomerAndProjectId(customerid, projectid).toPromise().then((response) => { this.loans = response.body.loans; }); this.allService.productTemplateService.getAllPreContractualsByProductAndSource(this.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.lessThanThousand = this.model.project_total_money_saved < 1000; this.environnementName = this.getPlatformFromUrl(); } calculateDate(toAdd) { const today = new Date(); const dateFormDay = this.addDays(today, (toAdd.day ? toAdd.day : 0)); const dd = dateFormDay.getDate(); let mm = null; if (dateFormDay.getMonth() + 1 === today.getMonth() + 1) { mm = today.getMonth() + 1 + (toAdd.month ? toAdd.month : 0); } else { mm = dateFormDay.getMonth() + 1 + (toAdd.month ? toAdd.month : 0); } const yyyy = today.getFullYear() + (toAdd.year ? toAdd.year : 0); let d = ''; if (dd < 10) { d = '0' + dd; } else { d = dd.toString(); } if (mm < 10) { mm = '0' + mm; } return d + '/' + mm + '/' + yyyy; } addDays(date: any, days: any) { const newDate = new Date(date); newDate.setDate(newDate.getDate() + days); return newDate; } downloadFile(filePath, fileName): void { const pathEncoded = encodeURIComponent(filePath); const extension = filePath.substring(filePath.lastIndexOf('.')); const fileNameToDownload = this.productid + '-' + fileName + extension; this.allService.fileStorageManagerService.downloadDocumentByPath(pathEncoded).subscribe( blob => { const filename = filePath.replace(/^.*[\\/]/, ''); saveAs(blob, filename); //saveAs(blob, fileNameToDownload); /*const file = new Blob([blob], {type: 'application/pdf'}); const fileURL = URL.createObjectURL(file); window.open(fileURL);*/ }, () => { 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'); } closeIfOutside(e: any): void { if (e.target.id === 'modalDocuments') { this.showModalDocument = false; } } getPlatformFromUrl(): string { const hostname = window && window.location && window.location.hostname; const hostInformation = hostname.split('.'); if (/^.*localhost.*/.test(hostname)) { return 'magnolia'; } else if (hostInformation.includes('magnolia')) { return 'magnolia'; } else if (hostInformation.includes('fluo')) { return 'fluo'; } else { return 'simulassur'; } } }