<?php ob_start();
error_reporting(0);
ini_set('display_errors', 0);
// Redirect to installer if not configured
if (!file_exists(__DIR__ . '/install/installed.lock') && !isset($_GET['ignore_install'])) {
	header("Location: install/");
	exit;
}
session_start();
require_once('site/libs/classes/DbPathsArray.php');
require_once('site/libs/classes/App_DB.php');
$crud = new App_DB(DBU::$dba_user);

// Route to Front-Page if enabled
if (!isset($_GET['login'])) {
	$fp_setting = $crud->getRecordSet("SELECT setting_value FROM cms_settings WHERE setting_key = 'frontpage_enabled'");
	if (!empty($fp_setting) && $fp_setting[0]['setting_value'] == '1') {
		header("Location: web/");
		exit;
	}
}

// Addressing PHPSESSID SameSite rejection
session_set_cookie_params([
	'lifetime' => 0,
	'path' => '/',
	'domain' => '', // Current domain
	'secure' => false, // Set to true if using HTTPS
	'httponly' => true,
	'samesite' => 'Lax'
]);
session_start(); ?>
<?php require_once('site/libs/classes/DbPathsArray.php'); ?>
<?php require_once('site/libs/classes/App_DB.php'); ?>
<?php require_once('site/classes/Hijri_GregorianConvert.php'); ?>
<?php
$hijri = new Hijri_GregorianConvert();
$crud = new App_DB(DBU::$dba_user);

$format = "YYYY/MM/DD";
$date = date("Y/m/d");
$yearHijri = $hijri->GregorianToHijri($date, $format);
// Format for HTML5 date input (YYYY-MM-DD)
$dateParts = explode('-', $yearHijri);
if (count($dateParts) === 3) {
	$yearHijri = $dateParts[2] . '-' . $dateParts[1] . '-' . $dateParts[0];
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Authorized Access | Madrasa Management System</title>

	<!-- Google Fonts: Inter -->
	<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">

	<!-- Bootstrap 5.3 CSS -->
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">

	<!-- SweetAlert2 CSS -->
	<link href="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.css" rel="stylesheet">

	<!-- Hijri Calendar Styles -->
	<link rel="stylesheet" href="site/assets/js/libs/calendars/jquery.calendars.picker.css">

	<style>
		:root {
			--primary-color: #2c3e50;
			--secondary-color: #34495e;
			--accent-color: #3498db;
			--bg-gradient: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
			--premium-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
		}

		body {
			font-family: 'Inter', sans-serif;
			background: var(--bg-gradient);
			min-height: 100vh;
			display: flex;
			align-items: center;
			justify-content: center;
			margin: 0;
			color: var(--primary-color);
		}

		.login-card {
			background: #ffffff;
			border: none;
			border-radius: 20px;
			box-shadow: var(--premium-shadow);
			width: 100%;
			max-width: 450px;
			padding: 2.5rem;
			transition: transform 0.3s ease;
		}

		.login-card:hover {
			transform: translateY(-5px);
		}

		.login-header {
			text-align: center;
			margin-bottom: 2rem;
		}

		.login-header h2 {
			font-weight: 700;
			font-size: 1.75rem;
			color: var(--primary-color);
			margin-bottom: 0.5rem;
		}

		.login-header p {
			color: #7f8c8d;
			font-size: 0.95rem;
		}

		.form-label {
			font-weight: 500;
			font-size: 0.9rem;
			color: var(--secondary-color);
			margin-bottom: 0.5rem;
		}

		.form-control {
			border: 2px solid #edf2f7;
			border-radius: 10px;
			padding: 0.75rem 1rem;
			font-size: 0.95rem;
			transition: all 0.2s ease;
		}

		.form-control:focus {
			border-color: var(--accent-color);
			box-shadow: 0 0 0 4px rgba(52, 152, 219, 0.1);
		}

		.btn-login {
			background: var(--primary-color);
			border: none;
			border-radius: 10px;
			padding: 0.8rem;
			font-weight: 600;
			font-size: 1rem;
			color: #fff;
			width: 100%;
			margin-top: 1.5rem;
			transition: all 0.3s ease;
		}

		.btn-login:hover {
			background: var(--secondary-color);
			transform: scale(1.02);
		}

		#login-error-container {
			display: none;
			margin-top: 0.5rem;
			text-align: center;
		}

		.footer-text {
			text-align: center;
			margin-top: 2rem;
			font-size: 0.85rem;
			color: #95a5a6;
		}
	</style>
</head>

<body>

	<div class="login-card">
		<div class="login-header">
			<h2>ADMIN LOGIN</h2>
			<p>Madrasa Management System</p>
		</div>

		<form id="loginForm">
			<div class="mb-3">
				<label for="admin" class="form-label">Administrator ID</label>
				<input type="text" name="admin" id="admin" class="form-control" placeholder="Enter your ID" autofocus
					required>
			</div>

			<div class="mb-3">
				<label for="userkey" class="form-label">Password</label>
				<input type="password" name="userkey" id="userkey" class="form-control" placeholder="••••••••" required>
			</div>

			<div class="mb-3">
				<label for="dat" class="form-label">System Date (Hijri)</label>
				<input type="text" name="dat" id="dat" class="form-control hijri-picker"
					value="<?php echo ($yearHijri); ?>" readonly required>
			</div>

			<div id="login-error-container"></div>

			<button type="submit" class="btn-login" id="loginBtn">LOGIN TO SYSTEM</button>
		</form>

		<div class="footer-text">
			2012 &copy; <?php echo date('Y'); ?> ESDN Services. All Rights Reserved.
		</div>
	</div>

	<!-- jQuery 3.7.1 -->
	<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
	<!-- Bootstrap 5.3 JS Bundle -->
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
	<!-- SweetAlert2 JS -->
	<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

	<!-- Custom Login JS -->
	<script src="site/assets/js/login.js"></script>

	<!-- Hijri Calendar Libraries -->
	<script src="site/assets/js/libs/calendars/jquery.plugin.js"></script>
	<script src="site/assets/js/libs/calendars/jquery.calendars.js"></script>
	<script src="site/assets/js/libs/calendars/jquery.calendars.plus.js"></script>
	<script src="site/assets/js/libs/calendars/jquery.calendars.islamic.js"></script>
	<script src="site/assets/js/libs/calendars/jquery.calendars.picker.js"></script>
	<script src="site/assets/js/libs/calendars/jquery.calendars.picker-ur.js"></script>

	<!-- Initialize Hijri Calendar -->
	<script>
		$(document).ready(function () {
			if (typeof $.calendars !== 'undefined') {
				var calendar = $.calendars.instance('islamic', 'ur');
				$('.hijri-picker').calendarsPicker({
					calendar: calendar,
					dateFormat: 'yyyy-mm-dd',
					showAnim: 'fadeIn',
					showOnFocus: true
				});
			}
		});
	</script>

</body>

</html>
<?php ob_flush(); ?>