<?php
/**
 * Admission Form Page — Gmind Institute
 *
 * Admission form with student details and course/branch selection,
 * info sidebar, CSRF protection.
 *
 * Expected PHP variables:
 *   $courses    — array of course records (for dropdown)
 *   $branches   — array of branch records (for dropdown)
 *   $csrfToken  — CSRF token string
 *   $currentPage — string 'admission'
 *   $title      — page title
 *   $errors     — array of validation errors (optional)
 *   $old        — array of old input values (optional)
 */

$courses   = $courses   ?? [];
$branches  = $branches  ?? [];
$errors    = $errors    ?? [];
$old       = $old       ?? [];
$csrfToken = $csrfToken ?? '';
?>

<?php include __DIR__ . '/../layouts/header.php'; ?>

<!-- ============================================================
     Page Banner
     ============================================================ -->
<section class="page-banner" aria-label="Page heading">
    <div class="container">
        <h1>Admission Form</h1>
        <p>Take the first step towards a successful career in IT — apply now!</p>
        <nav class="breadcrumb" aria-label="Breadcrumb">
            <a href="<?= url('/') ?>">Home</a>
            <span aria-hidden="true">/</span>
            <span>Admission</span>
        </nav>
    </div>
</section>

<!-- ============================================================
     Admission Form Section
     ============================================================ -->
