import { Component, inject, ViewChild } from '@angular/core'; import { HomeService } from '../core/services/home.service'; import { AngularMaterialModule } from '../shared/module/angular-material.module'; import { ChartComponent } from './chart/chart.component'; import { MatPaginator, MatPaginatorIntl } from '@angular/material/paginator'; import { MatSort } from '@angular/material/sort'; import { MatTableDataSource } from '@angular/material/table'; import { UserPreferences } from '../core/models/user-preference'; import { UserPreferencesService } from '../core/services/user-preference.service'; import { CommonModule } from '@angular/common'; import { CustomPaginator } from '../shared/custom-paginator'; import { CarnetStatus } from '../core/models/carnet-status'; import { ApiErrorHandlerService } from '../core/services/common/api-error-handler.service'; import { CommonService } from '../core/services/common/common.service'; import { NotificationService } from '../core/services/common/notification.service'; import * as XLSX from 'xlsx'; import { NavigationService } from '../core/services/common/navigation.service'; import { ConfirmDialogComponent } from '../shared/components/confirm-dialog/confirm-dialog.component'; import { MatDialog } from '@angular/material/dialog'; @Component({ selector: 'app-home', imports: [AngularMaterialModule, ChartComponent, CommonModule], templateUrl: './home.component.html', styleUrl: './home.component.scss', providers: [{ provide: MatPaginatorIntl, useClass: CustomPaginator }], }) export class HomeComponent { carnetData: any[] = []; isLoading = false; showTable = false; userPreferences: UserPreferences; carnetStatuses: CarnetStatus[] = []; dataSource = new MatTableDataSource(); @ViewChild(MatPaginator, { static: false }) set paginator(value: MatPaginator) { this.dataSource.paginator = value; } @ViewChild(MatSort, { static: false }) set sort(value: MatSort) { this.dataSource.sort = value; } displayedColumns: string[] = ['applicationName', 'holderName', 'carnetNumber', 'usSets', 'foreignSets', 'transitSets', 'carnetValue', 'issueDate', 'expiryDate', 'orderType', 'carnetStatus', 'actions']; private homeService = inject(HomeService); private errorHandler = inject(ApiErrorHandlerService); private notificationService = inject(NotificationService); private commonService = inject(CommonService); private navigationService = inject(NavigationService); private dialog = inject(MatDialog); constructor(userPrefenceService: UserPreferencesService) { this.userPreferences = userPrefenceService.getPreferences(); } ngOnInit(): void { this.loadCarnetStatuses(); this.loadCarnetData(); } loadCarnetStatuses(): void { this.commonService.getCarnetStatuses().subscribe({ next: (carnetStatuses) => { this.carnetStatuses = carnetStatuses; }, error: (error) => { console.error('Error loading carnet status:', error); } }); } loadCarnetData(): void { this.isLoading = true; this.homeService.getCarnetSummaryData().subscribe({ next: (data) => { this.carnetData = data; this.isLoading = false; this.defaultCarnetStatusData(); }, error: (error) => { console.error('Error loading carnet data:', error); this.isLoading = false; } }); } onCarnetStatusClick(event: any): void { this.isLoading = true; this.showTable = false; this.homeService.getCarnetDataByStatus(event.spid, event.carnetStatus).subscribe({ next: (carnetDetails) => { this.isLoading = false; this.showTable = true; this.dataSource.data = carnetDetails; this.dataSource.paginator = this.paginator; this.dataSource.sort = this.sort; }, error: (error) => { let errorMessage = this.errorHandler.handleApiError(error, 'Failed to load carnet data for the selected status'); this.notificationService.showError(errorMessage); this.isLoading = false; console.error('Error loading carnet data for the selected status:', error); } }); } defaultCarnetStatusData(): void { // if the carnet data has record with the status 'T' (submitted), then load that data const inProgressData = this.carnetData?.[0]?.CARNETSTATUS?.includes('T'); if (inProgressData) { let spid = this.carnetData[0].SPID; var carentData = { spid: spid, carnetStatus: 'T' } this.onCarnetStatusClick(carentData); } } exportData() { try { // Prepare worksheet data const worksheetData = this.prepareDownloadData(); // Create workbook and worksheet const workbook = XLSX.utils.book_new(); const worksheet = XLSX.utils.json_to_sheet(worksheetData); // Add worksheet to workbook XLSX.utils.book_append_sheet(workbook, worksheet, 'Carnet Data'); // Generate Excel file const fileName = `carnet_data_${new Date().toISOString()}.xlsx`; XLSX.writeFile(workbook, fileName); } catch (error) { console.error('Error downloading goods items:', error); this.notificationService.showError('Failed to download Excel file'); } } prepareDownloadData(): any[] { return this.dataSource.data.map(item => ({ 'Application Name': item.applicationName, 'Holder Name': item.holderName, 'Carnet Number': item.carnetNumber, 'US Sets': item.usSets, 'Foreign Sets': item.foreignSets, 'Transit Sets': item.transitSets, 'Carnet Value': item.carnetValue, 'Issue Date': item.issueDate ? new Date(item.issueDate).toLocaleDateString() : '', 'Expiry Date': item.expiryDate ? new Date(item.expiryDate).toLocaleDateString() : '', 'Order Type': item.orderType, 'Carnet Status': this.getCarnetStatusLabel(item.carnetStatus) })); } getCarnetStatusLabel(value: string): string { const carnetStatus = this.carnetStatuses.find(t => t.value === value); return carnetStatus ? carnetStatus.name : value; } processCarnet(item: any): void { if (item && item.HEADERID) { this.navigateTo(['edit-carnet', item.HEADERID], { queryParams: { applicationname: item.applicationName } }); } else { this.notificationService.showError('Invalid carnet data'); } } viewCarnet(item: any): void { if (item && item.HEADERID) { this.navigateTo(['view-carnet', item.HEADERID], { queryParams: { applicationname: item.applicationName } }); } else { this.notificationService.showError('Invalid carnet data'); } } printCarnet(item: any): void { if (item && item.HEADERID) { } else { this.notificationService.showError('Invalid carnet data'); } } deleteCarnet(headerId: number): void { if (headerId) { const dialogRef = this.dialog.open(ConfirmDialogComponent, { width: '350px', data: { title: 'Delete Carnet', message: 'Are you sure you want to delete the carnet?', confirmText: 'Yes', cancelText: 'Cancel' } }); dialogRef.afterClosed().subscribe(result => { if (result) { this.homeService.deleteCarnet(headerId).subscribe({ next: () => { this.notificationService.showSuccess('Carnet deleted successfully'); this.loadCarnetData(); }, error: (error) => { let errorMessage = this.errorHandler.handleApiError(error, 'Failed to delete carnet'); this.notificationService.showError(errorMessage); console.error('Error deleting carnet:', error); } }); } }); } else { this.notificationService.showError('Invalid carnet data'); } } resetClient(headerId: number): void { if (headerId) { const dialogRef = this.dialog.open(ConfirmDialogComponent, { width: '350px', data: { title: 'Reset Carnet', message: 'Are you sure you want to reset the carnet to client?', confirmText: 'Yes', cancelText: 'Cancel' } }); dialogRef.afterClosed().subscribe(result => { if (result) { this.homeService.resetCarnet(headerId).subscribe({ next: () => { this.notificationService.showSuccess('Carnet reset to client successfully'); this.loadCarnetData(); }, error: (error) => { let errorMessage = this.errorHandler.handleApiError(error, 'Failed to reset carnet client'); this.notificationService.showError(errorMessage); console.error('Error resetting carnet client:', error); } }); } }); } else { this.notificationService.showError('Invalid carnet data for reset'); } } navigateTo(route: any[], extras?: any): void { this.navigationService.navigate(route, extras); } }