director-classcapacity.ts 13.5 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

Open Source Developer's avatar
Open Source Developer committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    <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
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    <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
101 102 103
    


Open Source Developer's avatar
Open Source Developer committed
104 105 106
   `
})

107
@Injectable() export default class DirectorClassCapacity implements OnInit, OnDestroy {
Open Source Developer's avatar
Open Source Developer committed
108 109 110 111 112 113

    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
114
    private saveCapacitySub: Subscription;
Open Source Developer's avatar
Open Source Developer committed
115
    private selectionBClass: BehaviorSubject<boolean>;
Open Source Developer's avatar
Open Source Developer committed
116
    private selectiontype: BehaviorSubject<boolean>;
Open Source Developer's avatar
Open Source Developer committed
117
    private selectionCClass: BehaviorSubject<boolean>;
Open Source Developer's avatar
trans  
Open Source Developer committed
118 119 120
    private School$: BehaviorSubject<any>;
    private SchoolSub: Subscription;
    private SchoolId;
Open Source Developer's avatar
Open Source Developer committed
121
    private currentclass: Number;
Open Source Developer's avatar
trans  
Open Source Developer committed
122 123 124
    private classCapacity$: BehaviorSubject<any>;
    private classCapacitySub: Subscription;
    private retrievedStudent: BehaviorSubject<boolean>;
Open Source Developer's avatar
Open Source Developer committed
125 126 127
    private modalTitle: BehaviorSubject<string>;
    private modalText: BehaviorSubject<string>;
    private modalHeader: BehaviorSubject<string>;
Open Source Developer's avatar
trans  
Open Source Developer committed
128
    
129 130


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

    }

    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 {
168

Open Source Developer's avatar
Open Source Developer committed
169 170
    }

171
    ngOnDestroy() {
Open Source Developer's avatar
Open Source Developer committed
172 173 174 175 176 177 178
        if (this.StudentSelectedSub)
            this.StudentSelectedSub.unsubscribe();
        if (this.StudentSelectedSpecialSub)
            this.StudentSelectedSpecialSub.unsubscribe();
        if (this.selectionBClass)
            this.selectionBClass.unsubscribe();
        if (this.selectionCClass)
179
            this.selectionCClass.unsubscribe();
Open Source Developer's avatar
trans  
Open Source Developer committed
180 181 182
        if (this.classCapacitySub)
            this.classCapacitySub.unsubscribe();
        if (this.retrievedStudent)
183
            this.retrievedStudent.unsubscribe();
Open Source Developer's avatar
Open Source Developer committed
184
    }
185

Open Source Developer's avatar
Open Source Developer committed
186
    ngOnInit() {
Open Source Developer's avatar
trans  
Open Source Developer committed
187
        (<any>$('#capacitysaved')).appendTo("body");
Open Source Developer's avatar
Open Source Developer committed
188
        (<any>$('#checksaved')).appendTo("body");
Open Source Developer's avatar
trans  
Open Source Developer committed
189
        this.retrievedStudent.next(false);
Open Source Developer's avatar
trans  
Open Source Developer committed
190

Open Source Developer's avatar
Open Source Developer committed
191
            this.SchoolSub = this._hds.gettypeofschool().subscribe(x => {
Open Source Developer's avatar
trans  
Open Source Developer committed
192
                  this.School$.next(x);                 
Open Source Developer's avatar
Open Source Developer committed
193 194 195 196 197
                  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
198 199 200 201 202 203 204 205

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

Open Source Developer's avatar
Open Source Developer committed
206 207 208
    }


209 210 211 212 213 214 215 216
    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
217
                capacity: '',
218 219
            });
            console.log("a class");
Open Source Developer's avatar
Open Source Developer committed
220
            this.classCapacitySub = this._hds.getCapacityPerSchool(this.formGroup.value.taxi, 0, 0).subscribe(data => {
221 222
                this.classCapacity$.next(data);
                this.retrievedStudent.next(true);
Open Source Developer's avatar
trans  
Open Source Developer committed
223 224 225 226
                this.formGroup.patchValue({
                capacity : data[0].capacity,
            });
            
227 228 229 230 231 232
            },
                error => {
                    this.classCapacity$.next([{}]);
                    console.log("Error Getting Capacity");
                },
                () => console.log("Getting Capacity"));
Open Source Developer's avatar
Open Source Developer committed
233

234 235 236
        }
        else if (txop.value === "2") {
            this.formGroup.patchValue({
Open Source Developer's avatar
trans  
Open Source Developer committed
237
                tomeas: '',
238
                specialit: '',
Open Source Developer's avatar
trans  
Open Source Developer committed
239
                capacity: '',
240 241 242 243
            });
            this.selectionBClass.next(true);
            this.selectionCClass.next(false);
            this.StudentSelected$ = new BehaviorSubject([{}]);
Open Source Developer's avatar
Open Source Developer committed
244
            this.StudentSelectedSub = this._hds.getSectorPerSchool().subscribe(this.StudentSelected$);
245
        }
Open Source Developer's avatar
Open Source Developer committed
246
        else if (txop.value === "3" || txop.value === "4" ) {
Open Source Developer's avatar
trans  
Open Source Developer committed
247 248 249 250 251 252
            this.formGroup.patchValue({
                tomeas: '',
                specialit: '',
                capacity: '',
            });

253 254 255 256 257
            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
258
                this.StudentSelectedSpecialSub = this._hds.getSpecialityPerSchool( sectorint).subscribe(this.StudentSelectedSpecial$);
Open Source Developer's avatar
Open Source Developer committed
259
            }
260 261 262
            this.selectionBClass.next(true);
            this.selectionCClass.next(true);
            this.StudentSelected$ = new BehaviorSubject([{}]);
Open Source Developer's avatar
Open Source Developer committed
263
            this.StudentSelectedSub = this._hds.getSectorPerSchool().subscribe(this.StudentSelected$);
264 265 266 267 268
        }




Open Source Developer's avatar
Open Source Developer committed
269 270 271
    }


272
    checkbclass(tmop, txop) {
Open Source Developer's avatar
Open Source Developer committed
273

Open Source Developer's avatar
trans  
Open Source Developer committed
274
        
Open Source Developer's avatar
Open Source Developer committed
275
        var sectorint = +this.formGroup.value.tomeas;
Open Source Developer's avatar
Open Source Developer committed
276
        console.log(tmop, txop, "tomeas!!!!");
277 278
        if (txop.value === "2") {
            console.log("b class");
Open Source Developer's avatar
Open Source Developer committed
279
            this.classCapacitySub = this._hds.getCapacityPerSchool(this.formGroup.value.taxi, sectorint, 0).subscribe(data => {
280 281
                this.classCapacity$.next(data);
                this.retrievedStudent.next(true);
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
        }
Open Source Developer's avatar
Open Source Developer committed
292
        if (txop.value === "3" || txop.value === "4") {
Open Source Developer's avatar
Open Source Developer committed
293
            this.StudentSelectedSpecial$ = new BehaviorSubject([{}]);
Open Source Developer's avatar
Open Source Developer committed
294
            this.StudentSelectedSpecialSub = this._hds.getSpecialityPerSchool(sectorint).subscribe(this.StudentSelectedSpecial$);
295

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

Open Source Developer's avatar
Open Source Developer committed
297
        }
Open Source Developer's avatar
trans  
Open Source Developer committed
298 299 300

    }

301
    checkcclass(tmop, txop, spop) {
Open Source Developer's avatar
trans  
Open Source Developer committed
302
        
303
        var sectorint = +this.formGroup.value.tomeas;
Open Source Developer's avatar
trans  
Open Source Developer committed
304
        var specialint = +this.formGroup.value.specialit;
305

Open Source Developer's avatar
Open Source Developer committed
306
        if (txop.value === "3" || txop.value === "4") {
307
            console.log("c class");
Open Source Developer's avatar
trans  
Open Source Developer committed
308
            console.log(sectorint, specialint, "cclass")
Open Source Developer's avatar
Open Source Developer committed
309
            this.classCapacitySub = this._hds.getCapacityPerSchool(this.formGroup.value.taxi, sectorint, specialint).subscribe(data => {
310 311
                this.classCapacity$.next(data);
                this.retrievedStudent.next(true);
Open Source Developer's avatar
trans  
Open Source Developer committed
312
                
Open Source Developer's avatar
trans  
Open Source Developer committed
313 314 315
               this.formGroup.patchValue({
                capacity : data[0].capacity,
            });
316 317 318 319 320 321
            },
                error => {
                    this.classCapacity$.next([{}]);
                    console.log("Error Getting Capacity");
                },
                () => console.log("Getting Capacity"));
Open Source Developer's avatar
trans  
Open Source Developer committed
322 323 324

        }

Open Source Developer's avatar
Open Source Developer committed
325 326 327 328 329
    }




330
    saveCapacity() {
Open Source Developer's avatar
Open Source Developer committed
331

Open Source Developer's avatar
Open Source Developer committed
332
        var taxi = +this.formGroup.value.taxi;
333 334
        var tomeas = +this.formGroup.value.tomeas;
        var specialit = +this.formGroup.value.specialit;
Open Source Developer's avatar
Open Source Developer committed
335 336 337 338 339 340 341 342 343
        var capc = +this.formGroup.value.capacity;
        

         if ((taxi === 2 && tomeas === 0) || (taxi === 3  && tomeas === 0 ) || (taxi ===3  && specialit === 0 ) 
             || (taxi === 4  && tomeas === 0 ) || (taxi ===4  && specialit === 0 ) || (taxi = 0) || (capc ===0))
          {
              this.showModal("#checksaved");
          } else 
        {
Open Source Developer's avatar
trans  
Open Source Developer committed
344
        
Open Source Developer's avatar
Open Source Developer committed
345
         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
346 347 348 349 350
                 },
                error => {
                    
                    console.log("Error Saving Capacity");
                },
Open Source Developer's avatar
Open Source Developer committed
351 352
                () =>{
                 console.log("Saved Capacity");
Open Source Developer's avatar
trans  
Open Source Developer committed
353
                 this.showModal("#capacitysaved");
Open Source Developer's avatar
Open Source Developer committed
354
                 });
Open Source Developer's avatar
Open Source Developer committed
355
         }
Open Source Developer's avatar
Open Source Developer committed
356
                 
Open Source Developer's avatar
Open Source Developer committed
357

358
    }
Open Source Developer's avatar
Open Source Developer committed
359

Open Source Developer's avatar
Open Source Developer committed
360
 
Open Source Developer's avatar
trans  
Open Source Developer committed
361

362
}