import { Component, OnInit, OnDestroy, ElementRef, ViewChild, Input } from "@angular/core";
import { Injectable } from "@angular/core";
import { AppSettings } from '../../app.settings';
import { HelperDataService } from '../../services/helper-data-service';
import { Observable} from "rxjs/Observable";
import { Http, Headers, RequestOptions} from '@angular/http';
import { NgRedux, select } from '@angular-redux/store';
import { IAppState } from '../../store/store';
import { Router, ActivatedRoute, Params} from '@angular/router';
import { BehaviorSubject, Subscription } from 'rxjs/Rx';
import { ILoginInfo } from '../../store/logininfo/logininfo.types';
import { Ng2SmartTableModule, LocalDataSource } from 'ng2-smart-table';
import {reportsSchema, TableColumn} from './reports-schema';
import { LOGININFO_INITIAL_STATE } from '../../store/logininfo/logininfo.initial-state';
import {csvCreator} from './csv-creator';
import {chartCreator} from './chart-creator';
import {
FormBuilder,
FormGroup,
FormControl,
FormArray,
Validators,
} from '@angular/forms';
import { API_ENDPOINT } from '../../app.settings';
@Component({
selector: 'report-no-capacity',
template: `
`
})
@Injectable() export default class ReportNoCapacity implements OnInit, OnDestroy {
public formGroup: FormGroup;
loginInfo$: BehaviorSubject;
loginInfoSub: Subscription;
private generalReport$: BehaviorSubject;
private generalReportSub: Subscription;
private apiEndPoint = API_ENDPOINT;
private minedu_userName: string;
private minedu_userPassword: string;
private distStatus = "READY";
private data;
private validCreator: number;
private reportId: number;
private routerSub: any;
private enableCapacityFilter: boolean;
private source: LocalDataSource;
columnMap: Map = new Map();
@Input() settings: any;
private reportSchema = new reportsSchema();
private csvObj = new csvCreator();
private chartObj = new chartCreator();
@ViewChild('chart') public chartContainer: ElementRef;
private d3data: Array;
constructor(private fb: FormBuilder,
private _ngRedux: NgRedux,
private _hds: HelperDataService,
private activatedRoute: ActivatedRoute,
private router: Router) {
this.formGroup = this.fb.group({
capacityEnabled: ['', []],
});
this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
this.generalReport$ = new BehaviorSubject([{}]);
this.minedu_userName = '';
this.validCreator = -1;
this.enableCapacityFilter = false;
}
ngOnInit() {
this.loginInfoSub = this._ngRedux.select(state => {
if (state.loginInfo.size > 0) {
state.loginInfo.reduce(({}, loginInfoToken) => {
this.minedu_userName = loginInfoToken.minedu_username;
this.minedu_userPassword = loginInfoToken.minedu_userpassword;
return loginInfoToken;
}, {});
}
return state.loginInfo;
}).subscribe(this.loginInfo$);
this.routerSub = this.activatedRoute.params.subscribe(params => {
this.reportId = +params['reportId'];
});
}
ngOnDestroy() {
if (this.loginInfoSub)
this.loginInfoSub.unsubscribe();
if (this.generalReportSub)
this.generalReportSub.unsubscribe();
if (this.loginInfo$)
this.loginInfo$.unsubscribe();
if (this.generalReport$)
this.generalReport$.unsubscribe();
}
createReport() {
this.validCreator = 0;
let route;
if (this.reportId === 4) {
route = "/ministry/report-no-capacity/";
this.settings = this.reportSchema.reportNoCapacity;
}
else {
return;
}
this.generalReportSub = this._hds.makeReport(this.minedu_userName, this.minedu_userPassword, route, this.enableCapacityFilter, 0, 0, 0, 0, 0, 0).subscribe(data => {
this.generalReport$.next(data);
this.data = data;
this.validCreator = 1;
this.source = new LocalDataSource(this.data);
this.columnMap = new Map();
//pass parametes to csv class object
this.csvObj.columnMap = this.columnMap;
this.csvObj.source = this.source;
this.csvObj.settings = this.settings;
this.csvObj.prepareColumnMap();
},
error => {
this.generalReport$.next([{}]);
this.validCreator = -1;
console.log("Error Getting ReportNoCapacity");
});
}
toggleCapacityFilter() {
this.enableCapacityFilter = !this.enableCapacityFilter;
}
navigateBack() {
this.router.navigate(['/ministry/minister-reports']);
}
onSearch(query: string = '') {
this.csvObj.onSearch(query);
}
export2Csv() {
this.csvObj.export2Csv();
}
}