import {Component, inject, OnInit, signal, viewChild} from '@angular/core'; import {NzTableComponent} from "ng-zorro-antd/table"; import {ModalButton} from "../modal-button/modal-button"; import {ModalNav} from "../modal-nav/modal-nav"; import {NzDividerComponent} from "ng-zorro-antd/divider"; import {NzIconDirective} from "ng-zorro-antd/icon"; import {QuotationForm} from "../quotation-form/quotation-form"; import { GetQuotationDto, GetQuotationProductDto, QuotationsService } from "../../services/api"; import {NzNotificationService} from "ng-zorro-antd/notification"; import {firstValueFrom} from "rxjs"; import {FileService} from "../../services/file.service"; import {QuantityForm} from "../quantity-form/quantity-form"; import {AuthService} from "../../services/auth.service"; @Component({ selector: 'app-quotation-table', imports: [ ModalButton, ModalNav, NzDividerComponent, NzIconDirective, NzTableComponent, QuotationForm, QuantityForm, ], templateUrl: './quotation-table.html', styleUrl: './quotation-table.css', }) export class QuotationTable implements OnInit { private quotationsService = inject(QuotationsService); private notificationService = inject(NzNotificationService); private fileService = inject(FileService); private authService = inject(AuthService); quotations = signal([]); quotationsLoading = signal(false); admin = signal(false); modal = viewChild.required('modalNav'); modalQuantity = viewChild.required('modalQuantity'); async ngOnInit() { await this.fetchQuotations(); this.admin.set(this.authService.isAdmin()); } async fetchQuotations() { this.quotationsLoading.set(true) try { const quotations = await firstValueFrom(this.quotationsService.getAllQuotationEndpoint()) this.quotations.set(quotations); } catch { this.notificationService.error('Erreur', 'Erreur lors du chargement des devis') } this.quotationsLoading.set(false) } async delete(quotation: number) { this.quotationsLoading.set(true) try { await firstValueFrom(this.quotationsService.deleteQuotationEndpoint(quotation)) this.notificationService.success('Success', 'Suppression effectuée') } catch { this.notificationService.error('Erreur', 'Impossible de supprimer la ligne') } this.quotationsLoading.set(false) await this.fetchQuotations(); } async export(quotationId: number) { this.quotationsLoading.set(true) try { const pdf = await firstValueFrom( this.quotationsService.getQuotationPdfEndpoint(quotationId, "response") ); this.fileService.downloadBlob(pdf) } catch { this.notificationService.error('Erreur', 'Impossible de générer un PDF'); } this.quotationsLoading.set(false) } async edit(id: number, updateQuotationComponent: QuotationForm) { if (updateQuotationComponent.quotationForm.invalid) { this.notificationService.error('Erreur', 'Formulaire invalide'); return; } try { const quotations = updateQuotationComponent.quotationForm.getRawValue(); await firstValueFrom(this.quotationsService.updateQuotationEndpoint(id, quotations)); this.notificationService.success('Success', 'Devis modifié') } catch { this.notificationService.error('Erreur', 'Erreur lors de la modification') } } async deleteProduct(productId: number, quotationId: number) { this.quotationsLoading.set(true) try { await firstValueFrom(this.quotationsService.deleteProductFromQuotationEndpoint(productId, quotationId)) this.notificationService.success('Success', 'Suppression effectuée') } catch { this.notificationService.error('Erreur', 'Impossible de supprimer la ligne') } this.quotationsLoading.set(false) await this.fetchQuotations(); } async editQuantity(productId: number, quotationId: number, updateQuantityComponent: QuantityForm) { if (updateQuantityComponent.quantityForm.invalid) { this.notificationService.error('Erreur', 'Formulaire invalide') return; } try { const quantity = updateQuantityComponent.quantityForm.getRawValue(); await firstValueFrom(this.quotationsService.patchQuotationProductQuantityEndpoint(productId, quotationId, quantity)) this.notificationService.success('Success', 'Quantité modifiée') } catch { this.notificationService.error('Erreur', 'Erreur lors de la modification') } } selectedQuotation: GetQuotationDto | null = null; openEditModal(quotation: GetQuotationDto) { this.selectedQuotation = {...quotation}; this.modal().showModal(); } async onModalOk(id: number, updateQuotationComponent: QuotationForm, modal: ModalNav) { if (!this.selectedQuotation) return; await this.edit(id, updateQuotationComponent); updateQuotationComponent.quotationForm.reset(); this.onModalCancel(modal); await this.fetchQuotations(); } onModalCancel(modal: ModalNav) { modal.isVisible = false; } selectedQuantity: GetQuotationProductDto | null = null; openEditQuantityModal(quantity: GetQuotationProductDto) { this.selectedQuantity = {...quantity}; this.modalQuantity().showModal(); } async onModalQuantityOk(productId: number, quotationId: number, updateQuantityComponent: QuantityForm, modal: ModalNav) { if (!this.selectedQuantity) return; await this.editQuantity(productId, quotationId, updateQuantityComponent); updateQuantityComponent.quantityForm.reset(); this.onModalCancel(modal); await this.fetchQuotations(); } }