<?php $pageTitle = 'Applications'; ?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <p class="text-secondary mb-0">Manage student applications</p>
    </div>
</div>

<div class="card-cix mb-4">
    <form method="GET" action="<?= base_url('admin/applications') ?>" class="row g-3 align-items-end">
        <div class="col-md-4">
            <label class="form-label-cix">Search</label>
            <input type="text" name="search" class="form-control-cix" placeholder="Name or email..." value="<?= e($search ?? '') ?>">
        </div>
        <div class="col-md-3">
            <label class="form-label-cix">Status</label>
            <select name="status" class="form-control-cix">
                <option value="">All Statuses</option>
                <?php foreach (['pending','submitted','approved','rejected'] as $s): ?>
                <option value="<?= $s ?>" <?= ($status ?? '') === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-2">
            <button type="submit" class="btn-cix btn-cix-primary"><i class="fas fa-search"></i> Filter</button>
        </div>
    </form>
</div>

<div class="card-cix">
    <div class="table-responsive">
        <table class="table-cix">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Applicant</th>
                    <th>Email</th>
                    <th>Phone</th>
                    <th>Package</th>
                    <th>Status</th>
                    <th>Date</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($applications)): ?>
                    <?php foreach ($applications as $i => $app): ?>
                    <tr>
                        <td><?= ($pagination['offset'] ?? 0) + $i + 1 ?></td>
                        <td><strong><?= e($app['first_name'] . ' ' . $app['last_name']) ?></strong></td>
                        <td class="text-secondary"><?= e($app['email']) ?></td>
                        <td class="text-secondary"><?= e($app['phone'] ?? '-') ?></td>
                        <td><?= e($app['package_name'] ?? '-') ?></td>
                        <td><span class="badge-cix badge-<?= $app['status'] ?>"><?= ucfirst($app['status']) ?></span></td>
                        <td class="text-secondary small"><?= e($app['created_at']) ?></td>
                        <td>
                            <a href="<?= base_url('admin/applications/' . $app['id']) ?>" class="btn-cix btn-cix-sm btn-cix-primary"><i class="fas fa-eye"></i></a>
                            <form method="POST" action="<?= base_url('admin/applications/' . $app['id'] . '/delete') ?>" class="d-inline" onsubmit="return confirm('Are you sure?')">
                                <?= csrf_field() ?>
                                <button type="submit" class="btn-cix btn-cix-sm btn-cix-danger"><i class="fas fa-trash"></i></button>
                            </form>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr><td colspan="8" class="text-center text-secondary p-4">No applications found</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <?php if (!empty($pagination['total_pages']) && $pagination['total_pages'] > 1): ?>
    <div class="d-flex justify-content-center mt-3">
        <nav>
            <ul class="pagination">
                <?php for ($p = 1; $p <= $pagination['total_pages']; $p++): ?>
                <li class="page-item <?= ($pagination['current'] ?? 1) == $p ? 'active' : '' ?>">
                    <a class="page-link" href="?page=<?= $p ?>&status=<?= e($status ?? '') ?>&search=<?= e($search ?? '') ?>"><?= $p ?></a>
                </li>
                <?php endfor; ?>
            </ul>
        </nav>
    </div>
    <?php endif; ?>
</div>
