import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

import { SchoolService } from '../../../services/school.service';

import { SchoolData } from '../../../structures/school-data';

import * as Globals from '../../../globals';

@Component({
  selector: 'app-edit-school-detail-page',
  templateUrl: './edit-school-detail-page.component.html',
  styleUrls: ['./edit-school-detail-page.component.scss']
})
export class EditSchoolDetailPageComponent implements OnInit {

  title = Globals.pages.editSchoolDetail.text;

  schoolData: SchoolData | null = null;

  targetAfterSubmit: string = Globals.pages.instructorProfile.path;

  constructor(private route: ActivatedRoute, private schoolService: SchoolService) { }

  ngOnInit(): void {
    var idString = this.route.snapshot.paramMap.get('id');
    if(idString != null) {
      var id = +idString;
      if(id != NaN && id != Infinity && id != -Infinity) {
        this.loadSelectedSchool(id);
      }
    }
  }

  async loadSelectedSchool(id: number): Promise<void> {
    var data = await this.schoolService.getSchoolById(id);
    if(data != undefined) {
      this.schoolData = data;
    } else {
      this.schoolData = null;
    }
  }

}
