<?php
session_start();
include '../admin/includes/database.php';
include '../admin/includes/functions.php';

$login = new Functions();

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

    //Check If Username is valid
    if (!$login->officerloginCheck($username, $password)) {
        echo "<div class=\"pgn push-on-sidebar-open pgn-simple\" style=\"width: 380px; margin-bottom: 10px;\"><div class=\"alert alert-danger\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\"><span aria-hidden=\"true\">×</span>
        <span class=\"sr-only\">Close</span></button>Incorrect Username and Password</div></div>";
    }else {
        // Log User In
        $status = 'on';
        $sql = "SELECT * FROM officer 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['officer'] = $username;
            $redirect = $login->main_url()."/conference";
            echo "<div class=\"pgn push-on-sidebar-open pgn-simple\" style=\"width: 380px; margin-bottom: 10px;\"><div class=\"alert alert-success\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\"><span aria-hidden=\"true\">×</span>
<span class=\"sr-only\">Close</span></button>Login Successful, Redirecting to Dashboard</div></div><meta http-equiv='refresh' content='3; $redirect'>";
        }catch (PDOException $err) {
            $error = $err->getMessage();
            echo "<script>
$(function () { //ready
          toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
          toastr.error('$error');
      });</script>";
        }
    }
}else {
    echo "<script>
$(function () { //ready
          toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
          toastr.error('Both Username and Password are Required');
      });</script>";
}
