<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

// Addressing PHPSESSID SameSite rejection
session_set_cookie_params([
	'lifetime' => 0,
	'path' => '/',
	'domain' => '', // Current domain
	'secure' => isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on',
	'httponly' => true,
	'samesite' => 'Lax'
]);
session_start();

require_once('libs/classes/DbPathsArray.php');
require_once('libs/classes/App_DB.php');
require_once('libs/classes/EnvLoader.php');
require_once('libs/classes/MyMethods.php');
require_once('classes/Hijri_GregorianConvert.php');

$hijri = new Hijri_GregorianConvert();
require_once('includes/security.php');
$crud = new App_DB(DBU::$dba_user);
$method = new MyMethods();

// Language handling
if (isset($_GET['lang'])) {
	$lang = $_GET['lang'] === 'en' ? 'en' : 'ur';
	setcookie('lang', $lang, time() + (86400 * 30), "/"); // 30 days
	$_SESSION['lang'] = $lang;
} elseif (isset($_COOKIE['lang'])) {
	$lang = $_COOKIE['lang'] === 'en' ? 'en' : 'ur';
} else {
	$lang = $_SESSION['lang'] ?? 'ur';
}
$is_rtl = ($lang === 'ur');
if ($lang == 'en') {
	$font_size = 'fs-5';
	$dbr_fs_sz = 'fs-6';
} else {
	$font_size = 'fs-3';
	$dbr_fs_sz = 'fs-4';
}
require_once("includes/configuration.php");
?>
<!DOCTYPE html>
<html lang="<?php echo $lang; ?>" dir="<?php echo $is_rtl ? 'rtl' : 'ltr'; ?>">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>:::
		<?php echo isset($_SESSION['hijri']) ? $_SESSION['hijri'] : ''; ?> :::
		<?php echo $is_rtl ? 'مدرسہ منیجیمنٹ سسٹم' : 'Madrasa Management System'; ?>
	</title>
	<link rel="icon" type="image/png" href="images/card_logo.png" />
	<!-- 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">
	<!-- Bootstrap Icons -->
	<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
	<!-- SweetAlert2 CSS -->
	<link href="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.css" rel="stylesheet">
	<!-- Legacy Styles -->
	<link rel="stylesheet" type="text/css" href="css/default.css" />
	<link rel="stylesheet" href="menu/menu.css" type="text/css" media="screen" />
	<!-- Core Scripts -->
	<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
	<script src="assets/js/functions.js" type="text/javascript"></script>
	<link rel="stylesheet" href="assets/css/main.css">
	<!-- Hijri Calendar Styles -->
	<link rel="stylesheet" href="assets/js/libs/calendars/jquery.calendars.picker.css">
</head>

<body>
	<header id="main-header" class="shadow-sm">
		<div class="container-fluid d-flex justify-content-between align-items-center">
			<div class="d-flex align-items-center gap-3">
				<i class="bi bi-mortarboard-fill fs-5 text-white"></i>
				<h1 class="header-title mb-0 py-1">
					<?php echo $is_rtl ? M_Name : 'Madrasa Management System'; ?>
				</h1>

			</div>

			<div class="d-flex align-items-center gap-3">
				<div class="dropdown">
					<button class="btn btn-sm btn-outline-light dropdown-toggle border-0 opacity-75 urdu-regular fs-4"
						type="button" data-bs-toggle="dropdown">
						<i class="bi bi-globe me-1 px-2"></i>
						<?php echo $is_rtl ? 'اردو' : 'English'; ?>
					</button>

					<ul class="dropdown-menu dropdown-menu-dark dropdown-menu-end shadow">
						<li><a class="dropdown-item urdu-regular fs-4 <?php echo $lang === 'ur' ? 'active' : ''; ?>"
								href="?lang=ur">اردو</a></li>
						<li><a class="dropdown-item urdu-regular fs-4 <?php echo $lang === 'en' ? 'active' : ''; ?>"
								href="?lang=en">English</a></li>
					</ul>
				</div>
				<div class="session-info d-none d-md-block" style="direction: <?php echo $is_rtl ? 'rtl' : 'ltr'; ?>;">
					<span class="opacity-75 me-2 px-2">
						<?php echo $is_rtl ? 'صارف' : 'User'; ?>:
						<?php echo htmlspecialchars($_SESSION['userid']); ?>
					</span>
					<span class="fw-bold fw-sm border-start border-light border-opacity-25 ps-2 ms-2 float-end">
						<?php echo htmlspecialchars($_SESSION['hijri']); ?>
					</span>
				</div>
			</div>
		</div>
	</header>

	<div class="navbar-custom">
		<?php require_once('includes/menuGeneral.php'); ?>
	</div>

	<div id="content-wrapper" class="container-fluid">
		<section id="content" class="p-4">
			<?php
			$cmd = "dashboard"; // Changed default to dashboard
			if (isset($_REQUEST['cmd']) && !empty($_REQUEST['cmd'])) {
				$cmd = trim(addslashes($_REQUEST['cmd']), '/');
			}

			if (file_exists("pages/" . $cmd . ".php")) {
				include("pages/" . $cmd . ".php");
			} else {
				include("pages/error.php");
			}
			?>
		</section>
	</div>

	<footer id="footer" class="mt-auto">
		<?php require_once("includes/footer.php"); ?>
	</footer>

	<!-- Bootstrap 5 Modal Container -->
	<?php echo $method->modalBox3(); ?>

	<!-- jQuery 3.7.1 already loaded in head -->
	<!-- 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>
	<!-- jQuery Mask Plugin -->
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.16/jquery.mask.min.js"></script>
	<!-- Dynamic Translator (Dashboard & Header Only) -->
	<script src="assets/js/dynamic_translator.js"></script>

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

	<!-- Initialize Hijri Calendar Globally -->
	<script>
		$(document).ready(function () {
			if (typeof $.calendars !== 'undefined') {
				var calendar = $.calendars.instance('islamic', 'ur');

				// Global initialization for all elements with .hijri-picker class
				// This handles dynamically added elements too
				$(document).on('focus', '.hijri-picker', function () {
					if (!$(this).hasClass('hasCalendarsPicker')) {
						$(this).calendarsPicker({
							calendar: calendar,
							dateFormat: 'yyyy-mm-dd',
							showAnim: 'fadeIn',
							showOnFocus: true
						});
					}
				});

				// Immediate initialization for elements already on the page
				$('.hijri-picker').calendarsPicker({
					calendar: calendar,
					dateFormat: 'yyyy-mm-dd',
					showAnim: 'fadeIn',
					showOnFocus: true
				});
			}
		});
	</script>
</body>

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