import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { IspitService } from '../services/ispit.service';
import { StudentService } from '../services/student.service';
import { Ispit } from '../models/ispit';

@Component({
  selector: 'app-komponenta3',
  templateUrl: './komponenta3.component.html',
  styleUrls: ['./komponenta3.component.css']
})
export class Komponenta3Component implements OnInit {

  constructor(private router: Router, private ispitServis: IspitService, private studentServis: StudentService){}
  
  ngOnInit(): void {
    this.ispisiTabelu = false;
    let ispitStr = localStorage.getItem("ispit");
    if(ispitStr!=null){
      this.poslednjiIspit = JSON.parse(ispitStr);
      this.poslednjiIspit.datum = new Date();

      this.studentServis.dohvatiStudentaPoIndeksu(this.poslednjiIspit.student.substring(0,4)+this.poslednjiIspit.student.substring(5,9)).subscribe(
        podaci=>{
          if(podaci!=null){
            this.ispisiTabelu = true;
            this.ispitServis.dodavanjeIspitaZaStudenta(this.poslednjiIspit).subscribe(
              podaci=>{
                this.ispitiZaStudenta = podaci;
              }
            )
          }
        }
      )
    }
  }

  poslednjiIspit: Ispit = new Ispit()
  ispisiTabelu: boolean = false;
  ispitiZaStudenta: Ispit[] = []

  nazad(){
    this.router.navigate(['']);
  }

}
