import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core'; import { FormBuilder, FormControl, FormGroup } from '@angular/forms'; import { MatDialog } from '@angular/material/dialog'; import { ActivatedRoute, Router } from '@angular/router'; import * as moment from 'moment'; import { BehaviorSubject, Observable } from 'rxjs'; import { distinctUntilChanged, startWith, switchMap } from 'rxjs/operators'; import { Act } from '../../core/act'; import { Case } from '../../core/case'; import { CaseSource } from '../../core/case-source'; import { CaseType } from '../../core/case-type'; import { Court } from '../../core/court'; import { CourtStatus } from '../../core/court-status'; import { Department } from '../../core/department'; import { Hearing } from '../../core/hearing'; import { Nature } from '../../core/nature'; import { Office } from '../../core/office'; import { OfficeStatus } from '../../core/office-status'; import { ToasterService } from '../../core/toaster.service'; import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component'; import { CaseService } from '../case.service'; import { HearingDatasource } from './case-detail-datasource'; import { HearingDialogComponent } from './hearing-dialog.component'; @Component({ selector: 'app-case-detail', templateUrl: './case-detail.component.html', styleUrls: ['./case-detail.component.css'], }) export class CaseDetailComponent implements OnInit, AfterViewInit { @ViewChild('nameElement', { static: true }) nameElement?: ElementRef; public hearingsObservable = new BehaviorSubject([]); dataSource: HearingDatasource = new HearingDatasource(this.hearingsObservable); form: FormGroup; caseSources: CaseSource[] = []; caseTypes: CaseType[] = []; courts: Court[] = []; departments: Department[] = []; offices: Observable = new Observable(); courtStatuses: CourtStatus[] = []; officeStatuses: OfficeStatus[] = []; acts: Act[] = []; natures: Nature[] = []; item: Case = new Case(); displayedColumns = [ 'itemNumber', 'bench', 'courtStatus', 'proceedings', 'complianceDate', 'nextHearingDate', 'action', ]; constructor( private route: ActivatedRoute, private router: Router, private dialog: MatDialog, private fb: FormBuilder, private toaster: ToasterService, private ser: CaseService, ) { // Create form this.form = this.fb.group({ caseSource: '', officeFileNumber: '', courtCaseNumber: '', year: '', title: '', docketNumber: '', receiptDate: '', limitationDate: '', filingDate: '', appearOnBehalfOf: '', respondentNo: '', questionOfLaw: '', aorName: '', opposingCouncilAor: '', opposingCouncilDetail: '', lowerCourtCaseNumber: '', dateOfImpugnedJudgement: '', briefDescription: '', remarks: '', slpCounter: '', contactDetail: '', caseConnectedWith: '', bunchCases: '', court: '', department: '', office: '', caseType: '', act: '', nature: '', officeStatus: '', courtStatus: '', addRow: this.fb.group({ courtNumber: '', itemNumber: '', bench: '', latestStatus: '', proceedings: '', complianceDate: '', complianceDone: '', nextHearingDate: '', }), }); } ngOnInit() { this.route.data.subscribe((value) => { const data = value as { item: Case; caseSources: CaseSource[]; caseTypes: CaseType[]; courts: Court[]; departments: Department[]; courtStatuses: CourtStatus[]; officeStatuses: OfficeStatus[]; acts: Act[]; natures: Nature[]; }; this.caseSources = data.caseSources; this.caseTypes = data.caseTypes; this.courts = data.courts; this.departments = data.departments; this.courtStatuses = data.courtStatuses; this.officeStatuses = data.officeStatuses; this.acts = data.acts; this.natures = data.natures; this.showItem(data.item); }); } showItem(item: Case) { this.item = item; this.form.patchValue({ caseSource: this.item.caseSource.id, officeFileNumber: this.item.officeFileNumber, courtCaseNumber: this.item.courtCaseNumber, year: this.item.year, title: this.item.title, docketNumber: this.item.docketNumber, receiptDate: this.item.receiptDate ? moment(this.item.receiptDate, 'DD-MMM-YYYY').toDate() : '', limitationDate: this.item.limitationDate ? moment(this.item.limitationDate, 'DD-MMM-YYYY').toDate() : '', filingDate: this.item.filingDate ? moment(this.item.filingDate, 'DD-MMM-YYYY').toDate() : '', appearOnBehalfOf: this.item.appearOnBehalfOf === 'Petitioner' ? 'Petitioner' : 'Respondent', respondentNo: this.item.appearOnBehalfOf === 'Petitioner' || this.item.appearOnBehalfOf === 'Respondent' ? '' : this.item.appearOnBehalfOf, questionOfLaw: this.item.questionOfLaw, aorName: this.item.aorName, opposingCouncilAor: this.item.opposingCouncilAor, opposingCouncilDetail: this.item.opposingCouncilDetail, lowerCourtCaseNumber: this.item.lowerCourtCaseNumber, dateOfImpugnedJudgement: this.item.dateOfImpugnedJudgement ? moment(this.item.dateOfImpugnedJudgement, 'DD-MMM-YYYY').toDate() : '', briefDescription: this.item.briefDescription, remarks: this.item.remarks, slpCounter: this.item.slpCounter, contactDetail: this.item.contactDetail, caseConnectedWith: this.item.caseConnectedWith, bunchCases: this.item.bunchCases, court: this.item.court ? this.item.court.id : '', department: this.item.department ? this.item.department.id : '', office: this.item.office ? this.item.office.id : '', caseType: this.item.caseType ? this.item.caseType.id : '', act: this.item.act ? this.item.act.id : '', nature: this.item.nature ? this.item.nature.id : '', officeStatus: this.item.officeStatus ? this.item.officeStatus.id : '', courtStatus: this.item.courtStatus ? this.item.courtStatus.id : '', }); (this.form.get('caseSource') as FormControl).valueChanges .pipe( startWith(this.item.caseSource.id), distinctUntilChanged(), switchMap((x) => this.ser.newOfficeFileNumber(x)), ) .subscribe((x) => { if ( this.item.officeFileNumber === null || this.item.officeFileNumber === undefined || this.item.officeFileNumber === '' ) { this.form.patchValue({ officeFileNumber: x }); } }); this.hearingsObservable.next(this.item.hearings); } ngAfterViewInit() { setTimeout(() => { if (this.nameElement !== undefined) { this.nameElement.nativeElement.focus(); } }, 0); } save() { this.ser.saveOrUpdate(this.getItem()).subscribe( () => { this.toaster.show('Success', ''); this.router.navigateByUrl('/cases'); }, (error) => { this.toaster.show('Error', error); }, ); } delete() { this.ser.delete(this.item.id as string).subscribe( () => { this.toaster.show('Success', ''); this.router.navigateByUrl('/cases'); }, (error) => { this.toaster.show('Error', error); }, ); } confirmDelete(): void { const dialogRef = this.dialog.open(ConfirmDialogComponent, { width: '250px', data: { title: 'Delete Case?', content: 'Are you sure? This cannot be undone.' }, }); dialogRef.afterClosed().subscribe((result: boolean) => { if (result) { this.delete(); } }); } getItem(): Case { const formModel = this.form.value; this.item.caseSource.id = formModel.caseSource; this.item.officeFileNumber = formModel.officeFileNumber; this.item.courtCaseNumber = formModel.courtCaseNumber; this.item.year = formModel.year; this.item.title = formModel.title; this.item.docketNumber = formModel.docketNumber; if (formModel.receiptDate === '') { this.item.receiptDate = null; } else { this.item.receiptDate = moment(formModel.receiptDate).format('DD-MMM-YYYY'); } if (formModel.limitationDate === '') { this.item.limitationDate = null; } else { this.item.limitationDate = moment(formModel.limitationDate).format('DD-MMM-YYYY'); } if (formModel.filingDate === '') { this.item.filingDate = null; } else { this.item.filingDate = moment(formModel.filingDate).format('DD-MMM-YYYY'); } this.item.appearOnBehalfOf = formModel.appearOnBehalfOf === 'Petitioner' ? 'Petitioner' : formModel.respondentNo || 'Respondent'; this.item.questionOfLaw = formModel.questionOfLaw; this.item.aorName = formModel.aorName; this.item.opposingCouncilAor = formModel.opposingCouncilAor; this.item.opposingCouncilDetail = formModel.opposingCouncilDetail; this.item.lowerCourtCaseNumber = formModel.lowerCourtCaseNumber; if (formModel.dateOfImpugnedJudgement === '') { this.item.dateOfImpugnedJudgement = null; } else { this.item.dateOfImpugnedJudgement = moment(formModel.dateOfImpugnedJudgement).format( 'DD-MMM-YYYY', ); } this.item.briefDescription = formModel.briefDescription; this.item.remarks = formModel.remarks; this.item.slpCounter = formModel.slpCounter; this.item.contactDetail = formModel.contactDetail; this.item.caseConnectedWith = formModel.caseConnectedWith; this.item.bunchCases = formModel.bunchCases; if (formModel.court === undefined || formModel.court === '') { this.item.court = undefined; } else { if (this.item.court === null || this.item.court === undefined) { this.item.court = new Court(); } this.item.court.id = formModel.court; } if (formModel.department === undefined || formModel.department === '') { this.item.department = undefined; } else { if (this.item.department === null || this.item.department === undefined) { this.item.department = new Department(); } this.item.department.id = formModel.department; } if (formModel.office === undefined || formModel.office === '') { this.item.office = undefined; } else { if (this.item.office === null || this.item.office === undefined) { this.item.office = new Office(); } this.item.office.id = formModel.office; } if (formModel.caseType === undefined || formModel.caseType === '') { this.item.caseType = undefined; } else { if (this.item.caseType === null || this.item.caseType === undefined) { this.item.caseType = new CaseType(); } this.item.caseType.id = formModel.caseType; } if (formModel.act === undefined || formModel.act === '') { this.item.act = undefined; } else { if (this.item.act === null || this.item.act === undefined) { this.item.act = new Act(); } this.item.act.id = formModel.act; } if (formModel.nature === undefined || formModel.nature === '') { this.item.nature = undefined; } else { if (this.item.nature === null || this.item.nature === undefined) { this.item.nature = new Nature(); } this.item.nature.id = formModel.nature; } if (formModel.officeStatus === undefined || formModel.officeStatus === '') { this.item.officeStatus = undefined; } else { if (this.item.officeStatus === null || this.item.officeStatus === undefined) { this.item.officeStatus = new OfficeStatus(); } this.item.officeStatus.id = formModel.officeStatus; } if (formModel.courtStatus === undefined || formModel.courtStatus === '') { this.item.courtStatus = undefined; } else { if (this.item.courtStatus === null || this.item.courtStatus === undefined) { this.item.courtStatus = new CourtStatus(); } this.item.courtStatus.id = formModel.courtStatus; } return this.item; } addHearing() { const formValue = (this.form.get('addRow') as FormControl).value; const courtNumber: string = formValue.courtNumber; const itemNumber: string = formValue.itemNumber; const bench: string = formValue.bench; const latestStatus = new CourtStatus({ id: formValue.latestStatus, name: (this.courtStatuses.find((x) => x.id === formValue.latestStatus) as CourtStatus).name, }); const proceedings = formValue.proceedings; const complianceDate = formValue.complianceDate ? moment(formValue.complianceDate).format('DD-MMM-YYYY') : null; const complianceDone = !!formValue.complianceDone; const nextHearingDate = formValue.nextHearingDate ? moment(formValue.nextHearingDate).format('DD-MMM-YYYY') : null; this.item.hearings.push( new Hearing({ courtNumber, itemNumber, bench, courtStatus: latestStatus, proceedings, complianceDate, complianceDone, nextHearingDate, }), ); this.hearingsObservable.next(this.item.hearings); this.resetAddRow(); } resetAddRow() { (this.form.get('addRow') as FormControl).reset({ courtNumber: '', itemNumber: '', bench: '', courtStatus: '', proceedings: '', complianceDate: '', complianceDone: '', nextHearingDate: '', }); } editRow(row: Hearing) { const dialogRef = this.dialog.open(HearingDialogComponent, { width: '750px', data: { hearing: { ...row }, courtStatuses: this.courtStatuses }, }); dialogRef.afterClosed().subscribe((result: boolean | Hearing) => { if (!result) { return; } const h = result as Hearing; Object.assign(row, h); this.hearingsObservable.next(this.item.hearings); this.resetAddRow(); }); } deleteRow(row: Hearing) { this.item.hearings.splice(this.item.hearings.indexOf(row), 1); this.hearingsObservable.next(this.item.hearings); } }