director-classcapacity.ts 11.7 KB
Newer Older
1
import { Component, OnInit, OnDestroy, ElementRef, ViewChild} from "@angular/core";
Open Source Developer's avatar
Open Source Developer committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
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-classcapacity',
    template: `
23
    <div style="min-height: 500px;">
Open Source Developer's avatar
Open Source Developer committed
24 25 26 27 28 29 30
  <form [formGroup]="formGroup">
      <label for="taxi">Τάξη</label><br/>
      <div class="form-group">
            <select #txoption  class="form-control" (change)="verifyclass(txoption)" formControlName="taxi">
              <option value="1" >Α' Λυκείου</option>
              <option value="2" >Β' Λυκείου</option>
              <option value="3" >Γ' Λυκείου</option>
Open Source Developer's avatar
Open Source Developer committed
31
              <option *ngIf="(selectiontype | async)" value="4" >Δ' Λυκείου</option>
Open Source Developer's avatar
Open Source Developer committed
32
            </select>
Open Source Developer's avatar
Open Source Developer committed
33 34

           
35
      </div>
Open Source Developer's avatar
Open Source Developer committed
36 37 38 39 40 41 42
      <div class="form-group">

            <select #tmop class="form-control" *ngIf="(selectionBClass | async)" (change)="checkbclass(tmop,txoption)" formControlName="tomeas">
              <option *ngFor="let SectorSelection$  of StudentSelected$ | async; let i=index" [value] = "SectorSelection$.id"> {{SectorSelection$.sector_id}} </option>
            </select>
      </div>
      <div class="form-group">
Open Source Developer's avatar
trans  
Open Source Developer committed
43
            <select #spop class="form-control" *ngIf="(selectionCClass | async)" (change)="checkcclass(tmop,txoption,spop)"  formControlName="specialit">
Open Source Developer's avatar
Open Source Developer committed
44 45 46
              <option *ngFor="let SpecialSelection$  of StudentSelectedSpecial$ | async; let i=index" [value] = "SpecialSelection$.id"> {{SpecialSelection$.specialty_id}} </option>
            </select>
      </div>
47

Open Source Developer's avatar
trans  
Open Source Developer committed
48

49

Open Source Developer's avatar
trans  
Open Source Developer committed
50
      <p style="margin-top: 20px; line-height: 2em;"> Αλλάξτε παρακαλώ τον αριθμό των τμημάτων που μπορείτε να δημιουργήσετε στο σχολείο σας και πατήστε  <i>Αποθήκευση</i>.</p>
Open Source Developer's avatar
trans  
Open Source Developer committed
51
       <input  type="number" formControlName="capacity" min="1" max="10">
52

Open Source Developer's avatar
Open Source Developer committed
53 54 55
            <button type="button" class="btn-primary btn-sm pull-right" (click) ="saveCapacity()">
                Αποθήκευση
             </button>
56 57
       </form>
       </div>
Open Source Developer's avatar
trans  
Open Source Developer committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

    <div id="capacitysaved" (onHidden)="onHidden('#capacitysaved')" 
    class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
      <div class="modal-dialog modal-lg">
        <div class="modal-content">
          <div class="modal-header modal-header-success">
            <h3 class="modal-title pull-left"><i class="fa fa-check-square-o"></i>&nbsp;&nbsp;Αποθήκευση Δυναμικής</h3>
            <button type="button" class="close pull-right" aria-label="Close" (click)="hideModal('#capacitysaved')">
              <span aria-hidden="true"><i class="fa fa-times"></i></span>
            </button>
          </div>
          <div class="modal-body">
            <p>Η επιλογή σας έχει αποθηκευτεί.</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Κλείσιμο</button>
          </div>
        </div>
      </div>
    </div>


Open Source Developer's avatar
Open Source Developer committed
80 81 82
   `
})

83
@Injectable() export default class DirectorClassCapacity implements OnInit, OnDestroy {
Open Source Developer's avatar
Open Source Developer committed
84 85 86 87 88 89

