import { AfterViewInit, Component, OnInit, ViewChild, Input, SimpleChange } from '@angular/core'; import { MatPaginator } from '@angular/material/paginator'; import { MatSort } from '@angular/material/sort'; import { MatTable, MatTableDataSource } from '@angular/material/table'; import { AllReportsDataSource, AllReportsItem } from './report-list-datasource'; import { HttpClient } from '@angular/common/http'; import { Router } from '@angular/router'; import { environment } from '../../../../environments/environment'; import { ReportListService } from './report-list.service'; @Component( { selector: 'app-all-reports', templateUrl: './report-list.component.html', styleUrls: ['./report-list.component.css'] } ) export class ReportListComponent implements AfterViewInit, OnInit { @ViewChild( MatPaginator, { static: false } as any ) paginator: MatPaginator; @ViewChild( MatSort, { static: false } as any ) sort: MatSort; @ViewChild( MatTable, { static: false } as any ) table: MatTable; @Input( "reportId" ) reportId1: string; dataSource: AllReportsDataSource; dataSource1: any; finalGETObj: {}; finalGETObjRowsArr: [][]; rowArr: {}[]; reportIdArr: string[]; toggle: boolean; intermediateDisplayedColumns: string[]; displayedColumns = ["rep_id", "rep_name", "descr", "owner", "create_date", "copy", "edit", "delete", "schedule", "run"]; finalRowArr: AllReportsItem[]; rowObj: any; reportId: string; toggle1: boolean; showSpinner: boolean; showDialog: boolean; closable: boolean; delete: boolean; newReportId: string; constructor( private _http: HttpClient, private _router: Router, private _reportListService: ReportListService ) { this.showDialog = false; this.closable = false; this.delete = false; this.initializeReportList(); } initializeReportList() { this.showSpinner = true; this.dataSource = new AllReportsDataSource(); this.dataSource1 = new MatTableDataSource(); this.intermediateDisplayedColumns = new Array(); this.finalGETObj = new Object(); this.finalGETObj = new Object(); this.finalGETObjRowsArr = new Array(); this.rowArr = new Array(); this.reportIdArr = new Array(); this.toggle = false; this.toggle1 = false; this.finalRowArr = new Array(); this._reportListService.getAllReports() .subscribe(( responseObj ) => { this.finalGETObj = responseObj; this.finalGETObjRowsArr = this.finalGETObj["rows"]; let j = 0; while ( this.finalGETObj["columns"][0][j] ) { if ( this.finalGETObj["columns"][0][j]["columnId"] !== "no" ) { this.intermediateDisplayedColumns.push( this.finalGETObj["columns"][0][j]["columnId"] ); } j++; } this.displayedColumns = this.intermediateDisplayedColumns; let i = 0; while ( this.finalGETObjRowsArr[0][i] ) { this.rowArr = this.finalGETObjRowsArr[0][i]; this.rowObj = new Object(); let j = 0; while ( this.rowArr[j] ) { if ( this.rowArr[j]["columnId"] === "rep_id" ) { this.rowObj["rep_id"] = this.rowArr[j]["searchresultField"]["displayValue"]; this.reportIdArr.push( this.rowArr[j]["searchresultField"]["displayValue"] ); } if ( this.rowArr[j]["columnId"] === "rep_name" ) { this.rowObj["rep_name"] = this.rowArr[j]["searchresultField"]["displayValue"]; } if ( this.rowArr[j]["columnId"] === "descr" ) { this.rowObj["descr"] = this.rowArr[j]["searchresultField"]["displayValue"]; } if ( this.rowArr[j]["columnId"] === "owner" ) { this.rowObj["owner"] = this.rowArr[j]["searchresultField"]["displayValue"]; } if ( this.rowArr[j]["columnId"] === "create_date" ) { this.rowObj["create_date"] = this.rowArr[j]["searchresultField"]["displayValue"]; } if ( this.rowArr[j]["columnId"] === "copy" ) { this.rowObj["copy"] = this.rowArr[j]["searchresultField"]["displayValue"]; } if ( this.rowArr[j]["columnId"] === "edit" ) { this.rowObj["edit"] = this.rowArr[j]["searchresultField"]["displayValue"]; this.rowObj["canEdit"] = this.rowArr[j]["searchresultField"]["authorized"]; } if ( this.rowArr[j]["columnId"] === "delete" ) { this.rowObj["delete"] = this.rowArr[j]["searchresultField"]["displayValue"]; this.rowObj["canDelete"] = this.rowArr[j]["searchresultField"]["authorized"]; } if ( this.rowArr[j]["columnId"] === "schedule" ) { this.rowObj["schedule"] = this.rowArr[j]["searchresultField"]["displayValue"]; } if ( this.rowArr[j]["columnId"] === "run" ) { this.rowObj["run"] = this.rowArr[j]["searchresultField"]["displayValue"]; } j++; } this.finalRowArr.push( this.rowObj ); i++; } this.showSpinner = false; if ( !this.showSpinner ) { this.dataSource.data = this.finalRowArr; this.dataSource1 = new MatTableDataSource( this.finalRowArr ); this.dataSource1.sort = this.sort; this.dataSource1.paginator = this.paginator; this.table.dataSource = this.dataSource; } } ); } ngOnInit() { sessionStorage.clear(); const myItem = localStorage.getItem('id'); localStorage.clear(); localStorage.setItem('id', myItem); this.toggle = false; } ngAfterViewInit() { this.dataSource.sort = this.sort; this.dataSource.paginator = this.paginator; this.table.dataSource = this.dataSource; } displayReport( reportId: string ) { this.reportId = reportId; this._router.navigate( ["v2/app/reports", "Edit", reportId] ); } runReport( reportId: string ) { this.reportId = reportId; this._router.navigate( ['v2/app/run', reportId] ); } applyFilter( filterValue: string ) { this.dataSource1.filter = filterValue.trim().toLowerCase(); } confirmDelete( reportId: string ) { this.showDialog = true; this.closable = true; this.newReportId = reportId; } deleteReport() { this._reportListService.deleteReport( this.newReportId ) .subscribe(( responseDelete ) => { this.initializeReportList(); this.showDialog = !this.showDialog; this.closable = false; } ); } close() { this.showDialog = !this.showDialog; this.closable = false; } openReportSchedule( reportId: string ) { this._router.navigate( ['v2/app/schedule_report', reportId] ); } copydisplayReport(reportId : string) { this.reportId = reportId; this._router.navigate(["v2/app/reports", "Copy",reportId]); } }