import { Component, OnInit, OnDestroy } from '@angular/core';
import { Router } from '@angular/router';
import { BehaviorSubject, Subscription } from 'rxjs/Rx';
import { Injectable } from "@angular/core";
import { SectorCoursesActions } from '../../actions/sectorcourses.actions';
import { ISectors } from '../../store/sectorcourses/sectorcourses.types';
import { SECTOR_COURSES_INITIAL_STATE } from '../../store/sectorcourses/sectorcourses.initial-state';
import { NgRedux, select } from 'ng2-redux';
import { IAppState } from '../../store/store';
import {RemoveSpaces} from '../../pipes/removespaces';
import { RegionSchoolsActions } from '../../actions/regionschools.actions';
import {
FormBuilder,
FormGroup,
FormControl,
FormArray
} from '@angular/forms';
import {AppSettings} from '../../app.settings';
@Component({
selector: 'sectorcourses-fields-select',
template: `
Επιλογή Ειδικότητας
`
})
@Injectable() export default class SectorCoursesSelect implements OnInit, OnDestroy {
private sectors$: BehaviorSubject;
private sectorsSub: Subscription;
private formGroup: FormGroup;
private rss = new FormArray([]);
private sectorActive = -1;
private idx = -1;
private sectorsList: Array = new Array();
private modalTitle: BehaviorSubject;
private modalText: BehaviorSubject;
private modalHeader: BehaviorSubject;
public isModalShown: BehaviorSubject;
constructor(private fb: FormBuilder,
private _sca: SectorCoursesActions,
private _rsa: RegionSchoolsActions,
private _ngRedux: NgRedux,
private router: Router
) {
this.sectors$ = new BehaviorSubject(SECTOR_COURSES_INITIAL_STATE);
this.formGroup = this.fb.group({
formArray: this.rss
});
this.modalTitle = new BehaviorSubject("");
this.modalText = new BehaviorSubject("");
this.modalHeader = new BehaviorSubject("");
this.isModalShown = new BehaviorSubject(false);
};
ngOnInit() {
($('#sectorCourseNotice')).appendTo("body");
this._sca.getSectorCourses(false);
let ids = 0;
this.sectorsSub = this._ngRedux.select(state => {
state.sectors.reduce((prevSector, sector) =>{
this.sectorsList[ids] = sector.sector_selected;
ids++;
//In case we want to preserve last checked option when we revisit the form
if (sector.sector_selected === true)
this.sectorActive = ids-1;
sector.courses.reduce((prevCourse, course) =>{
this.rss.push( new FormControl(course.selected, []));
//this.retrieveCheck();
if (course.selected === true) {
//In case we want to preserve last checked option when we revisit the form
this.idx = course.globalIndex;
}
return course;
}, {});
return sector;
}, {});
ids = 0;
return state.sectors;
}).subscribe(this.sectors$);
}
ngOnDestroy() {
($('#sectorCourseNotice')).remove();
if (this.sectorsSub) this.sectorsSub.unsubscribe();
this.sectors$.unsubscribe();
}
public showModal(): void {
($('#sectorCourseNotice')).modal('show');
}
public hideModal(): void {
($('#sectorCourseNotice')).modal('hide');
}
public onHidden(): void {
this.isModalShown.next(false);
}
setActiveSector(ind) {
if (ind === this.sectorActive)
ind = -1;
this.sectorActive = ind;
}
saveSelected() {
this._sca.saveSectorCoursesSelected(this.formGroup.value.formArray, this.sectorsList);
this._rsa.initRegionSchools();
}
navigateToSchools() {
if (this.idx === -1) {
this.modalTitle.next("Δεν επιλέχθηκε ειδικότητα");
this.modalText.next("Παρακαλούμε να επιλέξετε πρώτα ειδικότητα φοίτησης του μαθητή για το νέο σχολικό έτος");
this.modalHeader.next("modal-header-danger");
this.showModal();
} else {
this.router.navigate(['/region-schools-select']);
}
}
updateCheckedOptions(globalIndex, cb){
this.idx = globalIndex;
for (let i = 0; i < this.formGroup.value.formArray.length; i++)
this.formGroup.value.formArray[i] = false;
this.formGroup.value.formArray[globalIndex] = cb.checked;
if (cb.checked === false)
this.idx = -1;
for (let i = 0; i < this.sectorsList.length; i++)
this.sectorsList[i] = false;
this.sectorsList[this.sectorActive] = true;
this.saveSelected();
}
}