director-classcapacity.ts 14.2 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
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';
Open Source Developer's avatar
Open Source Developer committed
12 13 14
import { VALID_CAPACITY_PATTERN} from '../../constants';
import {maxValue} from '../../constants';

Open Source Developer's avatar
Open Source Developer committed
15 16 17 18 19 20 21 22 23 24 25

import {
    FormBuilder,
    FormGroup,
    FormControl,
    FormArray,
    Validators,
} from '@angular/forms';
@Component({
    selector: 'director-classcapacity',
    template: `
26
    <div style="min-height: 500px;">
Open Source Developer's avatar
Open Source Developer committed
27 28 29 30 31 32 33
  <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
34
              <option *ngIf="(selectiontype | async)" value="4" >Δ' Λυκείου</option>
Open Source Developer's avatar
Open Source Developer committed
35
            </select>
Open Source Developer's avatar
Open Source Developer committed
36 37

           
38
      </div>
Open Source Developer's avatar
Open Source Developer committed
39 40 41 42 43 44 45
      <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
46
            <select #spop class="form-control" *ngIf="(selectionCClass | async)" (change)="checkcclass(tmop,txoption,spop)"  formControlName="specialit">
Open Source Developer's avatar
Open Source Developer committed
47 48 49
              <option *ngFor="let SpecialSelection$  of StudentSelectedSpecial$ | async; let i=index" [value] = "SpecialSelection$.id"> {{SpecialSelection$.specialty_id}} </option>
            </select>
      </div>
50

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

52

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

        <div class="alert alert-danger" *ngIf="formGroup.get('capacity').touched && formGroup.get('capacity').hasError('maxValue')">
      Συμπληρώστε την διαθεσιμότητα σας σε τμήματα !
      </div>
       <div class="alert alert-danger" *ngIf="formGroup.get('capacity').touched && formGroup.get('capacity').hasError('required')">
          Παρακαλώ συμπληρώστε ένα μικρότερο αριθμό!
      </div>
62

Open Source Developer's avatar
Open Source Developer committed
63 64 65
            <button type="button" class="btn-primary btn-sm pull-right" (click) ="saveCapacity()">
                Αποθήκευση
             </button>
66 67
       </form>
       </div>
Open Source Developer's avatar
trans  
Open Source Developer committed
68

Open Source Developer's avatar
Open Source Developer committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    <div id="checksaved" (onHidden)="onHidden('#checksaved')" 
    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-danger">
            <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('#checksaved')">
              <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
trans  
Open Source Developer committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    <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
111 112 113
    


Open Source Developer's avatar
Open Source Developer committed
114 115 116
   `
})

117
@Injectable() export default class DirectorClassCapacity implements OnInit, OnDestroy {
Open Source Developer's avatar
Open Source Developer committed
118 119 120 121 122 123

    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
124
    private saveCapacitySub: Subscription;
Open Source Developer's avatar
Open Source Developer committed
125
    private selectionBClass: BehaviorSubject<boolean>;
Open Source Developer's avatar
Open Source Developer committed
126
    private selectiontype: BehaviorSubject<boolean>;
Open Source Developer's avatar
Open Source Developer committed
127
    private selectionCClass: BehaviorSubject<boolean>;
Open Source Developer's avatar
trans  
Open Source Developer committed
128 129 130
    private School$: BehaviorSubject<any>;
    private SchoolSub: Subscription;
    private SchoolId;
Open Source Developer's avatar
Open Source Developer committed
131
    private currentclass: Number;
Open Source Developer's avatar
trans  
Open Source Developer committed
132 133 134
    private classCapacity$: BehaviorSubject<any>;
    private classCapacitySub: Subscription;
    private retrievedStudent: BehaviorSubject<boolean>;
Open Source Developer's avatar
Open Source Developer committed
135 136 137
    private modalTitle: BehaviorSubject<string>;
    private modalText: BehaviorSubject<string>;
    private modalHeader: BehaviorSubject<string>;
Open Source Developer's avatar
trans  
Open Source Developer committed
138
    
139 140


Open Source Developer's avatar
Open Source Developer committed
141
 
142 143 144 145 146 147 148 149
    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
150
        this.selectiontype = new BehaviorSubject(true);
151 152
        this.selectionCClass = new BehaviorSubject(false);
        this.retrievedStudent = new BehaviorSubject(false);
Open Source Developer's avatar
trans  
Open Source Developer committed
153
        this.School$ = new BehaviorSubject([{}]);
Open Source Developer's avatar
Open Source Developer committed
154 155 156
        this.modalTitle =  new BehaviorSubject("");
        this.modalText =  new BehaviorSubject("");
        this.modalHeader =  new BehaviorSubject("");
157 158 159 160
        this.formGroup = this.fb.group({
            tomeas: ['', []],
            taxi: ['', []],
            specialit: ['', []],
Open Source Developer's avatar
Open Source Developer committed
161
            capacity: ['', [Validators.pattern(VALID_CAPACITY_PATTERN),Validators.required, maxValue(99)]],
Open Source Developer's avatar
trans  
Open Source Developer committed
162 163 164 165
            });

    }

