<link href="<?php echo base_url('assets/css/admin-panel.css'); ?>" rel="stylesheet">

<style>
	.aoust-muted { color: rgba(33, 37, 41, 0.7); }
	.aoust-table-actions .btn { padding-inline: 0.75rem; }
	.btn-ca-success { color: #fff !important; }
	.btn-ca-success:hover, .btn-ca-success:focus { color: #fff !important; }
</style>

<div class="care-admin" id="aoustVisaCrud">
	<div class="ca-hero">
		<div class="ca-hero-inner row align-items-center g-3">
			<div class="col-lg-8">
				<p class="ca-kicker">AOUST portal</p>
				<h1 class="ca-title"><?php echo isset($page_title) ? htmlspecialchars($page_title) : 'AOUST — Program & Visa Info'; ?></h1>
				<p class="ca-sub mb-0">Firestore collection: <strong>VisaPrograms</strong> · Structured sections</p>
			</div>
			<div class="col-lg-4">
				<div class="ca-nav justify-content-lg-end">
					<a class="btn btn-light" href="<?php echo base_url('admin/aoust'); ?>">Dashboard</a>
					<a class="btn btn-light" href="<?php echo base_url('aoust_courses'); ?>">Courses</a>
					<a class="btn btn-primary" href="<?php echo base_url('aoust_visa'); ?>">Visa Info</a>
				</div>
			</div>
		</div>
	</div>

	<div class="ca-card mb-4">
		<div class="ca-card-header d-flex flex-column flex-lg-row align-items-lg-center justify-content-lg-between gap-2">
			<div>
				<h5 class="ca-card-title mb-1"><i class="fa-solid fa-passport me-2 text-success"></i>Program &amp; Visa Info</h5>
				<p class="mb-0">Country-based program overview + eligibility + working conditions + insurance benefits.</p>
			</div>
			<div class="d-flex align-items-stretch gap-2 flex-wrap justify-content-lg-end">
				<span class="badge rounded-pill ca-pill bg-light text-dark border">Total: <?php echo isset($rows) ? (int) count($rows) : 0; ?></span>
				<a class="btn btn-ca-success" href="<?php echo base_url('aoust_visa/create'); ?>">
					<i class="fa-solid fa-plus me-2"></i>Add record
				</a>
			</div>
		</div>

		<div class="ca-card-body p-0">
			<div class="ca-table-wrap border-0 rounded-0 border-top">
				<table class="table ca-table table-striped mb-0 align-middle">
					<thead>
						<tr>
							<th>Country</th>
							<th>Continent</th>
							<th>Resource</th>
							<th>Visa type</th>
							<th>Age limit</th>
							<th>Weekly hours</th>
							<th class="text-end">Actions</th>
						</tr>
					</thead>
					<tbody>
					<?php if (!empty($rows)): ?>
						<?php foreach ($rows as $r): ?>
							<?php
								$id = isset($r['id']) ? $r['id'] : '';
								$country = isset($r['countryName']) ? $r['countryName'] : '';
								$continent = isset($r['continent']) ? $r['continent'] : '';
								$resLink = isset($r['resourceLink']) ? trim((string) $r['resourceLink']) : '';
								$elig = isset($r['visa_eligibility']) && is_array($r['visa_eligibility']) ? $r['visa_eligibility'] : array();
								$work = isset($r['working_condition']) && is_array($r['working_condition']) ? $r['working_condition'] : array();
								$visaType = isset($elig['visa_type']) ? $elig['visa_type'] : '';
								$age = isset($elig['age_limit']) ? $elig['age_limit'] : '';
								$hours = isset($work['weekly_working_hour']) ? $work['weekly_working_hour'] : '';
							?>
							<tr>
								<td class="fw-semibold">
									<div class="min-w-0">
										<div class="text-truncate"><?php echo htmlspecialchars($country); ?></div>
										<div class="aoust-muted small">ID: <?php echo htmlspecialchars($id); ?></div>
									</div>
								</td>
								<td><?php echo htmlspecialchars($continent); ?></td>
								<td class="small">
									<?php if ($resLink !== ''): ?>
										<a href="<?php echo htmlspecialchars($resLink); ?>" target="_blank" rel="noopener noreferrer" class="text-truncate d-inline-block" style="max-width: 200px;"><?php echo htmlspecialchars($resLink); ?></a>
									<?php else: ?>
										<span class="aoust-muted">—</span>
									<?php endif; ?>
								</td>
								<td><?php echo htmlspecialchars($visaType); ?></td>
								<td><?php echo htmlspecialchars($age); ?></td>
								<td><?php echo htmlspecialchars($hours); ?></td>
								<td class="text-end">
									<div class="btn-group aoust-table-actions" role="group">
										<a class="btn btn-sm btn-light border" href="<?php echo base_url('aoust_visa/edit/' . rawurlencode($id)); ?>" title="Edit">
											<i class="fa-regular fa-pen-to-square"></i>
										</a>
										<a class="btn btn-sm btn-outline-danger" href="<?php echo base_url('aoust_visa/delete/' . rawurlencode($id)); ?>" onclick="return confirm('Delete this record?');" title="Delete">
											<i class="fa-regular fa-trash-can"></i>
										</a>
									</div>
								</td>
							</tr>
						<?php endforeach; ?>
					<?php else: ?>
						<tr>
							<td colspan="7" class="p-4 text-center">
								<div class="mb-2"><i class="fa-regular fa-face-frown fs-2 text-muted"></i></div>
								<div class="fw-semibold">No records yet</div>
								<div class="aoust-muted small">Click “Add record” to create your first one.</div>
							</td>
						</tr>
					<?php endif; ?>
					</tbody>
				</table>
			</div>
		</div>
	</div>
</div>

