import { Component, OnInit, ViewChild, ElementRef } from '@angular/core'; import { FormControl, FormGroup } from '@angular/forms'; import { MatPaginator } from '@angular/material/paginator'; import { MatSort } from '@angular/material/sort'; import { ActivatedRoute, Router } from '@angular/router'; import * as moment from 'moment'; import { Observable } from 'rxjs'; import { debounceTime, distinctUntilChanged } from 'rxjs/operators'; import { ToasterService } from '../core/toaster.service'; import { ToCsvService } from '../shared/to-csv.service'; import { MenuEngineeringReport } from './menu-engineering-report'; import { MenuEngineeringReportDataSource } from './menu-engineering-report-datasource'; import { MenuEngineeringReportService } from './menu-engineering-report.service'; @Component({ selector: 'app-menu-engineering-report', templateUrl: './menu-engineering-report.component.html', styleUrls: ['./menu-engineering-report.component.css'], }) export class MenuEngineeringReportComponent implements OnInit { @ViewChild('filterElement', { static: true }) filterElement?: ElementRef; @ViewChild(MatPaginator, { static: true }) paginator?: MatPaginator; @ViewChild(MatSort, { static: true }) sort?: MatSort; info: MenuEngineeringReport = new MenuEngineeringReport(); filter: Observable; dataSource: MenuEngineeringReportDataSource; form: FormGroup<{ startDate: FormControl; finishDate: FormControl; filter: FormControl; }>; /** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */ displayedColumns = ['name', 'price', 'saleCategory', 'menuCategory', 'quantity', 'amount']; constructor( private route: ActivatedRoute, private router: Router, private toCsv: ToCsvService, private toaster: ToasterService, private ser: MenuEngineeringReportService, ) { // Create form this.form = new FormGroup({ startDate: new FormControl(new Date(), { nonNullable: true }), finishDate: new FormControl(new Date(), { nonNullable: true }), filter: new FormControl('', { nonNullable: true }), }); // Listen to Filter Change this.filter = this.form.controls.filter.valueChanges.pipe(debounceTime(150), distinctUntilChanged()); this.dataSource = new MenuEngineeringReportDataSource(this.info.amounts, this.filter); } ngOnInit() { this.route.data.subscribe((value) => { const data = value as { info: MenuEngineeringReport }; 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(), filter: '', }); this.dataSource = new MenuEngineeringReportDataSource(this.info.amounts, this.filter, this.paginator, this.sort); }); } show() { const info = this.getInfo(); this.router.navigate(['menu-engineering-report'], { queryParams: { startDate: info.startDate, finishDate: info.finishDate, }, }); } getInfo(): MenuEngineeringReport { const formModel = this.form.value; return new MenuEngineeringReport({ startDate: moment(formModel.startDate).format('DD-MMM-YYYY'), finishDate: moment(formModel.finishDate).format('DD-MMM-YYYY'), }); } print() { this.ser.print(this.info.startDate, this.info.finishDate).subscribe( () => { this.toaster.show('', 'Successfully Printed'); }, (error) => { this.toaster.show('Error', error); }, ); } exportCsv() { const headers = { Name: 'name', Price: 'price', Average: 'average', 'Sale Category': 'saleCategory', 'Menu Category': 'menuCategory', Quantity: 'quantity', Amount: 'amount', }; const csvData = new Blob([this.toCsv.toCsv(headers, this.dataSource.data)], { type: 'text/csv;charset=utf-8;', }); const link = document.createElement('a'); link.href = window.URL.createObjectURL(csvData); link.setAttribute('download', 'menu-engineering-report.csv'); document.body.appendChild(link); link.click(); document.body.removeChild(link); } }