import { Component, OnInit, OnDestroy, ElementRef, ViewChild, Renderer} from "@angular/core";
import { Injectable } from "@angular/core";
import { AppSettings } from '../../app.settings';
import { HelperDataService } from '../../services/helper-data-service';
import {Observable} from "rxjs/Observable";
import {Http, Headers, RequestOptions} from '@angular/http';
import { NgRedux, select } from 'ng2-redux';
import { IAppState } from '../../store/store';
import {Router, ActivatedRoute, Params} from '@angular/router';
import { BehaviorSubject, Subscription } from 'rxjs/Rx';
import { ILoginInfo } from '../../store/logininfo/logininfo.types';
import {
FormBuilder,
FormGroup,
FormControl,
FormArray,
Validators,
} from '@angular/forms';
@Component({
selector: 'director-view',
template: `
`
})
@Injectable() export default class DirectorView implements OnInit, OnDestroy {
public formGroup: FormGroup;
private StudentSelected$: BehaviorSubject;
private StudentSelectedSub: Subscription;
private School$: BehaviorSubject;
private SchoolSub: Subscription;
private StudentInfo$: BehaviorSubject;
private StudentInfoSub: Subscription;
private StudentsSize$: BehaviorSubject;
private SavedStudentsSub: Subscription;
private SavedStudents$: BehaviorSubject;
private StudentsSizeSub: Subscription;
private StudentSelectedSpecial$: BehaviorSubject;
private StudentSelectedSpecialSub: Subscription;
private SubmitedDetails$: BehaviorSubject;
private SubmitedDetailsSub: Subscription;
private retrievedStudent: BehaviorSubject;
private selectionBClass: BehaviorSubject;
private selectiontype: BehaviorSubject;
private selectionCClass: BehaviorSubject;
private SchoolId ;
private currentclass: Number;
private saved: Array = new Array();
private limitdown = 0;
private limitup = 5;
private pageno = 1;
private userActive = -1;
private type: Number;
@ViewChild('fileInput') fileInput: ElementRef;
constructor(private fb: FormBuilder,
private _hds: HelperDataService,
private activatedRoute: ActivatedRoute,
private router: Router,
private renderer: Renderer) {
this.StudentSelected$ = new BehaviorSubject([{}]);
this.StudentSelectedSpecial$ = new BehaviorSubject([{}]);
this.StudentInfo$ = new BehaviorSubject([{}]);
this.StudentsSize$ = new BehaviorSubject({});
this.SavedStudents$ = new BehaviorSubject({});
this.SubmitedDetails$ = new BehaviorSubject([{}]);
this.retrievedStudent = new BehaviorSubject(false);
this.selectionBClass = new BehaviorSubject(false);
this.selectionCClass = new BehaviorSubject(false);
this.School$ = new BehaviorSubject([{}]);
this.selectiontype = new BehaviorSubject(true);
this.formGroup = this.fb.group({
tomeas: ['', []],
taxi: ['', []],
specialit: ['', []],
maxpage: [{ value: '', disabled: true }, []],
pageno: [{ value: '', disabled: true }, []],
});
}
ngOnDestroy() {
if (this.StudentSelectedSub)
this.StudentSelectedSub.unsubscribe();
if (this.StudentSelectedSpecialSub)
this.StudentSelectedSpecialSub.unsubscribe();
if (this.selectionBClass)
this.selectionBClass.unsubscribe();
if (this.selectionCClass)
this.selectionCClass.unsubscribe();
if (this.retrievedStudent)
this.retrievedStudent.unsubscribe();
if (this.SubmitedDetailsSub)
this.SubmitedDetailsSub.unsubscribe();
}
ngOnInit() {
this.SchoolSub = this._hds.gettypeofschool().subscribe(x => {
this.School$.next(x);
console.log(x[0].type, "schoolid!");
this.SchoolId = x[0].type;
if (this.SchoolId == 'ΗΜΕΡΗΣΙΟ'){
this.selectiontype.next(false);
}
},
error => {
this.School$.next([{}]);
console.log("Error Getting School");
},
() => console.log("Getting School "));
}
verifyclass(txop) {
this.limitdown = 0;
this.limitup = 5;
console.log(this.SchoolId,"schoolida");
this.pageno = 1;
this.retrievedStudent.next(false);
if (txop.value === "1") {
this.selectionBClass.next(false);
this.selectionCClass.next(false);
}
else if (txop.value === "2") {
this.StudentSelectedSub = this._hds.getSectorPerSchool().subscribe(data => {
this.selectionBClass.next(true);
this.selectionCClass.next(false);
this.StudentSelected$.next(data);
},
error => {
this.StudentSelected$.next([{}]);
console.log("Error Getting StudentSelectedSpecial");
},
() => console.log("Getting StudentSelectedSpecial"));
}
else if (txop.value === "3" || txop.value === "4") {
var sectorint = +this.formGroup.value.tomeas;
if (this.formGroup.value.tomeas != '') {
var sectorint = +this.formGroup.value.tomeas;
this.StudentSelectedSpecialSub = this._hds.getSpecialityPerSchool(sectorint).subscribe(data => {
this.StudentSelectedSpecial$.next(data);
},
error => {
this.StudentSelectedSpecial$.next([{}]);
console.log("Error Getting StudentSelectedSpecial");
},
() => console.log("Getting StudentSelectedSpecial"));
}
this.StudentSelectedSub = this._hds.getSectorPerSchool().subscribe(data => {
this.StudentSelected$.next(data);
this.selectionBClass.next(true);
this.selectionCClass.next(true);
},
error => {
this.StudentSelected$.next([{}]);
console.log("Error Getting StudentSelected");
},
() => console.log("Getting StudentSelected"));
}
}
checkbclass(tmop, txop) {
console.log(this.SchoolId,"schoolidn");
this.pageno = 1;
this.retrievedStudent.next(false);
var sectorint = +this.formGroup.value.tomeas;
console.log(sectorint, "tomeas");
if (txop.value === "3" || txop.value === "4") {
// this.StudentSelectedSpecial$ = new BehaviorSubject([{}]);
this.StudentSelectedSpecialSub = this._hds.getSpecialityPerSchool(sectorint).subscribe(data => {
this.StudentSelectedSpecial$.next(data);
},
error => {
this.StudentSelectedSpecial$.next([{}]);
console.log("Error Getting StudentSelectedSpecial");
},
() => console.log("Getting StudentSelectedSpecial"));
}
}
findstudent(txop, pageno) {
var tot_pages: number;
var sectorint = +this.formGroup.value.tomeas;
if (txop.value === "1") {
this.currentclass = 1;
}
else if (txop.value === "2") {
this.currentclass = 2;
}
else if (txop.value === "3") {
this.currentclass = 3;
}
else if (txop.value === "4") {
this.currentclass = 4;
}
this.formGroup.get('pageno').setValue(this.pageno);
if (this.pageno == 1) {
console.log(this.SchoolId, sectorint, this.currentclass, "testaaaaaa");
this.StudentsSizeSub = this._hds.getStudentPerSchool(sectorint, this.currentclass, 0, 0).subscribe(x => {
this.StudentsSize$.next(x);
tot_pages = x.id / 5;
if (x.id % 5 > 0) {
tot_pages = (x.id - (x.id % 5)) / 5 + 1;
}
console.log(tot_pages,"totpages")
if (isNaN(tot_pages)){
this.retrievedStudent.next(false);
tot_pages = 0;
}
this.formGroup.get('maxpage').setValue(tot_pages);
});
}
console.log(this.limitdown, this.limitup,"mits");
this.StudentInfoSub = this._hds.getStudentPerSchool(sectorint, this.currentclass, this.limitdown, this.limitup).subscribe(data => {
this.StudentInfo$.next(data);
if (tot_pages === 0){
console.log("tot.pages", this.formGroup.value.maxpage);
this.retrievedStudent.next(false);
}
else
{
console.log("tot.pages", this.formGroup.value.maxpage, "max", tot_pages);
this.retrievedStudent.next(true);
}
},
error => {
this.StudentInfo$.next([{}]);
console.log("Error Getting Students");
},
() => console.log("Getting Students"));
}
updateCheckedOptions(id, cbvalue) {
let i = 0;
if (cbvalue.value === '1') {
this.saved[i] = id;
this.type = 1;
}
else if (cbvalue.value === '2') {
this.saved[i] = id;
this.type = 2;
console.log("not confirmed")
}
else if (cbvalue.value === '3') {
}
}
confirmStudent(txop) {
this.SavedStudentsSub = this._hds.saveConfirmStudents(this.saved, this.type).subscribe(data => {
this.SavedStudents$.next(data);
},
error => {
this.SavedStudents$.next([{}]);
console.log("Error saving Students");
},
() => {
console.log("saved Students");
this.findstudent(txop, this.pageno);
});
}
checkcclass() {
this.pageno = 1;
this.retrievedStudent.next(false);
}
nextpage(txop, maxpage) {
console.log(maxpage.value);
if (this.pageno < maxpage.value) {
this.pageno = this.pageno + 1;
this.limitdown = (this.pageno - 1) * 5;
this.limitup = this.pageno * 5;
this.findstudent(txop, this.pageno)
}
}
prevpage(txop) {
console.log(this.pageno, "pageno");
if (this.pageno > 1) {
this.pageno = this.pageno - 1;
this.limitdown = (this.pageno - 1) * 5;
this.limitup = this.pageno * 5;
this.findstudent(txop, this.pageno)
}
}
setActiveUser(ind) {
ind = +ind;
console.log(this.userActive, "RA", ind);
if (ind === this.userActive) {
ind = -1;
}
ind--;
this.userActive = ind + 1;
}
}