Open Source Developer's avatar
Open Source Developer committed
166 167 168 169

    


Open Source Developer's avatar
trans  
Open Source Developer committed
170 171 172 173 174 175 176 177 178 179 180 181
    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 {
182

Open Source Developer's avatar
Open Source Developer committed
183 184
    }

185
    ngOnDestroy() {
Open Source Developer's avatar
Open Source Developer committed
186 187 188 189 190 191 192
        if (this.StudentSelectedSub)
            this.StudentSelectedSub.unsubscribe();
        if (this.StudentSelectedSpecialSub)
            this.StudentSelectedSpecialSub.unsubscribe();
        if (this.selectionBClass)
            this.selectionBClass.unsubscribe();
        if (this.selectionCClass)
193
            this.selectionCClass.unsubscribe();
Open Source Developer's avatar
trans  
Open Source Developer committed
194 195 196
        if (this.classCapacitySub)
            this.classCapacitySub.unsubscribe();
        if (this.retrievedStudent)
197
            this.retrievedStudent.unsubscribe();
Open Source Developer's avatar
Open Source Developer committed
198
    }
199

Open Source Developer's avatar
Open Source Developer committed
200
    ngOnInit() {
Open Source Developer's avatar
trans  
Open Source Developer committed
201
        (<any>$('#capacitysaved')).appendTo("body");
Open Source Developer's avatar
Open Source Developer committed
202
        (<any>$('#checksaved')).appendTo("body");
Open Source Developer's avatar
trans  
Open Source Developer committed
203
        this.retrievedStudent.next(false);
Open Source Developer's avatar
trans  
Open Source Developer committed
204

Open Source Developer's avatar
Open Source Developer committed
205
            this.SchoolSub = this._hds.gettypeofschool().subscribe(x => {
Open Source Developer's avatar
trans  
Open Source Developer committed
206
                  this.School$.next(x);                 
Open Source Developer's avatar
Open Source Developer committed
207 208 209 210 211
                  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
212 213 214 215 216 217 218 219

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

Open Source Developer's avatar
Open Source Developer committed
220 221 222
    }


223 224 225 226 227 228 229 230
    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
231
                capacity: '',
232 233
            });
            console.log("a class");
Open Source Developer's avatar
Open Source Developer committed
234
            this.classCapacitySub = this._hds.getCapacityPerSchool(this.formGroup.value.taxi, 0, 0).subscribe(data => {
235 236
                this.classCapacity$.next(data);
                this.retrievedStudent.next(true);
Open Source Developer's avatar
trans  
Open Source Developer committed
237 238 239 240
                this.formGroup.patchValue({
                capacity : data[0].capacity,
            });
            
241 242 243 244 245 246
            },
                error => {
                    this.classCapacity$.next([{}]);
                    console.log("Error Getting Capacity");
                },
                () => console.log("Getting Capacity"));
Open Source Developer's avatar
Open Source Developer committed
247

248 249 250
        }
        else if (txop.value === "2") {
            this.formGroup.patchValue({
Open Source Developer's avatar
trans  
Open Source Developer committed
251
                tomeas: '',
252
                specialit: '',
Open Source Developer's avatar
trans  
Open Source Developer committed
253
                capacity: '',
254 255 256 257
            });
            this.selectionBClass.next(true);
            this.selectionCClass.next(false);
            this.StudentSelected$ = new BehaviorSubject([{}]);
Open Source Developer's avatar
Open Source Developer committed
258
            this.StudentSelectedSub = this._hds.getSectorPerSchool().subscribe(this.StudentSelected$);
259
        }
Open Source Developer's avatar
Open Source Developer committed
260
        else if (txop.value === "3" || txop.value === "4" ) {
Open Source Developer's avatar
trans  
Open Source Developer committed
261 262 263 264 265 266
            this.formGroup.patchValue({
                tomeas: '',
                specialit: '',
                capacity: '',
            });

267 268 269 270 271
            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
272
                this.StudentSelectedSpecialSub = this._hds.getSpecialityPerSchool( sectorint).subscribe(this.StudentSelectedSpecial$);
Open Source Developer's avatar
Open Source Developer committed
273
            }
274 275 276
            this.selectionBClass.next(true);
            this.selectionCClass.next(true);
            this.StudentSelected$ = new BehaviorSubject([{}]);
Open Source Developer's avatar
Open Source Developer committed
277
            this.StudentSelectedSub = this._hds.getSectorPerSchool().subscribe(this.StudentSelected$);
278 279 280 281 282
        }




Open Source Developer's avatar
Open Source Developer committed
283 284 285
    }


