<div class="container-fluid py-4" dir="rtl">
	<div class="row justify-content-center">
		<div class="col-12 col-lg-8">
			<div class="card border-0 shadow-sm mb-4">
				<div class="card-header bg-danger bg-opacity-10 py-3 text-center border-0">
					<h5 class="card-title mb-0 text-danger fw-bold">
						<i class="bi bi-journal-x me-2"></i>مضامین مٹائیں
					</h5>
					<p class="text-muted small mb-0 mt-1">درجات سے مضامین کو حذف کرنے کے لیے درجہ منتخب کریں</p>
				</div>
				<div class="card-body p-4 border-bottom">
					<div class="row align-items-center g-3">
						<div class="col-md-3 text-md-end">
							<label class="form-label fw-bold mb-0">درجہ منتخب کریں:</label>
						</div>
						<div class="col-md-9">
							<?php
							$crud = new App_DB(DBU::$dba_user);
							$darjaSn = $_REQUEST['darjaSn'] ?? '';
							$crud->darjaatCmb('darja', 'class="form-select border-2" id="darja" required');
							?>
						</div>
					</div>
				</div>

				<?php if ($darjaSn): ?>
					<?php
					$sqlFetch = "SELECT s.sno, s.subjectName FROM subjects s WHERE s.darjaSno = :darjaSno ORDER BY s.sno ASC";
					$subjects = $crud->getRecordSet($sqlFetch, ['darjaSno' => $darjaSn]);
					$selectedDarjaName = $crud->getValue("SELECT darja FROM darjaat WHERE derjaCode = :code", ['code' => $darjaSn]);
					?>
					<div class="card-body p-0" id="subjectsListContainer">
						<div class="bg-light px-4 py-2 border-bottom d-flex justify-content-between align-items-center">
							<span class="fw-bold text-primary fs-5">منتخب درجہ:
								<?php echo htmlspecialchars($selectedDarjaName); ?></span>
							<span class="badge bg-primary rounded-pill"><?php echo count($subjects); ?> مضامین</span>
						</div>
						<div class="table-responsive">
							<table class="table table-hover align-middle mb-0" id="subjectsTable">
								<thead class="table-light">
									<tr>
										<th class="text-center" style="width: 50px;">#</th>
										<th>مضمون کا نام</th>
										<th class="text-end px-4" style="width: 150px;">کارروائی</th>
									</tr>
								</thead>
								<tbody>
									<?php if (empty($subjects)): ?>
										<tr>
											<td colspan="3" class="text-center text-muted py-4">اس درجہ میں کوئی مضمون موجود
												نہیں ہے</td>
										</tr>
									<?php else: ?>
										<?php $counter = 0;
										foreach ($subjects as $row):
											$counter++; ?>
											<tr>
												<td class="text-center text-muted small"><?php echo $counter; ?></td>
												<td class="fw-medium">
													<?php echo htmlspecialchars($row['subjectName']); ?>
												</td>
												<td class="text-end px-4">
													<button type="button" class="btn btn-outline-danger btn-sm btn-delete-subject"
														data-id="<?php echo $row['sno']; ?>"
														data-name="<?php echo htmlspecialchars($row['subjectName']); ?>"
														title="مضمون حذف کریں">
														<i class="bi bi-trash3-fill"></i> مٹائیں
													</button>
												</td>
											</tr>
										<?php endforeach; ?>
									<?php endif; ?>
								</tbody>
							</table>
						</div>
					</div>
				<?php endif; ?>
			</div>
		</div>
	</div>
</div>

<!-- Scripts -->
<script src="assets/js/delete_subject.js"></script>

<script>
	// Set initial value for darja dropdown if darjaSn is present
	$(document).ready(function () {
		const initialDarja = "<?php echo $darjaSn; ?>";
		if (initialDarja) {
			$('#darja').val(initialDarja);
		}
	});
</script>