import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core'; import { FormControl, FormGroup } from '@angular/forms'; import { MatDialog } from '@angular/material/dialog'; import { ActivatedRoute, Router } from '@angular/router'; import * as moment from 'moment'; import { CostCentre } from '../../core/cost-centre'; import { ToasterService } from '../../core/toaster.service'; import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component'; import { Employee } from '../employee'; import { EmployeeService } from '../employee.service'; @Component({ selector: 'app-employee-detail', templateUrl: './employee-detail.component.html', styleUrls: ['./employee-detail.component.css'], }) export class EmployeeDetailComponent implements OnInit, AfterViewInit { @ViewChild('nameElement', { static: true }) nameElement?: ElementRef; form: FormGroup<{ code: FormControl; name: FormControl; designation: FormControl; salary: FormControl; points: FormControl; isActive: FormControl; costCentre: FormControl; joiningDate: FormControl; leavingDate: FormControl; }>; costCentres: CostCentre[] = []; item: Employee = new Employee(); constructor( private route: ActivatedRoute, private router: Router, private dialog: MatDialog, private toaster: ToasterService, private ser: EmployeeService, ) { this.form = new FormGroup({ code: new FormControl({ value: 0, disabled: true }, { nonNullable: true }), name: new FormControl(null), designation: new FormControl(null), salary: new FormControl(null), points: new FormControl(null), isActive: new FormControl(true, { nonNullable: true }), costCentre: new FormControl(null), joiningDate: new FormControl(new Date(), { nonNullable: true }), leavingDate: new FormControl(null), }); // Listen to IsActive Changes this.form.controls.isActive.valueChanges.subscribe((x) => { this.item.isActive = x; }); } ngOnInit() { this.route.data.subscribe((value) => { const data = value as { item: Employee; costCentres: CostCentre[] }; this.costCentres = data.costCentres; this.showItem(data.item); }); } showItem(item: Employee) { this.item = item; this.form.setValue({ code: this.item.code || '(Auto)', name: this.item.name || '', designation: this.item.designation || '', salary: this.item.salary || '', points: this.item.points || '', isActive: this.item.isActive, costCentre: this.item.costCentre.id ?? '', joiningDate: this.item.joiningDate ? moment(this.item.joiningDate, 'DD-MMM-YYYY').toDate() : new Date(), leavingDate: this.item.isActive ? null : moment(this.item.leavingDate, 'DD-MMM-YYYY').toDate(), }); } ngAfterViewInit() { setTimeout(() => { if (this.nameElement) { this.nameElement.nativeElement.focus(); } }, 0); } save() { this.ser.saveOrUpdate(this.getItem()).subscribe( () => { this.toaster.show('Success', ''); this.router.navigateByUrl('/employees'); }, (error) => { this.toaster.show('Danger', error); }, ); } delete() { this.ser.delete(this.item.id as string).subscribe( () => { this.toaster.show('Success', ''); this.router.navigateByUrl('/employees'); }, (error) => { this.toaster.show('Danger', error); }, ); } confirmDelete(): void { const dialogRef = this.dialog.open(ConfirmDialogComponent, { width: '250px', data: { title: 'Delete Employee?', content: 'Are you sure? This cannot be undone.' }, }); dialogRef.afterClosed().subscribe((result: boolean) => { if (result) { this.delete(); } }); } getItem(): Employee { const formValue = this.form.value; this.item.name = formValue.name ?? ''; this.item.designation = formValue.designation ?? ''; this.item.salary = Number(formValue.salary); this.item.points = Number(formValue.points); this.item.isActive = formValue.isActive ?? true; this.item.costCentre.id = formValue.costCentre ?? ''; this.item.joiningDate = moment(formValue.joiningDate).format('DD-MMM-YYYY'); this.item.leavingDate = this.item.isActive ? null : moment(formValue.leavingDate).format('DD-MMM-YYYY'); return this.item; } }