286
    checkbclass(tmop, txop) {
Open Source Developer's avatar
Open Source Developer committed
287

Open Source Developer's avatar
trans  
Open Source Developer committed
288
        
Open Source Developer's avatar
Open Source Developer committed
289
        var sectorint = +this.formGroup.value.tomeas;
Open Source Developer's avatar
Open Source Developer committed
290
        console.log(tmop, txop, "tomeas!!!!");
291 292
        if (txop.value === "2") {
            console.log("b class");
Open Source Developer's avatar
Open Source Developer committed
293
            this.classCapacitySub = this._hds.getCapacityPerSchool(this.formGroup.value.taxi, sectorint, 0).subscribe(data => {
294 295
                this.classCapacity$.next(data);
                this.retrievedStudent.next(true);
Open Source Developer's avatar
trans  
Open Source Developer committed
296 297 298
                this.formGroup.patchValue({
                capacity : data[0].capacity,
            });
299 300 301 302 303 304
            },
                error => {
                    this.classCapacity$.next([{}]);
                    console.log("Error Getting Capacity");
                },
                () => console.log("Getting Capacity"));
Open Source Developer's avatar
trans  
Open Source Developer committed
305
        }
Open Source Developer's avatar
Open Source Developer committed
306
        if (txop.value === "3" || txop.value === "4") {
Open Source Developer's avatar
Open Source Developer committed
307
            this.StudentSelectedSpecial$ = new BehaviorSubject([{}]);
Open Source Developer's avatar
Open Source Developer committed
308
            this.StudentSelectedSpecialSub = this._hds.getSpecialityPerSchool(sectorint).subscribe(this.StudentSelectedSpecial$);
309

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

Open Source Developer's avatar
Open Source Developer committed
311
        }
Open Source Developer's avatar
trans  
Open Source Developer committed
312 313 314

    }

315
    checkcclass(tmop, txop, spop) {
Open Source Developer's avatar
trans  
Open Source Developer committed
316
        
317
        var sectorint = +this.formGroup.value.tomeas;
Open Source Developer's avatar
trans  
Open Source Developer committed
318
        var specialint = +this.formGroup.value.specialit;
319

Open Source Developer's avatar
Open Source Developer committed
320
        if (txop.value === "3" || txop.value === "4") {
321
            console.log("c class");
Open Source Developer's avatar
trans  
Open Source Developer committed
322
            console.log(sectorint, specialint, "cclass")
Open Source Developer's avatar
Open Source Developer committed
323
            this.classCapacitySub = this._hds.getCapacityPerSchool(this.formGroup.value.taxi, sectorint, specialint).subscribe(data => {
324 325
                this.classCapacity$.next(data);
                this.retrievedStudent.next(true);
Open Source Developer's avatar
trans  
Open Source Developer committed
326
                
Open Source Developer's avatar
trans  
Open Source Developer committed
327 328 329
               this.formGroup.patchValue({
                capacity : data[0].capacity,
            });
330 331 332 333 334 335
            },
                error => {
                    this.classCapacity$.next([{}]);
                    console.log("Error Getting Capacity");
                },
                () => console.log("Getting Capacity"));
Open Source Developer's avatar
trans  
Open Source Developer committed
336 337 338

        }

Open Source Developer's avatar
Open Source Developer committed
339 340 341
    }


342
    saveCapacity() {
Open Source Developer's avatar
Open Source Developer committed
343

Open Source Developer's avatar
Open Source Developer committed
344
        var taxi = +this.formGroup.value.taxi;
345 346
        var tomeas = +this.formGroup.value.tomeas;
        var specialit = +this.formGroup.value.specialit;
Open Source Developer's avatar
Open Source Developer committed
347 348 349 350
        var capc = +this.formGroup.value.capacity;
        

         if ((taxi === 2 && tomeas === 0) || (taxi === 3  && tomeas === 0 ) || (taxi ===3  && specialit === 0 ) 
Open Source Developer's avatar
Open Source Developer committed
351 352 353
             || (taxi === 4  && tomeas === 0 ) || (taxi ===4  && specialit === 0 ) || (taxi = 0) || (this.formGroup.invalid)

             )
Open Source Developer's avatar
Open Source Developer committed
354 355 356 357
          {
              this.showModal("#checksaved");
          } else 
        {
Open Source Developer's avatar
trans  
Open Source Developer committed
358
        
Open Source Developer's avatar
Open Source Developer committed
359
         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
360 361 362 363 364
                 },
                error => {
                    
                    console.log("Error Saving Capacity");
                },
Open Source Developer's avatar
Open Source Developer committed
365 366
                () =>{
                 console.log("Saved Capacity");
Open Source Developer's avatar
trans  
Open Source Developer committed
367
                 this.showModal("#capacitysaved");
Open Source Developer's avatar
Open Source Developer committed
368
                 });
Open Source Developer's avatar
Open Source Developer committed
369
         }
Open Source Developer's avatar
Open Source Developer committed
370
                 
Open Source Developer's avatar
Open Source Developer committed
371

372
    }
Open Source Developer's avatar
Open Source Developer committed
373

Open Source Developer's avatar
Open Source Developer committed
374
 
Open Source Developer's avatar
trans  
Open Source Developer committed
375

376
}