report-no-capacity.ts 6.72 KB
Newer Older
1 2
import { Component, OnInit, OnDestroy, ElementRef, ViewChild, Input } from "@angular/core";
import { Injectable } from "@angular/core";
3 4
import { AppSettings } from "../../app.settings";
import { HelperDataService } from "../../services/helper-data-service";
5
import { Observable} from "rxjs/Observable";
6 7 8 9 10
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";
11
import { ILoginInfoRecords } from "../../store/logininfo/logininfo.types";
12 13 14 15 16
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";
17 18 19 20 21 22 23

import {
    FormBuilder,
    FormGroup,
    FormControl,
    FormArray,
    Validators,
24
} from "@angular/forms";
25 26


27
import { API_ENDPOINT } from "../../app.settings";
28 29

@Component({
30
    selector: "report-no-capacity",
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    template: `

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

        <form [formGroup]="formGroup"  #form>
          <h5> >Επιλογή Φίλτρων <br><br></h5>
          <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>
          </div>
          <br><br>

          <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>
          <br><br>
        </form>

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

        <button type="button" class="alert alert-info pull-right" (click)="export2Csv()" [hidden]="validCreator != 1">
        <i class="fa fa-download"></i>
            <br>Εξαγωγή σε csv
        </button>

    </div>

   `
})

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

80
    private formGroup: FormGroup;
81
    private loginInfo$: BehaviorSubject<ILoginInfoRecords>;
82
    private loginInfoSub: Subscription;
83 84 85 86 87 88 89 90 91 92 93 94 95
    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;
96
    columnMap: Map<string, TableColumn> = new Map<string, TableColumn>();
97
    @Input() settings: any;
98 99
    private reportSchema = new ReportsSchema();
    private csvObj = new CsvCreator();
100

101 102
    private chartObj = new ChartCreator();
    @ViewChild("chart") public chartContainer: ElementRef;
103 104 105 106 107 108 109 110
    private d3data: Array<any>;

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

111
        this.formGroup = this.fb.group({
112
            capacityEnabled: ["", []],
113
        });
114

115 116
        this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
        this.generalReport$ = new BehaviorSubject([{}]);
117
        this.minedu_userName = "";
118 119
        this.validCreator = -1;
        this.enableCapacityFilter = false;
120 121 122 123 124

    }

    ngOnInit() {

125
        this.loginInfoSub = this._ngRedux.select("loginInfo")
126
            .map(loginInfo => <ILoginInfoRecords>loginInfo)
127 128
            .subscribe(loginInfo => {
                if (loginInfo.size > 0) {
129 130 131 132
                    loginInfo.reduce(({}, loginInfoObj) => {
                        this.minedu_userName = loginInfoObj.minedu_username;
                        this.minedu_userPassword = loginInfoObj.minedu_userpassword;
                        return loginInfoObj;
133 134 135 136
                    }, {});
                }
                this.loginInfo$.next(loginInfo);
            }, error => console.log("error selecting loginInfo"));
137

138
        this.routerSub = this.activatedRoute.params.subscribe(params => {
139
            this.reportId = +params["reportId"];
140

141
        });
142 143 144 145 146

    }

    ngOnDestroy() {

147 148 149 150
        if (this.loginInfoSub)
            this.loginInfoSub.unsubscribe();
        if (this.generalReportSub)
            this.generalReportSub.unsubscribe();
151 152
    }

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    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>();

172
            // pass parametes to csv class object
173 174 175 176 177 178 179 180 181 182
            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");
            });
183

184
    }
185

186 187 188
    toggleCapacityFilter() {
        this.enableCapacityFilter = !this.enableCapacityFilter;
    }
189

190
    navigateBack() {
191
        this.router.navigate(["/ministry/minister-reports"]);
192
    }
193 194


195
    onSearch(query: string = "") {
196 197
        this.csvObj.onSearch(query);
    }
198 199


200 201 202
    export2Csv() {
        this.csvObj.export2Csv();
    }
203
}