import { CurrencyPipe } from '@angular/common'; import { Component, OnInit, ViewChild } from '@angular/core'; import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms'; import { MatButton } from '@angular/material/button'; import { MatCard, MatCardHeader, MatCardTitle, MatCardContent } from '@angular/material/card'; import { MatDatepickerInput, MatDatepickerToggle, MatDatepicker } from '@angular/material/datepicker'; import { MatFormField, MatLabel, MatSuffix } from '@angular/material/form-field'; import { MatInput } from '@angular/material/input'; import { MatPaginator } from '@angular/material/paginator'; import { MatSort, MatSortHeader } from '@angular/material/sort'; import { MatTable, MatColumnDef, MatHeaderCellDef, MatHeaderCell, MatCellDef, MatCell, MatFooterCellDef, MatFooterCell, MatHeaderRowDef, MatHeaderRow, MatRowDef, MatRow, MatFooterRowDef, MatFooterRow, } from '@angular/material/table'; import { ActivatedRoute, Router } from '@angular/router'; import moment from 'moment'; import { ProfitLoss } from './profit-loss'; import { ProfitLossDataSource } from './profit-loss-datasource'; @Component({ selector: 'app-profit-loss', templateUrl: './profit-loss.component.html', styleUrls: ['./profit-loss.component.css'], standalone: true, imports: [ MatCard, MatCardHeader, MatCardTitle, MatCardContent, ReactiveFormsModule, MatFormField, MatLabel, MatInput, MatDatepickerInput, MatDatepickerToggle, MatSuffix, MatDatepicker, MatButton, MatTable, MatSort, MatColumnDef, MatHeaderCellDef, MatHeaderCell, MatSortHeader, MatCellDef, MatCell, MatFooterCellDef, MatFooterCell, MatHeaderRowDef, MatHeaderRow, MatRowDef, MatRow, MatFooterRowDef, MatFooterRow, MatPaginator, CurrencyPipe, ], }) export class ProfitLossComponent implements OnInit { @ViewChild(MatPaginator, { static: true }) paginator?: MatPaginator; @ViewChild(MatSort, { static: true }) sort?: MatSort; info: ProfitLoss = new ProfitLoss(); dataSource: ProfitLossDataSource = new ProfitLossDataSource(this.info.body); form: FormGroup<{ startDate: FormControl; finishDate: FormControl; }>; selectedRowId = ''; /** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */ displayedColumns = ['group', 'name', 'amount', 'total']; constructor( private route: ActivatedRoute, private router: Router, ) { this.form = new FormGroup({ startDate: new FormControl(new Date(), { nonNullable: true }), finishDate: new FormControl(new Date(), { nonNullable: true }), }); } ngOnInit() { this.route.data.subscribe((value) => { const data = value as { info: ProfitLoss }; this.info = data.info; this.form.setValue({ startDate: moment(this.info.startDate, 'DD-MMM-YYYY').toDate(), finishDate: moment(this.info.finishDate, 'DD-MMM-YYYY').toDate(), }); this.dataSource = new ProfitLossDataSource(this.info.body, this.paginator, this.sort); }); } show() { const l = this.prepareSubmit(); this.router.navigate(['profit-loss'], { queryParams: { startDate: l.startDate, finishDate: l.finishDate, }, }); } prepareSubmit(): ProfitLoss { const formModel = this.form.value; return new ProfitLoss({ startDate: moment(formModel.startDate).format('DD-MMM-YYYY'), finishDate: moment(formModel.finishDate).format('DD-MMM-YYYY'), }); } }