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

9
import { API_ENDPOINT } from "../../app.settings";
10 11 12 13 14 15 16
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";
17 18

@Component({
19
    selector: "report-no-capacity",
20 21
    template: `

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

        <form [formGroup]="formGroup"  #form>
26 27 28 29 30
            <h5>Σχολικές μονάδες που δεν έχουν δηλώσει Χωρητικότητα τμημάτων</h5>
            <h6>Επιλογή Φίλτρων</h6>
            <div class="row">
                <div class="col-md-1"><input type="checkbox" formControlName="capacityEnabled" (click)="toggleCapacityFilter()" ></div>
                <div class="col-md-9"><label for="capacityEnabled"><i>Εμφάνιση ΚΑΙ των σχολείων που έχουν καθορίσει χωρητικότητα</i></label></div>
31 32
            </div>

33 34
            <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>
35 36 37 38
        </form>

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

44
        <button type="button" class="alert alert-info pull-right" (click)="export2Csv()" [hidden]="validCreator != 1"><i class="fa fa-download"></i> Εξαγωγή σε csv</button>
45 46 47 48 49 50
    </div>
   `
})

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

51
    private formGroup: FormGroup;
52
    private loginInfo$: BehaviorSubject<ILoginInfoRecords>;
53
    private loginInfoSub: Subscription;
54 55 56 57 58 59 60 61 62 63 64 65 66
    private generalReport$: BehaviorSubject<any>;
    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;
67
    columnMap: Map<string, TableColumn> = new Map<string, TableColumn>();
68
    @Input() settings: any;
69 70
    private reportSchema = new ReportsSchema();
    private csvObj = new CsvCreator();
71

72 73
    private chartObj = new ChartCreator();
    @ViewChild("chart") public chartContainer: ElementRef;
74 75 76 77 78 79 80 81
    private d3data: Array<any>;

    constructor(private fb: FormBuilder,
        private _ngRedux: NgRedux<IAppState>,
        private _hds: HelperDataService,
        private activatedRoute: ActivatedRoute,
        private router: Router) {

82
        this.formGroup = this.fb.group({
83
            capacityEnabled: ["", []],
84
        });
85

86 87
        this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
        this.generalReport$ = new BehaviorSubject([{}]);
88
        this.minedu_userName = "";
89 90
        this.validCreator = -1;
        this.enableCapacityFilter = false;
91 92 93 94 95

    }

    ngOnInit() {

96
        this.loginInfoSub = this._ngRedux.select("loginInfo")
97
            .map(loginInfo => <ILoginInfoRecords>loginInfo)
98 99
            .subscribe(loginInfo => {
                if (loginInfo.size > 0) {
100 101 102 103
                    loginInfo.reduce(({}, loginInfoObj) => {
                        this.minedu_userName = loginInfoObj.minedu_username;
                        this.minedu_userPassword = loginInfoObj.minedu_userpassword;
                        return loginInfoObj;
104 105 106 107
                    }, {});
                }
                this.loginInfo$.next(loginInfo);
            }, error => console.log("error selecting loginInfo"));
108

109
        this.routerSub = this.activatedRoute.params.subscribe(params => {
110
            this.reportId = +params["reportId"];
111

112
        });
113 114 115 116 117

    }

    ngOnDestroy() {

118 119 120 121
        if (this.loginInfoSub)
            this.loginInfoSub.unsubscribe();
        if (this.generalReportSub)
            this.generalReportSub.unsubscribe();
122 123
    }

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    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<string, TableColumn>();

143
            // pass parametes to csv class object
144 145 146 147 148 149 150 151 152 153
            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");
            });
154

155
    }
156

157 158 159
    toggleCapacityFilter() {
        this.enableCapacityFilter = !this.enableCapacityFilter;
    }
160

161
    navigateBack() {
162
        this.router.navigate(["/ministry/minister-reports"]);
163
    }
164 165


166
    onSearch(query: string = "") {
167 168
        this.csvObj.onSearch(query);
    }
169 170


171 172 173
    export2Csv() {
        this.csvObj.export2Csv();
    }
174
}