import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; import { Observable } from 'rxjs/Rx'; import { Injectable } from "@angular/core"; import { RegionSchoolsActions } from '../../actions/regionschools.actions'; import { NgRedux, select } from 'ng2-redux'; import { IRegions } from '../../store/regionschools/regionschools.types'; import { SectorCoursesActions } from '../../actions/sectorcourses.actions'; import { ISectors } from '../../store/sectorcourses/sectorcourses.types'; import { IAppState } from '../../store/store'; import {RemoveSpaces} from '../../pipes/removespaces'; import { FormBuilder, FormGroup, FormControl, FormArray } from '@angular/forms'; import {AppSettings} from '../../app.settings'; @Component({ selector: 'course-fields-select', template: `
` }) @Injectable() export default class RegionSchoolsSelect implements OnInit { private regions$: Observable; private sectors$: Observable; private showLoader$: Observable; private formGroup: FormGroup; private rss = new FormArray([]); private classActive = "-1"; private regionActive = -1; private courseActive = -1; private numSelected = 0; //private schoolArray: Array = new Array(); constructor(private fb: FormBuilder, private _rsa: RegionSchoolsActions, private _rsb: SectorCoursesActions, private _ngRedux: NgRedux, private router: Router ) { this.formGroup = this.fb.group({ formArray: this.rss }); }; ngOnInit() { this.classActive = this.classActive = this.getClassActive(); let class_id = -1; if (this.classActive === "Α' Λυκείου") { //είναι Α' Λυκείου, οπότε courseActive = "-1" (είναι ήδη ορισμένο με αυτή την τιμή από την αρχικοποίηση) class_id = 1; } else if (this.classActive === "Β' Λυκείου") { class_id = 2; this.courseActive = this.getSectorActive(); } else if (this.classActive === "Γ' Λυκείου") { class_id = 3; this.courseActive = this.getCourseActive(); } this._rsa.getRegionSchools(class_id,this.courseActive, false); this.regions$ = this._ngRedux.select(state => { let numsel = 0; state.regions.reduce((prevRegion, region) =>{ region.epals.reduce((prevEpal, epal) =>{ this.rss.push( new FormControl(epal.selected, [])); if (epal.selected === true) { numsel++; } return epal; }, {}); return region; }, {}); this.numSelected = numsel; return state.regions; }); this.showLoader$ = this.regions$.map(regions => regions.size === 0); } navigateBack() { // this.router.navigate(['/epal-class-select']); if (this.classActive === "Α' Λυκείου") { this.router.navigate(['/epal-class-select']); } else if (this.classActive === "Β' Λυκείου") { this.router.navigate(['/sector-fields-select']); } else if (this.classActive === "Γ' Λυκείου") { this.router.navigate(['/sectorcourses-fields-select']); } } setActiveRegion(ind) { if (ind === this.regionActive) ind = -1; this.regionActive = ind; } saveSelected(cb,j) { this._rsa.saveRegionSchoolsSelected(this.formGroup.value.formArray); } navigateToApplication() { //if (this.numSelected > 1) this.router.navigate(['/schools-order-select']); //else // this.router.navigate(['/student-application-form-main']); } getCourseActive() { const { sectors } = this._ngRedux.getState(); let l,m; for ( l=0; l