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

import { API_ENDPOINT } from "../../app.settings";
9 10 11 12 13 14
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 { CsvCreator } from "./csv-creator";
import { ReportsSchema, TableColumn } from "./reports-schema";
15 16

@Component({
17
    selector: "report-users",
18 19 20 21 22 23 24 25 26 27 28 29 30 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
    template: `

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

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

        <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>

        <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 ReportUsers implements OnInit, OnDestroy {

56
    loginInfo$: BehaviorSubject<ILoginInfoRecords>;
57 58 59 60 61 62 63 64 65 66 67 68
    loginInfoSub: Subscription;
    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 routerSub: any;

    private source: LocalDataSource;
69
    columnMap: Map<string, TableColumn> = new Map<string, TableColumn>();
70
    @Input() settings: any;
71 72
    private reportSchema = new ReportsSchema();
    private csvObj = new CsvCreator();
73

74 75
    constructor(
        private _ngRedux: NgRedux<IAppState>,
76 77
        private _hds: HelperDataService,
        private activatedRoute: ActivatedRoute,
78 79
        private router: Router
    ) {
80 81
        this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
        this.generalReport$ = new BehaviorSubject([{}]);
82
        this.minedu_userName = "";
83
        this.validCreator = -1;
84 85 86 87
    }

    ngOnInit() {

88
        this.loginInfoSub = this._ngRedux.select("loginInfo")
89
            .map(loginInfo => <ILoginInfoRecords>loginInfo)
90 91
            .subscribe(loginInfo => {
                if (loginInfo.size > 0) {
92 93 94 95
                    loginInfo.reduce(({}, loginInfoObj) => {
                        this.minedu_userName = loginInfoObj.minedu_username;
                        this.minedu_userPassword = loginInfoObj.minedu_userpassword;
                        return loginInfoObj;
96 97 98 99
                    }, {});
                }
                this.loginInfo$.next(loginInfo);
            }, error => console.log("error selecting loginInfo"));
100 101 102
    }

    ngOnDestroy() {
103 104 105 106
        if (this.loginInfoSub)
            this.loginInfoSub.unsubscribe();
        if (this.generalReportSub)
            this.generalReportSub.unsubscribe();
107 108
    }

109 110
    createReport() {
        this.validCreator = 0;
111
        let route = "/ministry/report-users/";
112
        this.settings = this.reportSchema.ReportUsersSchema;
113

114 115 116 117 118 119 120 121 122 123 124 125 126
        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();
            },
127 128 129 130 131 132
            error => {
                this.generalReport$.next([{}]);
                this.validCreator = -1;
                console.log("Error Getting ReportUsers");
            });
    }
133

134
    navigateBack() {
135
        this.router.navigate(["/ministry/minister-reports"]);
136
    }
137

138
    onSearch(query: string = "") {
139 140
        this.csvObj.onSearch(query);
    }
141

142 143 144
    export2Csv() {
        this.csvObj.export2Csv();
    }
145 146

}