<?php
// Default dates from session hijri
$dateAdmition = $_SESSION["hijri"] ?? "";
$toDateDefault = "";

if ($dateAdmition) {
	$dateParts = explode('-', $dateAdmition);
	if (count($dateParts) >= 1) {
		// YYYY-MM-DD -> increment YYYY for "to" date
		$toDateDefault = ($dateParts[0] + 1) . '-' . ($dateParts[1] ?? '01') . '-' . ($dateParts[2] ?? '01');
	}
}
?>

<div id="idCardStdWrapper" dir="rtl">
	<!-- Page Header -->
	<div class="mb-4 d-print-none">
		<h2 class="fw-bold mb-0 text-primary">
			<i class="bi bi-card-heading me-2"></i>طالب العلم کارڈ
		</h2>
		<p class="text-muted small">
			<?php echo htmlspecialchars(M_Name); ?>
		</p>
	</div>

	<!-- Search Section -->
	<div id="searchSection" class="card border-0 shadow-sm mb-4 bg-light d-print-none">
		<div class="card-body p-4">
			<form id="searchForm">
				<div class="row g-3 align-items-end">
					<div class="col-md-4">
						<label class="form-label fw-semibold">درجہ منتخب کریں</label>
						<?php $crud->darjaatCmb('darja', 'class="form-select form-select-lg" required'); ?>
					</div>
					<div class="col-md-3">
						<label class="form-label fw-semibold">سے</label>
						<input type="text" id="fromDat" name="fromDat"
							class="form-control form-control-lg text-start hijri-picker" dir="ltr"
							value="<?php echo $dateAdmition; ?>" readonly required>
					</div>
					<div class="col-md-3">
						<label class="form-label fw-semibold">تک</label>
						<input type="text" id="toDate" name="toDate"
							class="form-control form-control-lg text-start hijri-picker" dir="ltr"
							value="<?php echo $toDateDefault; ?>" readonly required>
					</div>
					<div class="col-md-2">
						<button type="submit" id="btnSearch" class="btn btn-primary btn-lg w-100">
							<i class="bi bi-search me-2"></i>تلاش کریں
						</button>
					</div>
				</div>
			</form>
		</div>
	</div>

	<!-- Actions Section -->
	<div id="resultsSection" class="d-none">
		<div class="d-flex justify-content-between align-items-center mb-4 d-print-none">
			<h4 class="mb-0 fw-bold text-success"><i class="bi bi-check-circle-fill me-2"></i>کارڈز تیار ہیں</h4>
			<button type="button" id="btnPrint" class="btn btn-success btn-lg">
				<i class="bi bi-printer me-2"></i>پرنٹ کریں
			</button>
		</div>

		<div id="idCardResults">
			<!-- Rendered cards will appear here via JS -->
		</div>
	</div>

	<script src="assets/js/id_card_std.js"></script>
</div>