import { Component, inject, ViewChild } from '@angular/core'; import { ReactiveFormsModule } from '@angular/forms'; import { AngularMaterialModule } from '../../shared/module/angular-material.module'; import { CommonModule } from '@angular/common'; import { StepperSelectionEvent } from '@angular/cdk/stepper'; import { ApplicationComponent } from "../application/application.component"; import { HolderComponent } from '../holder/holder.component'; import { GoodsComponent } from '../goods/goods.component'; import { TravelPlanComponent } from '../travel-plan/travel-plan.component'; import { ShippingComponent } from '../shipping/shipping.component'; import { UserPreferencesService } from '../../core/services/user-preference.service'; import { UserPreferences } from '../../core/models/user-preference'; @Component({ selector: 'app-add-carnet', imports: [AngularMaterialModule, CommonModule, ReactiveFormsModule, ApplicationComponent, HolderComponent, GoodsComponent, TravelPlanComponent, ShippingComponent], templateUrl: './add-carnet.component.html', styleUrl: './add-carnet.component.scss' }) export class AddCarnetComponent { currentStep = 0; isLinear = true; applicationType: 'new' | 'additional' | 'duplicate' | 'extend' | null = 'new'; isEditMode = false; headerid: number = 0; applicationName: string = ''; userPreferences: UserPreferences; missingSectionMessage: string = ''; // Track completion of each step stepsCompleted = { applicationDetail: false, holderSelection: false, goodsSection: false, travelPlan: false, shipping: false }; @ViewChild(HolderComponent, { static: false }) private holderComponent!: HolderComponent; @ViewChild(ShippingComponent, { static: false }) private shippingComponent!: ShippingComponent; constructor(userPrefenceService: UserPreferencesService) { this.userPreferences = userPrefenceService.getPreferences(); } onStepChange(event: StepperSelectionEvent): void { this.currentStep = event.selectedIndex; } onApplicationDetailCreated(data: { headerid: number, applicationName: string }): void { this.headerid = data.headerid; this.applicationName = data.applicationName; this.stepsCompleted.applicationDetail = true; this.isLinear = false; // Disable linear mode after application detail is created this.holderComponent.refreshHolder(); } onHolderSelectionSaved(completed: boolean): void { this.stepsCompleted.holderSelection = completed; this.isAllSectionsCompleted(); } onHolderSelectionUpdated(updated: boolean): void { if (updated) { this.shippingComponent?.refreshShippingData(); } } onGoodsSectionSaved(completed: boolean): void { this.stepsCompleted.goodsSection = completed; this.isAllSectionsCompleted(); } onTravelPlanSaved(completed: boolean): void { this.stepsCompleted.travelPlan = completed; this.isAllSectionsCompleted(); } onShippingSaved(completed: boolean): void { this.stepsCompleted.shipping = completed; this.isAllSectionsCompleted(); } isAllSectionsCompleted(): void { let allSectionsCompleted = this.stepsCompleted.applicationDetail && this.stepsCompleted.holderSelection && this.stepsCompleted.goodsSection && this.stepsCompleted.shipping && this.stepsCompleted.travelPlan; if (allSectionsCompleted) { this.missingSectionMessage = ''; } else { let message = 'Missing details in: '; if (!this.stepsCompleted.applicationDetail) { message += 'Application Name, '; } if (!this.stepsCompleted.holderSelection) { message += 'Holder Selection, '; } if (!this.stepsCompleted.goodsSection) { message += 'Goods Section, '; } if (!this.stepsCompleted.travelPlan) { message += 'Travel Plan, '; } if (!this.stepsCompleted.shipping) { message += 'Shipping & Payment, '; } // Remove trailing comma and space this.missingSectionMessage = message.replace(/, $/, ''); } } }