report-general.ts 6.69 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 21 22 23 24 25
    template: `

  <div>
        <div
          class = "loading" *ngIf="validCreator == 0" >
        </div>

26
        <form #form>
27 28 29 30

          <h5> >Επιλογή Φίλτρων <br><br></h5>
          <h6> Δεν υπάρχουν διαθέσιμα φίλτρα <br><br><br></h6>

31
          <button type="submit" class="btn btn-alert"  (click)="createReport()" [hidden]="minedu_userName == ''" >
32 33 34 35 36 37 38 39 40 41
          <i class="fa fa-file-text"></i>
              Δημιουργία Αναφοράς
          </button>
          <button type="submit" class="btn btn-alert pull-right"  (click)="navigateBack()" [hidden]="minedu_userName == ''" >
              Επιστροφή
          </button>
          <br><br>
        </form>

        <div *ngIf="validCreator == 1 ">
42
          <input #search class="search" type="text" placeholder="Αναζήτηση..." (keydown.enter)="onSearch(search.value)">
43 44 45 46 47
          <div class="smart-table-container" reportScroll>
            <ng2-smart-table [settings]="settings" [source]="source"></ng2-smart-table>
          </div>
        </div>

48
        <button type="button" class="alert alert-info pull-right" (click)="export2Csv()" [hidden]="validCreator != 1">
49 50 51 52 53 54 55 56 57 58
        <i class="fa fa-download"></i>
            <br>Εξαγωγή σε 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>
59
        <br><br><br><br><br>
60 61 62 63 64 65 66 67

    </div>

   `
})

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

68
    private loginInfo$: BehaviorSubject<ILoginInfoRecords>;
69
    private loginInfoSub: Subscription;
70 71 72 73 74 75
    private generalReport$: BehaviorSubject<any>;
    private generalReportSub: Subscription;
    private apiEndPoint = API_ENDPOINT;
    private minedu_userName: string;
    private minedu_userPassword: string;
    private distStatus = "READY";
76
    private data: any;
77 78 79
    private validCreator: number;
    private createGraph: boolean;

80
    private source: LocalDataSource;
81
    columnMap: Map<string, TableColumn> = new Map<string, TableColumn>();
82
    @Input() settings: any;
83 84
    private reportSchema = new ReportsSchema();
    private csvObj = new CsvCreator();
85

86
    private chartObj = new ChartCreator();
87
    @ViewChild("chart") public chartContainer: ElementRef;
88
    private d3data: Array<any>;
89

90
    constructor(
91 92 93
        private _ngRedux: NgRedux<IAppState>,
        private _hds: HelperDataService,
        private activatedRoute: ActivatedRoute,
94 95
        private router: Router
    ) {
96 97
        this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
        this.generalReport$ = new BehaviorSubject([{}]);
98
        this.minedu_userName = "";
99 100
        this.validCreator = -1;
        this.createGraph = false;
101 102 103
    }

    ngOnInit() {
104
        this.loginInfoSub = this._ngRedux.select("loginInfo")
105
            .map(loginInfo => <ILoginInfoRecords>loginInfo)
106 107
            .subscribe(loginInfo => {
                if (loginInfo.size > 0) {
108 109 110 111
                    loginInfo.reduce(({ }, loginInfoObj) => {
                        this.minedu_userName = loginInfoObj.minedu_username;
                        this.minedu_userPassword = loginInfoObj.minedu_userpassword;
                        return loginInfoObj;
112 113 114 115
                    }, {});
                }
                this.loginInfo$.next(loginInfo);
            }, error => console.log("error selecting loginInfo"));
116 117 118
    }

    ngOnDestroy() {
119 120 121 122
        if (this.loginInfoSub)
            this.loginInfoSub.unsubscribe();
        if (this.generalReportSub)
            this.generalReportSub.unsubscribe();
123 124
    }

125
    createReport() {
126 127 128
        this.validCreator = 0;
        this.createGraph = false;

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        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();
            },
145 146 147 148 149 150
            error => {
                this.generalReport$.next([{}]);
                this.validCreator = -1;
                console.log("Error Getting generalReport");
            });
    }
151

152
    navigateBack() {
153
        this.router.navigate(["/ministry/minister-reports"]);
154
    }
155

156
    onSearch(query: string = "") {
157 158
        this.csvObj.onSearch(query);
    }
159

160 161 162
    export2Csv() {
        this.csvObj.export2Csv();
    }
163 164


165 166 167 168 169 170 171 172 173 174
    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;
        }
    }
175

176 177 178
    generateGraphData() {
        this.d3data = [];

179 180 181 182 183 184 185 186 187 188 189
        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,
            ]);
190 191
        }
    }
192 193

}