    public formGroup: FormGroup;
    private StudentSelected$: BehaviorSubject<any>;
    private StudentSelectedSub: Subscription;
    private StudentSelectedSpecial$: BehaviorSubject<any>;
    private StudentSelectedSpecialSub: Subscription;
Open Source Developer's avatar
trans  
Open Source Developer committed
90
    private saveCapacitySub: Subscription;
Open Source Developer's avatar
Open Source Developer committed
91
    private selectionBClass: BehaviorSubject<boolean>;
Open Source Developer's avatar
Open Source Developer committed
92
    private selectiontype: BehaviorSubject<boolean>;
Open Source Developer's avatar
Open Source Developer committed
93
    private selectionCClass: BehaviorSubject<boolean>;
Open Source Developer's avatar
trans  
Open Source Developer committed
94 95 96
    private School$: BehaviorSubject<any>;
    private SchoolSub: Subscription;
    private SchoolId;
Open Source Developer's avatar
Open Source Developer committed
97
    private currentclass: Number;
Open Source Developer's avatar
trans  
Open Source Developer committed
98 99 100
    private classCapacity$: BehaviorSubject<any>;
    private classCapacitySub: Subscription;
    private retrievedStudent: BehaviorSubject<boolean>;
Open Source Developer's avatar
trans  
Open Source Developer committed
101
    
102 103 104 105 106 107 108 109 110 111 112



    constructor(private fb: FormBuilder,
        private _hds: HelperDataService,
        private activatedRoute: ActivatedRoute,
        private router: Router) {
        this.StudentSelected$ = new BehaviorSubject([{}]);
        this.StudentSelectedSpecial$ = new BehaviorSubject([{}]);
        this.classCapacity$ = new BehaviorSubject([{}]);
        this.selectionBClass = new BehaviorSubject(false);
Open Source Developer's avatar
Open Source Developer committed
113
        this.selectiontype = new BehaviorSubject(true);
114 115
        this.selectionCClass = new BehaviorSubject(false);
        this.retrievedStudent = new BehaviorSubject(false);
Open Source Developer's avatar
trans  
Open Source Developer committed
116
        this.School$ = new BehaviorSubject([{}]);
117 118 119 120 121
        this.formGroup = this.fb.group({
            tomeas: ['', []],
            taxi: ['', []],
            specialit: ['', []],
            capacity: ['', []],
Open Source Developer's avatar
trans  
Open Source Developer committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
            });

    }

    public showModal(popupMsgId):void {
        console.log("about to show modal");
        //(<any>$('#distributionWaitingNotice')).modal('show');
        (<any>$(popupMsgId)).modal('show');
    }

    public hideModal(popupMsgId):void {
        //(<any>$('#distributionWaitingNotice')).modal('hide');
        (<any>$(popupMsgId)).modal('hide');
    }

