import { Component, signal, ViewChild } from '@angular/core'; import { Button } from 'primeng/button'; import { ConfirmDialog } from 'primeng/confirmdialog'; import { Dialog } from 'primeng/dialog'; import { FormsModule } from '@angular/forms'; import { IconField } from 'primeng/iconfield'; import { InputIcon } from 'primeng/inputicon'; import { InputText } from 'primeng/inputtext'; import { Table, TableModule } from 'primeng/table'; import { Toolbar } from 'primeng/toolbar'; import { Country, PropertyService } from '@/pages/service/property.service'; import { ConfirmationService, MessageService } from 'primeng/api'; import { CountryService } from '@/pages/service/country.service'; import { AttachmentService } from '@/pages/service/attachment.service'; import { environment } from '../../../environments/environments'; import { Industry, IndustryService } from '@/pages/service/industry.service'; import { Toast } from 'primeng/toast'; import { Ripple } from 'primeng/ripple'; interface Column { field: string; header: string; customExportHeader?: string; } interface ExportColumn { title: string; dataKey: string; } @Component({ selector: 'app-industry-manager', imports: [ Button, ConfirmDialog, Dialog, FormsModule, IconField, InputIcon, InputText, TableModule, Toolbar, Toast ], templateUrl: './industry-manager.html', styleUrl: './industry-manager.scss', providers: [MessageService, ConfirmationService, PropertyService, CountryService, AttachmentService], }) export class IndustryManager { industryDialog: boolean = false; industries = signal([]); industry = signal({}); selectedIndustries!: Industry[] | null; submitted: boolean = false; statuses!: any[]; @ViewChild('dt') dt!: Table; exportColumns!: ExportColumn[]; cols!: Column[]; countries: Country[] = []; constructor( private messageService: MessageService, private confirmationService: ConfirmationService, private industryService: IndustryService, private countryService: CountryService, private attachmentService: AttachmentService, ) {} exportCSV() { // this.dt.exportCSV(); console.debug(this, ' -- ', this.dt); } ngOnInit() { this.selectedIndustries = []; this.industryService.getIndustries().subscribe((industries) => { console.log('Industries', industries); this.industries.set(industries); }); this.countryService.getCountries().then((countries) => { this.countries = countries; }); } loadDemoData() { this.statuses = [ { label: 'INSTOCK', value: 'instock' }, { label: 'LOWSTOCK', value: 'lowstock' }, { label: 'OUTOFSTOCK', value: 'outofstock' } ]; this.cols = [ { field: 'id', header: 'ID', customExportHeader: 'Branchen ID' }, { field: 'name', header: 'Name' } ]; this.exportColumns = this.cols.map((col) => ({ title: col.header, dataKey: col.field })); } onGlobalFilter(table: Table, event: Event) { table.filterGlobal((event.target as HTMLInputElement).value, 'contains'); } openNew() { this.industry.set({}); this.submitted = false; this.industryDialog = true; } editIndustry(industry: Industry) { this.industry.set({ ...industry }); this.industryDialog = true; } deleteSelected() { this.confirmationService.confirm({ message: 'Sind Sie sicher, dass sie die angewählten Branchen endgültig löschen möchten?', header: 'Bestätigung', icon: 'pi pi-exclamation-triangle', accept: () => { console.log('properties to delete', this.selectedIndustries); if (this.selectedIndustries === null) { return; } this.industryService.deleteIndustries(this.selectedIndustries).subscribe({ next: () => { this.industries.set(this.industries().filter((val) => !this.selectedIndustries?.includes(val))); this.selectedIndustries = null; this.messageService.add({ severity: 'success', summary: 'Successful', detail: 'Branche wurde erfolgreich gelöscht.', life: 3000 }); }, error: (err) => { console.log('Error while deleting industries -- Error: ' + err + ' -- Industries: ', this.selectedIndustries); this.messageService.add({ severity: 'error', summary: 'Fehler', detail: 'Beim Löschen der Branche ist ein Fehler aufgetreten. Bitte versuchen Sie es erneut.', life: 3000 }); } }); } }); } hideDialog() { this.industryDialog = false; this.submitted = false; } deleteIndustry(industry: Industry) { this.confirmationService.confirm({ message: 'Sind Sie sicher, dass sie "' + industry.name + '" löschen möchten?', acceptLabel: 'Ja', acceptButtonStyleClass: 'p-button-danger', rejectLabel: 'Nein', header: 'Bitte bestätigen Sie', icon: 'pi pi-exclamation-triangle', accept: () => { this.industryService.deleteIndustry(industry).subscribe({ next: () => { this.industries.set(this.industries().filter((val) => val.id !== industry.id)); this.industry.set({}); this.messageService.add({ severity: 'success', summary: 'Successful', detail: 'Branche erfolgreich gelöscht', life: 3000 }); }, error: (err) => { console.log('Error while deleting industry -- Error: ' + err + ' -- Industry: ', industry); this.messageService.add({ severity: 'error', summary: 'Fehler', detail: 'Beim Löschen der Branche ist ein Fehler aufgetreten. Bitte versuchen Sie es erneut.', life: 3000 }); } }); }, reject: () => { this.messageService.add({ severity: 'info', summary: 'Info', detail: 'Der Löschvorgang wurde abgebrochen.', life: 3000 }) } }); } findIndexById(id?: string): number { let index = -1; // console.log("Looking for id " + id); for (let i = 0; i < this.industries().length; i++) { // console.log("current item id: " + this.properties()[i].id) if (this.industries()[i].id === id) { // console.log("Index", this.properties()[i].id, i); index = i; break; } } return index; } saveIndustry() { this.submitted = true; let _industry = this.industry(); let _industries = this.industries(); console.log('Saving Industry', _industry); if (this.industry.name?.trim()) { if (_industry.id) { // update this.industryService.updateIndustry(_industry).subscribe({ next: (arg) => { this.messageService.add({ severity: 'success', summary: 'Successful', detail: 'Branche aktualisiert', life: 3000 }); _industries[this.findIndexById(_industry.id)] = _industry; this.industries.set([..._industries]); }, error: (err) => { console.log('Error while updating industry -- Error: ' + err + ' -- Industry: ', _industry); } }); } else { this.industryService.createIndustry(_industry).subscribe({ next: (arg) => { this.messageService.add({ severity: 'success', summary: 'Successful', detail: 'Branche angelegt', life: 3000 }); this.industries.set([..._industries, arg]); }, error: (err) => { console.log('Error while creating industry -- Error: ' + err + ' -- Industry: ', _industry); } }); } this.industryDialog = false; this.industry.set({}); } } protected readonly environment = environment; }