report-general.ts 6.56 KB
Newer Older
1
import { NgRedux } from "@angular-redux/store";
2
import { Injectable } from "@angular/core";
3 4 5
import { Component, ElementRef, Input, OnDestroy, OnInit, ViewChild } from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";
import { LocalDataSource } from "ng2-smart-table";
6
import { BehaviorSubject, Subscription } from "rxjs/Rx";
7

8
import { API_ENDPOINT } from "../../app.settings";
9 10 11 12 13 14 15
import { HelperDataService } from "../../services/helper-data-service";
import { LOGININFO_INITIAL_STATE } from "../../store/logininfo/logininfo.initial-state";
import { ILoginInfoRecords } from "../../store/logininfo/logininfo.types";
import { IAppState } from "../../store/store";
import { ChartCreator } from "./chart-creator";
import { CsvCreator } from "./csv-creator";
import { ReportsSchema, TableColumn } from "./reports-schema";
16 17

@Component({
18
    selector: "report-general",
19 20
    template: `

21 22
    <div class="reports-container">
        <div class = "loading" *ngIf="validCreator == 0" ></div>
23

24 25
        <h5>Κατανομή Μαθητών με Βάση τη Σειρά Προτίμησης</h5>
        <h6>Επιλογή Φίλτρων: Δεν υπάρχουν διαθέσιμα φίλτρα</h6>
26

27 28
        <button type="submit" class="btn btn-alert"  (click)="createReport()" [hidden]="minedu_userName == ''" ><i class="fa fa-file-text"></i> Δημιουργία Αναφοράς</button>
        <button type="submit" class="btn btn-alert pull-right"  (click)="navigateBack()" [hidden]="minedu_userName == ''" > Επιστροφή</button>
29 30

        <div *ngIf="validCreator == 1 ">
31
          <input #search class="search" type="text" placeholder="Αναζήτηση..." (keydown.enter)="onSearch(search.value)">
32
          <div class="smart-table-container table table-hover table-striped" reportScroll>
33 34 35 36
            <ng2-smart-table [settings]="settings" [source]="source"></ng2-smart-table>
          </div>
        </div>

37 38 39
        <button type="button" class="alert alert-info pull-right" (click)="export2Csv()" [hidden]="validCreator != 1"><i class="fa fa-download"></i> Εξαγωγή σε csv</button>
        <button type="button" class="alert alert-info pull-left" (click)="createDiagram()" [hidden]="validCreator != 1 "><i class="fa fa-bar-chart"></i> Διάγραμμα</button>
        <div class="d3-chart" *ngIf = "validCreator == 1" #chart></div>
40 41 42 43 44 45
    </div>
   `
})

@Injectable() export default class ReportGeneral implements OnInit, OnDestroy {

46
    private loginInfo$: BehaviorSubject<ILoginInfoRecords>;
47
    private loginInfoSub: Subscription;
48 49 50 51 52 53
    private generalReport$: BehaviorSubject<any>;
    private generalReportSub: Subscription;
    private apiEndPoint = API_ENDPOINT;
    private minedu_userName: string;
    private minedu_userPassword: string;
    private distStatus = "READY";
54
    private data: any;
55 56 57
    private validCreator: number;
    private createGraph: boolean;

58
    private source: LocalDataSource;
59
    columnMap: Map<string, TableColumn> = new Map<string, TableColumn>();
60
    @Input() settings: any;
61 62
    private reportSchema = new ReportsSchema();
    private csvObj = new CsvCreator();
63

64
    private chartObj = new ChartCreator();
65
    @ViewChild("chart") public chartContainer: ElementRef;
66
    private d3data: Array<any>;
67

68
    constructor(
69 70 71
        private _ngRedux: NgRedux<IAppState>,
        private _hds: HelperDataService,
        private activatedRoute: ActivatedRoute,
72 73
        private router: Router
    ) {
74 75
        this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
        this.generalReport$ = new BehaviorSubject([{}]);
76
        this.minedu_userName = "";
77 78
        this.validCreator = -1;
        this.createGraph = false;
79 80 81
    }

    ngOnInit() {
82
        this.loginInfoSub = this._ngRedux.select("loginInfo")
83
            .map(loginInfo => <ILoginInfoRecords>loginInfo)
84 85
            .subscribe(loginInfo => {
                if (loginInfo.size > 0) {
86 87 88 89
                    loginInfo.reduce(({ }, loginInfoObj) => {
                        this.minedu_userName = loginInfoObj.minedu_username;
                        this.minedu_userPassword = loginInfoObj.minedu_userpassword;
                        return loginInfoObj;
90 91 92 93
                    }, {});
                }
                this.loginInfo$.next(loginInfo);
            }, error => console.log("error selecting loginInfo"));
94 95 96
    }

    ngOnDestroy() {
97 98 99 100
        if (this.loginInfoSub)
            this.loginInfoSub.unsubscribe();
        if (this.generalReportSub)
            this.generalReportSub.unsubscribe();
101 102
    }

103
    createReport() {
104 105 106
        this.validCreator = 0;
        this.createGraph = false;

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
        let route = "/ministry/general-report/";
        this.settings = this.reportSchema.genReportSchema;

        this.generalReportSub = this._hds.makeReport(this.minedu_userName, this.minedu_userPassword, route, 0, 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<string, TableColumn>();

                this.csvObj.columnMap = this.columnMap;
                this.csvObj.source = this.source;
                this.csvObj.settings = this.settings;
                this.csvObj.prepareColumnMap();
            },
123 124 125 126 127 128
            error => {
                this.generalReport$.next([{}]);
                this.validCreator = -1;
                console.log("Error Getting generalReport");
            });
    }
129

130
    navigateBack() {
131
        this.router.navigate(["/ministry/minister-reports"]);
132
    }
133

134
    onSearch(query: string = "") {
135 136
        this.csvObj.onSearch(query);
    }
137

138 139 140
    export2Csv() {
        this.csvObj.export2Csv();
    }
141 142


143 144 145 146 147 148 149 150 151 152
    createDiagram() {
        if (!this.createGraph) {
            this.generateGraphData();
            this.chartObj.d3data = this.d3data;
            this.chartObj.chartContainer = this.chartContainer;
            this.chartObj.createChart();
            this.chartObj.updateChart();
            this.createGraph = true;
        }
    }
153

154 155 156
    generateGraphData() {
        this.d3data = [];

157 158 159 160 161 162 163 164 165 166 167
        let labelsX = [];
        labelsX.push("1η Προτίμηση");
        labelsX.push("2η Προτίμηση");
        labelsX.push("3η Προτίμηση");
        labelsX.push("Μη τοποθετημένοι");
        labelsX.push("Προσωρινά τοποθετημένοι σε ολιγομελή");
        for (let i = 1; i <= 5; i++) {
            this.d3data.push([
                labelsX[i - 1],
                this.data[i].numStudents / this.data[0].numStudents,
            ]);
168 169
        }
    }
170 171

}