    public onHidden(popupMsgId):void {
138

Open Source Developer's avatar
Open Source Developer committed
139 140
    }

141
    ngOnDestroy() {
Open Source Developer's avatar
Open Source Developer committed
142 143 144 145 146 147 148
        if (this.StudentSelectedSub)
            this.StudentSelectedSub.unsubscribe();
        if (this.StudentSelectedSpecialSub)
            this.StudentSelectedSpecialSub.unsubscribe();
        if (this.selectionBClass)
            this.selectionBClass.unsubscribe();
        if (this.selectionCClass)
149
            this.selectionCClass.unsubscribe();
Open Source Developer's avatar
trans  
Open Source Developer committed
150 151 152
        if (this.classCapacitySub)
            this.classCapacitySub.unsubscribe();
        if (this.retrievedStudent)
153
            this.retrievedStudent.unsubscribe();
Open Source Developer's avatar
Open Source Developer committed
154
    }
155

Open Source Developer's avatar
Open Source Developer committed
156
    ngOnInit() {
Open Source Developer's avatar
trans  
Open Source Developer committed
157
        (<any>$('#capacitysaved')).appendTo("body");
Open Source Developer's avatar
trans  
Open Source Developer committed
158
        this.retrievedStudent.next(false);
Open Source Developer's avatar
trans  
Open Source Developer committed
159

Open Source Developer's avatar
Open Source Developer committed
160
            this.SchoolSub = this._hds.gettypeofschool().subscribe(x => {
Open Source Developer's avatar
trans  
Open Source Developer committed
161
                  this.School$.next(x);                 
Open Source Developer's avatar
Open Source Developer committed
162 163 164 165 166
                  console.log(x[0].type, "schoolid!");
                   this.SchoolId = x[0].type;
                   if (this.SchoolId == 'ΗΜΕΡΗΣΙΟ'){
                       this.selectiontype.next(false);
                   }
Open Source Developer's avatar
trans  
Open Source Developer committed
167 168 169 170 171 172 173 174

                  },
                  error => {
                      this.School$.next([{}]);
                      console.log("Error Getting School");
                  },
                  () => console.log("Getting School "));

Open Source Developer's avatar
Open Source Developer committed
175 176 177
    }


178 179 180 181 182 183 184 185
    verifyclass(txop) {
        console.log(this.formGroup.value.specialit, "speciality");
        if (txop.value === "1") {
            this.selectionBClass.next(false);
            this.selectionCClass.next(false);
            this.formGroup.patchValue({
                tomeas: '',
                specialit: '',
Open Source Developer's avatar
trans  
Open Source Developer committed
186
                capacity: '',
187 188
            });
            console.log("a class");
Open Source Developer's avatar
Open Source Developer committed
189
            this.classCapacitySub = this._hds.getCapacityPerSchool(this.formGroup.value.taxi, 0, 0).subscribe(data => {
190 191
                this.classCapacity$.next(data);
                this.retrievedStudent.next(true);
Open Source Developer's avatar
trans  
Open Source Developer committed
192 193 194 195
                this.formGroup.patchValue({
                capacity : data[0].capacity,
            });
            
196 197 198 199 200 201
            },
                error => {
                    this.classCapacity$.next([{}]);
                    console.log("Error Getting Capacity");
                },
                () => console.log("Getting Capacity"));
Open Source Developer's avatar
Open Source Developer committed
202

203 204 205
        }
        else if (txop.value === "2") {
            this.formGroup.patchValue({
Open Source Developer's avatar
trans  
Open Source Developer committed
206
                tomeas: '',
207
                specialit: '',
Open Source Developer's avatar
trans  
Open Source Developer committed
208
                capacity: '',
209 210 211 212
            });
            this.selectionBClass.next(true);
            this.selectionCClass.next(false);
            this.StudentSelected$ = new BehaviorSubject([{}]);
Open Source Developer's avatar
Open Source Developer committed
213
            this.StudentSelectedSub = this._hds.getSectorPerSchool().subscribe(this.StudentSelected$);
214
        }
Open Source Developer's avatar
Open Source Developer committed
215
        else if (txop.value === "3" || txop.value === "4" ) {
Open Source Developer's avatar
trans  
Open Source Developer committed
216 217 218 219 220 221
            this.formGroup.patchValue({
                tomeas: '',
                specialit: '',
                capacity: '',
            });

222 223 224 225 226
            var sectorint = +this.formGroup.value.tomeas;
            console.log(sectorint, "test");
            if (this.formGroup.value.tomeas != '') {
                var sectorint = +this.formGroup.value.tomeas;
                this.StudentSelectedSpecial$ = new BehaviorSubject([{}]);
Open Source Developer's avatar
Open Source Developer committed
227
                this.StudentSelectedSpecialSub = this._hds.getSpecialityPerSchool( sectorint).subscribe(this.StudentSelectedSpecial$);
Open Source Developer's avatar
Open Source Developer committed
228
            }
229 230 231
            this.selectionBClass.next(true);
            this.selectionCClass.next(true);
            this.StudentSelected$ = new BehaviorSubject([{}]);
Open Source Developer's avatar
Open Source Developer committed
232
            this.StudentSelectedSub = this._hds.getSectorPerSchool().subscribe(this.StudentSelected$);
233 234 235 236 237
        }




Open Source Developer's avatar
Open Source Developer committed
238 239 240
    }


241
    checkbclass(tmop, txop) {
Open Source Developer's avatar
Open Source Developer committed
242

Open Source Developer's avatar
trans  
Open Source Developer committed
243
        
Open Source Developer's avatar
Open Source Developer committed
244
        var sectorint = +this.formGroup.value.tomeas;
Open Source Developer's avatar
Open Source Developer committed
245
        console.log(tmop, txop, "tomeas!!!!");
246 247
        if (txop.value === "2") {
            console.log("b class");
Open Source Developer's avatar
Open Source Developer committed
248
            this.classCapacitySub = this._hds.getCapacityPerSchool(this.formGroup.value.taxi, sectorint, 0).subscribe(data => {
249 250
                this.classCapacity$.next(data);
                this.retrievedStudent.next(true);
Open Source Developer's avatar
trans  
Open Source Developer committed
251 252 253
                this.formGroup.patchValue({
                capacity : data[0].capacity,
            });
254 255 256 257 258 259
            },
                error => {
                    this.classCapacity$.next([{}]);
                    console.log("Error Getting Capacity");
                },
                () => console.log("Getting Capacity"));
Open Source Developer's avatar
trans  
Open Source Developer committed
260
        }
Open Source Developer's avatar
Open Source Developer committed
261
        if (txop.value === "3" || txop.value === "4") {
Open Source Developer's avatar
Open Source Developer committed
262
            this.StudentSelectedSpecial$ = new BehaviorSubject([{}]);
Open Source Developer's avatar
Open Source Developer committed
263
            this.StudentSelectedSpecialSub = this._hds.getSpecialityPerSchool(sectorint).subscribe(this.StudentSelectedSpecial$);
264

Open Source Developer's avatar
trans  
Open Source Developer committed
265

Open Source Developer's avatar
Open Source Developer committed
266
        }
Open Source Developer's avatar
trans  
Open Source Developer committed
267 268 269

    }

270
    checkcclass(tmop, txop, spop) {
Open Source Developer's avatar
trans  
Open Source Developer committed
271
        
272
        var sectorint = +this.formGroup.value.tomeas;
Open Source Developer's avatar
trans  
Open Source Developer committed
273
        var specialint = +this.formGroup.value.specialit;
274

Open Source Developer's avatar
Open Source Developer committed
275
        if (txop.value === "3" || txop.value === "4") {
276
            console.log("c class");
Open Source Developer's avatar
trans  
Open Source Developer committed
277
            console.log(sectorint, specialint, "cclass")
Open Source Developer's avatar
Open Source Developer committed
278
            this.classCapacitySub = this._hds.getCapacityPerSchool(this.formGroup.value.taxi, sectorint, specialint).subscribe(data => {
279 280
                this.classCapacity$.next(data);
                this.retrievedStudent.next(true);
Open Source Developer's avatar
trans  
Open Source Developer committed
281
                
Open Source Developer's avatar
trans  
Open Source Developer committed
282 283 284
               this.formGroup.patchValue({
                capacity : data[0].capacity,
            });
285 286 287 288 289 290
            },
                error => {
                    this.classCapacity$.next([{}]);
                    console.log("Error Getting Capacity");
                },
                () => console.log("Getting Capacity"));
Open Source Developer's avatar
trans  
Open Source Developer committed
291 292 293

        }

Open Source Developer's avatar
Open Source Developer committed
294 295 296 297 298
    }




299
    saveCapacity() {
Open Source Developer's avatar
Open Source Developer committed
300

301 302
        var tomeas = +this.formGroup.value.tomeas;
        var specialit = +this.formGroup.value.specialit;
Open Source Developer's avatar
trans  
Open Source Developer committed
303
        
Open Source Developer's avatar
Open Source Developer committed
304
         this.saveCapacitySub = this._hds.saveCapacity(this.formGroup.value.taxi, tomeas, specialit, this.formGroup.value.capacity).subscribe(data => {
Open Source Developer's avatar
trans  
Open Source Developer committed
305 306 307 308 309 310
                
                 },
                error => {
                    
                    console.log("Error Saving Capacity");
                },
Open Source Developer's avatar
Open Source Developer committed
311 312
                () =>{
                 console.log("Saved Capacity");
Open Source Developer's avatar
trans  
Open Source Developer committed
313
                 this.showModal("#capacitysaved");
Open Source Developer's avatar
Open Source Developer committed
314 315
                 });
                 
Open Source Developer's avatar
Open Source Developer committed
316

317
    }
Open Source Developer's avatar
Open Source Developer committed
318

Open Source Developer's avatar
trans  
Open Source Developer committed
319

320
}