import { AfterViewInit, Component, ElementRef, OnInit, ViewChild, inject } from '@angular/core'; import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms'; import { MatButtonModule } from '@angular/material/button'; import { MatOptionModule } from '@angular/material/core'; import { MatDialog } from '@angular/material/dialog'; import { MatFormFieldModule } from '@angular/material/form-field'; import { MatInputModule } from '@angular/material/input'; import { MatSelectModule } from '@angular/material/select'; import { MatSnackBar } from '@angular/material/snack-bar'; import { ActivatedRoute, Router } from '@angular/router'; import { round } from 'mathjs'; import { Regime } from '../../core/regime'; import { Tax } from '../../core/tax'; import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component'; import { TaxService } from '../tax.service'; @Component({ selector: 'app-tax-detail', templateUrl: './tax-detail.component.html', styleUrls: ['./tax-detail.component.css'], imports: [MatButtonModule, MatFormFieldModule, MatInputModule, MatOptionModule, MatSelectModule, ReactiveFormsModule], }) export class TaxDetailComponent implements OnInit, AfterViewInit { private route = inject(ActivatedRoute); private router = inject(Router); private dialog = inject(MatDialog); private snackBar = inject(MatSnackBar); private ser = inject(TaxService); @ViewChild('nameElement', { static: true }) nameElement?: ElementRef; form: FormGroup<{ name: FormControl; rate: FormControl; regime: FormControl; }>; item: Tax = new Tax(); regimes: Regime[] = []; constructor() { // Create form this.form = new FormGroup({ name: new FormControl('', { nonNullable: true }), rate: new FormControl(100, { nonNullable: true }), regime: new FormControl(0, { nonNullable: true }), }); } ngOnInit() { this.route.data.subscribe((value) => { const data = value as { item: Tax; regimes: Regime[] }; this.regimes = data.regimes; this.showItem(data.item); }); } showItem(item: Tax) { this.item = item; this.form.setValue({ name: this.item.name ?? '', rate: this.item.rate * 100, regime: this.item.regime?.id ?? 0, }); } ngAfterViewInit() { setTimeout(() => { if (this.nameElement !== undefined) { this.nameElement.nativeElement.focus(); } }, 0); } save() { this.ser.saveOrUpdate(this.getItem()).subscribe({ next: () => { this.snackBar.open('', 'Success'); this.router.navigateByUrl('/taxes'); }, error: (error) => { this.snackBar.open(error, 'Error'); }, }); } delete() { this.ser.delete(this.item.id as string).subscribe({ next: () => { this.snackBar.open('', 'Success'); this.router.navigateByUrl('/taxes'); }, error: (error) => { this.snackBar.open(error, 'Error'); }, }); } confirmDelete(): void { const dialogRef = this.dialog.open(ConfirmDialogComponent, { width: '250px', data: { title: 'Delete Tax?', content: 'Are you sure? This cannot be undone.' }, }); dialogRef.afterClosed().subscribe((result: boolean) => { if (result) { this.delete(); } }); } getItem(): Tax { const formModel = this.form.value; this.item.name = formModel.name ?? ''; this.item.rate = round((formModel.rate ?? 0) / 100, 5); if (this.item.regime === null || this.item.regime === undefined) { this.item.regime = new Regime(); } this.item.regime.id = formModel.regime; return this.item; } }