import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; import { CommonModule } from '@angular/common'; import { AngularSvgIconModule } from 'angular-svg-icon'; import { FormControl, ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup, Validators, } from '@angular/forms'; import { NgbModule, NgbPopover } from '@ng-bootstrap/ng-bootstrap'; import { map, Observable, startWith } from 'rxjs'; // enums import { ePosition, eSharedString, eStringPlaceholder } from '../../enums'; import { eFactoringFormControl, eFactoringPdfFormat, eFactoringPdfOrganize, } from './enums'; // interfaces import { IFactoringCheckboxItem, IFactoringExportItem } from './interfaces'; // components import { CaAppTooltipV2Component } from '../ca-app-tooltip-v2/ca-app-tooltip-v2.component'; import { CaCheckboxComponent } from '../ca-checkbox/ca-checkbox.component'; import { CaSpinnerComponent } from '../ca-spinner/ca-spinner.component'; import { InputComponent, CaiInputConfig } from 'carriera-intern-components'; // constants import { FactoringDropdownConstants } from './utils/constants'; // svg routes import { SharedSvgRoutes } from '../../utils/svg-routes'; import { CheckboxSvgRoutes } from '../ca-checkbox/utils/svg-routes'; @Component({ selector: 'app-ca-factoring-dropdown', templateUrl: './ca-factoring-dropdown.component.html', styleUrl: './ca-factoring-dropdown.component.scss', standalone: true, imports: [ // modules CommonModule, AngularSvgIconModule, NgbModule, ReactiveFormsModule, // components CaAppTooltipV2Component, CaCheckboxComponent, CaSpinnerComponent, InputComponent, ], }) export class CaFactoringDropdownComponent implements OnInit { @Input() itemsCount: number = 0; @Input() set isExportFinished(isExportFinished: boolean) { if (isExportFinished) { this.isInProgressUpload = false; this.isInProgressExport = false; this.isExportClicked = false; this.onCLoseDropdown(); } } @Output() onFactoringExport: EventEmitter = new EventEmitter(); @Output() onFactoringUploadToRts: EventEmitter = new EventEmitter(); public checkBoxItems: IFactoringCheckboxItem[] = FactoringDropdownConstants.CHECKBOX_COLUMNS; public factoringDropdownPopover: NgbPopover | null = null; public factoringDropdownForm!: UntypedFormGroup; public isByTagDisabled: boolean = false; public checkedCount$!: Observable; // svg routes public sharedSvgRoutes = SharedSvgRoutes; public checkboxSvgRputes = CheckboxSvgRoutes; public scheduleNoInputConfig: CaiInputConfig = FactoringDropdownConstants.SCHEDULE_NO_INPUT_CONFIG; // enums public eFactoringFormControl = eFactoringFormControl; public eFactoringPdfFormat = eFactoringPdfFormat; public eFactoringPdfOrganize = eFactoringPdfOrganize; public eSharedString = eSharedString; public ePosition = ePosition; public isInProgressUpload = false; public isInProgressExport = false; public isExportClicked = false; constructor(private formBuilder: UntypedFormBuilder) {} ngOnInit(): void { this.createForm(); } private createForm(): void { this.factoringDropdownForm = this.formBuilder.group({ scheduleNo: [eStringPlaceholder.EMPTY, Validators.required], pdfFormat: [null, Validators.required], organizePdf: [null, Validators.required], includeUntagged: false, }); this.checkBoxItems.forEach((item) => { this.factoringDropdownForm.addControl( item.name, new FormControl({ value: item.isChecked, disabled: item.isChecked, }) ); }); this.checkedCount$ = this.factoringDropdownForm.valueChanges.pipe( startWith(this.factoringDropdownForm.getRawValue()), map(() => { const raw = this.factoringDropdownForm.getRawValue(); return this.checkBoxItems.filter((c) => !!raw[c.name]).length; }) ); } public onFactoringDropdownOpenCloseClick( factoringDropdownPopover: NgbPopover, event?: MouseEvent ): void { event?.stopPropagation(); if (factoringDropdownPopover.isOpen()) { factoringDropdownPopover.close(); this.factoringDropdownPopover = null; } else { this.factoringDropdownPopover = factoringDropdownPopover; this.factoringDropdownPopover?.open(); } } public onCLoseDropdown(): void { this.isInProgressUpload = false; this.isInProgressExport = false; this.factoringDropdownPopover?.close(); } public onCheckboxRowClick(name: string): void { const control = this.factoringDropdownForm.get(name); if (!control || control.disabled) { return; } control.setValue(!control.value); control.markAsTouched(); } public onRadioPdfFormatClick( value: eFactoringPdfFormat, event: MouseEvent ): void { event.stopPropagation(); const pdfFormat = this.factoringDropdownForm.get( eFactoringFormControl.PDF_FORMAT ); if (pdfFormat && !pdfFormat.disabled && pdfFormat.value !== value) { pdfFormat.setValue(value); pdfFormat.markAsTouched(); } const organizePdfCtrl = this.factoringDropdownForm.get( eFactoringFormControl.ORGANIZE_PDF ); if (value === eFactoringPdfFormat.MULTIPLE) { organizePdfCtrl?.setValue(eFactoringPdfOrganize.BY_LOAD); this.isByTagDisabled = true; } else { this.isByTagDisabled = false; } } public onRadioOrganizePdfClick( value: eFactoringPdfOrganize, event: MouseEvent ): void { event.stopPropagation(); const organizePdf = this.factoringDropdownForm.get( eFactoringFormControl.ORGANIZE_PDF ); if ( organizePdf && !organizePdf.disabled && organizePdf.value !== value ) { organizePdf.setValue(value); organizePdf.markAsTouched(); } } public onExportClick(event?: MouseEvent): void { event?.stopPropagation(); this.isExportClicked = true; } public onExportBatch(isUpload: boolean, event?: MouseEvent): void { event?.stopPropagation(); isUpload ? (this.isInProgressUpload = true) : (this.isInProgressExport = true); const formValue = this.factoringDropdownForm.getRawValue(); const factoringExportItem: IFactoringExportItem = { scheduleNumber: formValue.scheduleNo, origin: formValue.origin, destination: formValue.destination, payTerm: formValue.payTerm, pdfFormat: formValue.pdfFormat, pdfOrganize: formValue.organizePdf, includeUntagged: formValue.includeUntagged, }; isUpload ? this.onFactoringUploadToRts.emit(factoringExportItem) : this.onFactoringExport.emit(factoringExportItem); } }