<?php
session_start();
error_reporting(0);
include '../admin/includes/database.php';
include '../admin/includes/functions.php';
$login= new Functions();
$teacher_url = $login->teacher_url();

if(!empty($_POST['username']) && !empty($_POST['password'])){
    $username = ($_POST['username']);
    $password = ($_POST['password']);

    //perform validations for email
    if(!$login->teacherLoginCheck($username, $password)){

        echo "<script>
              toastr['error']('Double check your details');
           </script>";

    }else{
        // All checks passed, process login
        $status = 'on';
        $sql = "SELECT * FROM teachers WHERE username =:username AND status =:on";
        $login->query($sql);
        $login->bind(":username", $_POST['username']);
        $login->bind(":on", $status);
        try{
            $result = $login->fetchSingle();
            $username = $result->username;
            $_SESSION['teacher'] = $username;

            if($result){
                echo "<script>
              toastr['success']('Login Successful. Redirecting to Account Dashboard...');
           </script><meta http-equiv='refresh' content='4; $teacher_url'>";

                echo  "<div class='spinner-border text-info text-center' role='status'>
                    <span class='sr-only'>Loading...</span>
                </div>";
            }else{
                echo "<script>
              toastr['error']('Sorry you cannot login. Please contact the school admin for further assistance');
           </script>";
            }
        }catch (PDOException $err){

            echo "<script>
              toastr['error']('An internal error occurred. Pls try again.');
           </script>";
        }

    }
}else{
    echo "<script>
              toastr['error']('Both fields are required.');
           </script>";
}



