import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormControl, Validators } from '@angular/forms';
import {MatSnackBar} from '@angular/material/snack-bar';

import { LoginService } from '../../../services/login.service';

import { CredentialsSchool } from '../../../structures/credentials-school';

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

@Component({
  selector: 'app-school-login-form',
  templateUrl: './school-login-form.component.html',
  styleUrls: ['./school-login-form.component.scss']
})
export class SchoolLoginFormComponent implements OnInit {

  model: CredentialsSchool = { email: "", password: "" };

  hidePassword: boolean = true;

  constructor(private loginService: LoginService, private snackBar: MatSnackBar, private router: Router) { }

  ngOnInit(): void {
  }

  logIn(): void {
    this.loginService.logInSchool({email: this.model.email, password: this.model.password}).subscribe(
      resp => {
        //this.afterLogIn(resp);
        if(resp.school?.ID != undefined && resp.school?.email != undefined) {
          this.snackBar.open('Přihlášení bylo úspěšné', 'x', {duration: Globals.snackbarSuccessTime, panelClass:'snackBarSuccess'}); 
          var page = Globals.getAfterLoginPage('skola',''); 
          if(page != undefined) {
            this.router.navigate([page.path]);
          } else {
            this.router.navigate([Globals.pages.mainSchool.path]);
          }
        } else {
          this.snackBar.open('Přihlášení bylo neúspěšné', 'x', {duration: Globals.snackbarErrorTime, panelClass:'snackBarError'});
        }
      },
      error => this.snackBar.open('Přihlášení bylo neúspěšné', 'x', {duration: Globals.snackbarErrorTime, panelClass:'snackBarError'})
    );
  }

}
