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

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

<?php
	$r = isset($row) && is_array($row) ? $row : array();
	$mode = isset($mode) ? $mode : 'create';
	$is_edit = ($mode === 'edit');
	$id = isset($r['id']) ? $r['id'] : '';
	$action = $is_edit ? base_url('aoust_visa/update/' . rawurlencode($id)) : base_url('aoust_visa/store');
	$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();
	$ins = isset($r['insurance_benefit']) && is_array($r['insurance_benefit']) ? $r['insurance_benefit'] : array();
?>

<div class="care-admin" id="aoustVisaForm">
	<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) : ($is_edit ? 'Edit visa info' : 'Add visa info'); ?></h1>
				<p class="ca-sub mb-0">Structured sections: overview → eligibility → working conditions → insurance benefits.</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('aoust_visa'); ?>">Back to list</a>
				</div>
			</div>
		</div>
	</div>

	<div class="row g-3">
		<div class="col-lg-8">
			<div class="ca-card mb-4">
				<div class="ca-card-header">
					<h5 class="ca-card-title mb-1"><i class="fa-solid fa-passport me-2 text-success"></i><?php echo $is_edit ? 'Edit record' : 'Add record'; ?></h5>
					<p class="mb-0">Saved to Firestore collection <strong>VisaPrograms</strong>.</p>
				</div>
				<div class="ca-card-body">
					<form method="post" action="<?php echo $action; ?>">
						<div class="mb-3">
							<label class="form-label">Country name</label>
							<input class="form-control" name="countryName" required value="<?php echo htmlspecialchars(isset($r['countryName']) ? $r['countryName'] : ''); ?>">
						</div>

						<div class="mb-3">
							<label class="form-label">Continent</label>
							<input class="form-control" name="continent" value="<?php echo htmlspecialchars(isset($r['continent']) ? $r['continent'] : ''); ?>">
						</div>

						<div class="mb-3">
							<label class="form-label">Resource link</label>
							<input class="form-control" type="url" name="resourceLink" placeholder="https://..." value="<?php echo htmlspecialchars(isset($r['resourceLink']) ? $r['resourceLink'] : ''); ?>">
							<div class="form-text aoust-muted">Stored as <strong>resourceLink</strong> on the VisaPrograms document.</div>
						</div>

						<div class="mb-4">
							<label class="form-label">Program overview</label>
							<textarea class="form-control" name="program_overview" rows="5" required><?php echo htmlspecialchars(isset($r['program_overview']) ? $r['program_overview'] : ''); ?></textarea>
						</div>

						<div class="ca-card mb-3">
							<div class="ca-card-header">
								<h6 class="mb-0 fw-bold">Visa eligibility</h6>
							</div>
							<div class="ca-card-body">
								<div class="row g-3">
									<div class="col-md-4">
										<label class="form-label">Visa type</label>
										<input class="form-control" name="visa_type" value="<?php echo htmlspecialchars(isset($elig['visa_type']) ? $elig['visa_type'] : ''); ?>">
									</div>
									<div class="col-md-4">
										<label class="form-label">Age limit</label>
										<input class="form-control" name="age_limit" value="<?php echo htmlspecialchars(isset($elig['age_limit']) ? $elig['age_limit'] : ''); ?>">
									</div>
									<div class="col-md-4">
										<label class="form-label">Language requirement</label>
										<input class="form-control" name="language_requirement" value="<?php echo htmlspecialchars(isset($elig['language_requirement']) ? $elig['language_requirement'] : ''); ?>">
									</div>
								</div>
							</div>
						</div>

						<div class="ca-card mb-3">
							<div class="ca-card-header">
								<h6 class="mb-0 fw-bold">Working condition</h6>
							</div>
							<div class="ca-card-body">
								<div class="row g-3">
									<div class="col-md-4">
										<label class="form-label">Weekly working hour</label>
										<input class="form-control" name="weekly_working_hour" value="<?php echo htmlspecialchars(isset($work['weekly_working_hour']) ? $work['weekly_working_hour'] : ''); ?>">
									</div>
									<div class="col-md-4">
										<label class="form-label">Pocket money / stipend</label>
										<input class="form-control" name="pocket_money_stipend" value="<?php echo htmlspecialchars(isset($work['pocket_money_stipend']) ? $work['pocket_money_stipend'] : ''); ?>">
									</div>
									<div class="col-md-4">
										<label class="form-label">Vacation day / holiday</label>
										<input class="form-control" name="vocationday_holiday" value="<?php echo htmlspecialchars(isset($work['vocationday_holiday']) ? $work['vocationday_holiday'] : ''); ?>">
									</div>
								</div>
							</div>
						</div>

						<div class="ca-card mb-3">
							<div class="ca-card-header">
								<h6 class="mb-0 fw-bold">Insurance benefit</h6>
							</div>
							<div class="ca-card-body">
								<div class="row g-3">
									<div class="col-md-4">
										<label class="form-label">Health insurance</label>
										<input class="form-control" name="health_insurance" value="<?php echo htmlspecialchars(isset($ins['health_insurance']) ? $ins['health_insurance'] : ''); ?>">
									</div>
									<div class="col-md-4">
										<label class="form-label">Accident insurance</label>
										<input class="form-control" name="accident_insurance" value="<?php echo htmlspecialchars(isset($ins['accident_insurance']) ? $ins['accident_insurance'] : ''); ?>">
									</div>
									<div class="col-md-4">
										<label class="form-label">Additional benefit</label>
										<input class="form-control" name="additional_benefit" value="<?php echo htmlspecialchars(isset($ins['additional_benefit']) ? $ins['additional_benefit'] : ''); ?>">
									</div>
								</div>
							</div>
						</div>

						<div class="mt-3">
							<label class="form-label">User ID</label>
							<input class="form-control" name="userId" value="<?php echo htmlspecialchars(isset($r['userId']) ? $r['userId'] : ''); ?>">
						</div>

						<div class="d-flex gap-2 mt-4">
							<a class="btn btn-light border" href="<?php echo base_url('aoust_visa'); ?>">Cancel</a>
							<button class="btn btn-primary" type="submit"><?php echo $is_edit ? 'Save changes' : 'Create record'; ?></button>
							<?php if ($is_edit): ?>
								<a class="btn btn-outline-danger ms-auto" href="<?php echo base_url('aoust_visa/delete/' . rawurlencode($id)); ?>" onclick="return confirm('Delete this record?');">Delete</a>
							<?php endif; ?>
						</div>
					</form>
				</div>
			</div>
		</div>

		<div class="col-lg-4">
			<div class="ca-card">
				<div class="ca-card-header">
					<h5 class="ca-card-title mb-1"><i class="fa-solid fa-circle-info me-2 text-success"></i>Notes</h5>
					<p class="mb-0">How data is stored.</p>
				</div>
				<div class="ca-card-body">
					<div class="aoust-muted small">Eligibility, working condition, and insurance benefit are stored as nested objects (maps) in Firestore. Resource link is a top-level string field.</div>
					<?php if ($is_edit): ?>
						<hr>
						<div class="aoust-muted small">Document ID</div>
						<div class="fw-semibold"><?php echo htmlspecialchars($id); ?></div>
					<?php endif; ?>
				</div>
			</div>
		</div>
	</div>
</div>

