<?php include 'config.php'; ?>
<?php
// login.php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $ad_no = $_POST['ad_no'];
    $dob = $_POST['dob']; // Changed from classes to dob

    // checking if field is empty
    if($ad_no == "") {
        $extra="index.php";
        $host  = $_SERVER['HTTP_HOST'];
        $uri  = rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
        echo "<script type='text/javascript'> document.location = 'https://$host$uri/$extra'; </script>";
        $_SESSION['errmsg']="Admission number field is empty";
        exit();
    }

    if($dob == "") { // Changed from classes to dob
        $extra="index.php";
        $host  = $_SERVER['HTTP_HOST'];
        $uri  = rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
        echo "<script type='text/javascript'> document.location = 'https://$host$uri/$extra'; </script>";
        $_SESSION['errmsg']="Date of birth field is empty";
        exit();
    }

    // Query to check admission number and date of birth
    $query = mysqli_query($conn, "SELECT * FROM students WHERE Admission_num='$ad_no' AND dob='$dob'");
    $num = mysqli_fetch_array($query);

    if($num > 0) {
        $extra = "profile.php";
        $_SESSION['login'] = $num['Admission_num'];
        $_SESSION['id'] = $num['id'];
        $_SESSION['username'] = $num['name'];
        $uip = $_SERVER['REMOTE_ADDR'];
        $status = 1;
        $host = $_SERVER['HTTP_HOST'];
        $uri = rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
        echo "<script type='text/javascript'> document.location = 'https://$host$uri/$extra'; </script>";
        exit();
    } else {
        echo "<script type='text/javascript'> document.location = 'index.php'; </script>";
        $_SESSION['errmsg'] = "Invalid Admission number or date of birth";
        exit();
    }
}
?>

<!doctype html>
<html lang="en">
<head>
	<!-- Required meta tags -->
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<!--favicon-->
	<link rel="icon" href="assets/images/favicon-32x32.png" type="image/png" />
	<!--plugins-->
	<link href="assets/plugins/simplebar/css/simplebar.css" rel="stylesheet" />
	<link href="assets/plugins/perfect-scrollbar/css/perfect-scrollbar.css" rel="stylesheet" />
	<link href="assets/plugins/metismenu/css/metisMenu.min.css" rel="stylesheet" />
	<!-- loader-->
	<link href="assets/css/pace.min.css" rel="stylesheet" />
	<script src="assets/js/pace.min.js"></script>
	<!-- Bootstrap CSS -->
	<link href="assets/css/bootstrap.min.css" rel="stylesheet">
	<link href="assets/css/bootstrap-extended.css" rel="stylesheet">
	<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&amp;display=swap" rel="stylesheet">
	<link href="assets/css/app.css" rel="stylesheet">
	<link href="assets/css/icons.css" rel="stylesheet">
	<title>Student Result</title>
</head>

<body class="bg-login" style="padding: 2%;">
	<!--wrapper-->
	<div class="wrapper">
		<div class="section-authentication-signin d-flex align-items-center justify-content-center my-5 my-lg-0">
			<div class="container-fluid">
				<div class="row row-cols-1 row-cols-lg-2 row-cols-xl-3">
					<div class="col mx-auto">
						<div class="mb-4 text-center">
							<a href="index.php"> <img src="assets/images/logo-img.png" width="180" alt="" /> </a>
						</div>
						<div class="card">
							<div class="card-body">
								<div class="border p-4 rounded">
									<div class="col-md-12 col-sm-12">
										<div class="section-title wow fadeInUp" data-wow-delay="0.1s" style="padding-top: 10px; text-align: center;">
											<span style="color:red;font-size: 20px;">
												<?php 
												if(isset($_SESSION['errmsg']) && $_SESSION['errmsg']){
													echo htmlentities($_SESSION['errmsg']);
													$_SESSION['errmsg'] = "";
												}
												?>
											</span>
											<span style="color:green;font-size: 20px;">
												<?php 
												if(isset($_SESSION['mess']) && $_SESSION['mess']){
													echo htmlentities($_SESSION['mess']);
													$_SESSION['mess'] = "";
												}
												?>
											</span>
										</div>
									</div>
									<div class="text-center">
										<h3 class="">Sign in</h3>
									</div>
									
									<div class="form-body">
										<form class="row g-3" action="" method="post">
											<div class="col-12">
												<label for="inputEmailAddress" class="form-label">Admission Number</label>
												<input type="text" name="ad_no" class="form-control" placeholder="Admission Number">
											</div>
											<div class="col-12">
												<label for="inputDOB" class="form-label">Date of Birth</label>
												<input type="date" name="dob" class="form-control" id="inputDOB">
											</div>
											<div class="col-md-12">
												<div class="form-check form-switch">
													<input class="form-check-input" type="checkbox" id="flexSwitchCheckChecked" checked>
													<label class="form-check-label" for="flexSwitchCheckChecked">Remember Me</label>
												</div>
											</div>
											
											<div class="col-12">
												<div class="d-grid">
													<button type="submit" class="btn btn-primary"><i class="bx bxs-lock-open"></i>Sign in</button>
												</div>
											</div>
										</form>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
				<!--end row-->
			</div>
		</div>
	</div>
	<!--end wrapper-->
	<!-- Bootstrap JS -->
	<script src="assets/js/bootstrap.bundle.min.js"></script>
	<!--plugins-->
	<script src="assets/js/jquery.min.js"></script>
	<script src="assets/plugins/simplebar/js/simplebar.min.js"></script>
	<script src="assets/plugins/metismenu/js/metisMenu.min.js"></script>
	<script src="assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
	<!--app JS-->
	<script src="assets/js/app.js"></script>
</body>
</html>