<section class="admission-section section-offwhite" aria-label="Admission form">
    <div class="container">
        <div class="admission-layout">
            <!-- Form -->
            <div class="admission-form-wrapper">
                <?php if (!empty($errors)): ?>
                <div class="alert alert-error" role="alert">
                    <strong>Please fix the following errors:</strong>
                    <ul>
                        <?php foreach ($errors as $error): ?>
                        <li><?= htmlspecialchars($error, ENT_QUOTES, 'UTF-8') ?></li>
                        <?php endforeach; ?>
                    </ul>
                </div>
                <?php endif; ?>

                <form method="POST" action="<?= url('/admission') ?>" class="admission-form" novalidate>
                    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8') ?>">

                    <!-- Personal Details -->
                    <h2 class="form-section-title">Personal Details</h2>
                    <div class="form-grid">
                        <div class="form-group">
                            <label for="full_name">Full Name <span class="required">*</span></label>
                            <input type="text" id="full_name" name="full_name" required placeholder="Enter your full name" value="<?= htmlspecialchars($old['full_name'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                        </div>
                        <div class="form-group">
                            <label for="email">Email Address <span class="required">*</span></label>
                            <input type="email" id="email" name="email" required placeholder="Enter your email" value="<?= htmlspecialchars($old['email'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                        </div>
                        <div class="form-group">
                            <label for="phone">Phone Number <span class="required">*</span></label>
                            <input type="tel" id="phone" name="phone" required placeholder="Enter your phone number" value="<?= htmlspecialchars($old['phone'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                        </div>
                        <div class="form-group">
                            <label for="dob">Date of Birth <span class="required">*</span></label>
                            <input type="date" id="dob" name="dob" required value="<?= htmlspecialchars($old['dob'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                        </div>
                        <div class="form-group form-group-full">
                            <label for="address">Address <span class="required">*</span></label>
                            <textarea id="address" name="address" rows="3" required placeholder="Enter your full address"><?= htmlspecialchars($old['address'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
                        </div>
                    </div>

                    <!-- Guardian Details -->
                    <h2 class="form-section-title">Guardian Details</h2>
                    <div class="form-grid">
                        <div class="form-group">
                            <label for="guardian_name">Guardian Name <span class="required">*</span></label>
                            <input type="text" id="guardian_name" name="guardian_name" required placeholder="Enter guardian's name" value="<?= htmlspecialchars($old['guardian_name'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                        </div>
                        <div class="form-group">
                            <label for="guardian_phone">Guardian Phone <span class="required">*</span></label>
                            <input type="tel" id="guardian_phone" name="guardian_phone" required placeholder="Enter guardian's phone" value="<?= htmlspecialchars($old['guardian_phone'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                        </div>
                    </div>

                    <!-- Course Selection -->
                    <h2 class="form-section-title">Course &amp; Branch</h2>
                    <div class="form-grid">
                        <div class="form-group">
                            <label for="course_id">Select Course <span class="required">*</span></label>
                            <select id="course_id" name="course_id" required>
                                <option value="">-- Choose a Course --</option>
                                <?php foreach ($courses as $course): ?>
                                <option value="<?= htmlspecialchars($course['id'] ?? '', ENT_QUOTES, 'UTF-8') ?>" <?= ($old['course_id'] ?? '') == ($course['id'] ?? '') ? 'selected' : '' ?>>
                                    <?= htmlspecialchars($course['name'] ?? '', ENT_QUOTES, 'UTF-8') ?>
                                    <?php if (!empty($course['fee'])): ?>
                                    (₹<?= number_format((float) $course['fee'], 0) ?>)
                                    <?php endif; ?>
                                </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="branch_id">Select Branch <span class="required">*</span></label>
                            <select id="branch_id" name="branch_id" required>
                                <option value="">-- Choose a Branch --</option>
                                <?php foreach ($branches as $branch): ?>
                                <option value="<?= htmlspecialchars($branch['id'] ?? '', ENT_QUOTES, 'UTF-8') ?>" <?= ($old['branch_id'] ?? '') == ($branch['id'] ?? '') ? 'selected' : '' ?>>
                                    <?= htmlspecialchars($branch['city'] ?? $branch['name'] ?? '', ENT_QUOTES, 'UTF-8') ?>
                                    — <?= htmlspecialchars($branch['state'] ?? '', ENT_QUOTES, 'UTF-8') ?>
                                </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>

                    <!-- Message -->
                    <div class="form-group">
                        <label for="message">Additional Message</label>
                        <textarea id="message" name="message" rows="4" placeholder="Any additional information or queries..."><?= htmlspecialchars($old['message'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
                    </div>

                    <button type="submit" class="btn-primary btn-submit">Submit Application</button>
                </form>
            </div>

            <!-- Info Sidebar -->
            <aside class="admission-sidebar">
                <div class="info-card">
                    <h3>
                        <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/></svg>
                        Documents Required
                    </h3>
                    <ul class="info-list">
                        <li>Passport-size photograph (2 copies)</li>
                        <li>10th / 12th Mark Sheet (photocopy)</li>
                        <li>Aadhaar Card (photocopy)</li>
                        <li>Signature on application form</li>
                        <li>Migration Certificate (if applicable)</li>
                    </ul>
                </div>

                <div class="info-card">
                    <h3>
                        <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><line x1="12" y1="1" x2="12" y2="23"/><path d="M17 5H9.5a3.5 3.5 0 000 7h5a3.5 3.5 0 010 7H6"/></svg>
                        Fee Payment
                    </h3>
                    <ul class="info-list">
                        <li>Fee can be paid in full or in instalments</li>
                        <li>Payment modes: Cash / UPI / Bank Transfer</li>
                        <li>Admission confirmed only after fee payment</li>
                        <li>Refund policy as per institute norms</li>
                    </ul>
                </div>

                <div class="info-card">
                    <h3>
                        <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M22 16.92v3a2 2 0 01-2.18 2 19.79 19.79 0 01-8.63-3.07 19.5 19.5 0 01-6-6 19.79 19.79 0 01-3.07-8.67A2 2 0 014.11 2h3a2 2 0 012 1.72c.127.96.361 1.903.7 2.81a2 2 0 01-.45 2.11L8.09 9.91a16 16 0 006 6l1.27-1.27a2 2 0 012.11-.45c.907.339 1.85.573 2.81.7A2 2 0 0122 16.92z"/></svg>
                        Contact for Admission
                    </h3>
                    <ul class="info-list">
                        <li>Phone: <a href="tel:9305640290">9305640290</a></li>
                        <li>Email: <a href="mailto:info@gmind.in">info@gmind.in</a></li>
                        <li>Working Hours: Mon-Sat, 9 AM - 7 PM</li>
                        <li>Walk-in admissions welcome</li>
                    </ul>
                </div>
            </aside>
        </div>
    </div>
</section>

<?php include __DIR__ . '/../layouts/footer.php'; ?>
