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, QuotationproductsService, 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"; @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 quotationProductsService = inject(QuotationproductsService) private fileService = inject(FileService); quotations = signal([]); quotationsLoading = signal(false); modal = viewChild.required('modalNav'); modalQuantity = viewChild.required('modalQuantity'); async ngOnInit() { await this.fetchQuotations(); } async fetchQuotations() { this.quotationsLoading.set(true) try { const quotations = await firstValueFrom(this.quotationsService.getAllQuotationEndpoint()) this.quotations.set(quotations); } catch (e) { this.notificationService.error( 'Erreur', 'Erreur de communication avec l\'API' ) } 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 (e) { 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 (e) { console.error(e); 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 (e) { console.error(e) this.notificationService.error('Erreur', 'Erreur lors de la modification') } } async deleteProduct(productId: number, quotationId: number) { this.quotationsLoading.set(true) try { await firstValueFrom(this.quotationProductsService.deleteQuotationProductEndpoint(productId, quotationId)) this.notificationService.success( 'Success', 'Suppression effectuée' ) } catch (e) { 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', 'Erreur d\'écriture dans le formulaire' ) return; } try { const quantity = updateQuantityComponent.quantityForm.getRawValue(); await firstValueFrom(this.quotationProductsService.patchQuotationProductQuantityEndpoint(productId, quotationId, quantity)) this.notificationService.success('Success', 'Quantité modifiée') } catch (e) { 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(); modal.isVisible = false; 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(); modal.isVisible = false; await this.fetchQuotations(); } }