<?php
/**
 * Admin Scholarships Management View
 * Lists all scholarships with status, category filters, and CRUD actions.
 */
?>
<div class="container-fluid py-4 px-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="fw-bold font-outfit text-dark m-0"><i class="fa-solid fa-graduation-cap text-warning me-2"></i>Manage Scholarships</h2>
            <p class="text-muted mb-0"><?= number_format($pagination['total'] ?? 0) ?> scholarships listed</p>
        </div>
        <a href="<?= APP_URL ?>/admin/scholarships/create" class="btn btn-warning rounded-pill px-4 font-outfit fw-bold text-dark">
            <i class="fa-solid fa-plus me-2"></i>Add Scholarship
        </a>
    </div>

    <!-- Filters -->
    <div class="card border-0 shadow-sm p-4 mb-4">
        <form action="<?= APP_URL ?>/admin/scholarships" method="GET" class="row g-3 align-items-end">
            <div class="col-md-4">
                <label class="form-label text-muted text-xs font-outfit fw-bold">Search</label>
                <div class="input-group">
                    <span class="input-group-text bg-white border-end-0"><i class="fa-solid fa-search text-muted"></i></span>
                    <input type="text" name="search" class="form-control border-start-0 ps-0" placeholder="Name, provider..." value="<?= htmlspecialchars($search ?? '') ?>">
                </div>
            </div>
            <div class="col-md-2">
                <label class="form-label text-muted text-xs font-outfit fw-bold">Category</label>
                <select name="category" class="form-select">
                    <option value="">All Categories</option>
                    <?php foreach ($categories ?? [] as $cat): ?>
                        <option value="<?= htmlspecialchars($cat) ?>" <?= ($selectedCategory ?? '') === $cat ? 'selected' : '' ?>><?= htmlspecialchars($cat) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label text-muted text-xs font-outfit fw-bold">Status</label>
                <select name="status" class="form-select">
                    <option value="">All</option>
                    <option value="open" <?= ($selectedStatus ?? '') === 'open' ? 'selected' : '' ?>>Open</option>
                    <option value="closed" <?= ($selectedStatus ?? '') === 'closed' ? 'selected' : '' ?>>Closed</option>
                    <option value="upcoming" <?= ($selectedStatus ?? '') === 'upcoming' ? 'selected' : '' ?>>Upcoming</option>
                </select>
            </div>
            <div class="col-md-2">
                <button class="btn btn-warning rounded-pill w-100 font-outfit fw-bold text-dark"><i class="fa-solid fa-filter me-2"></i>Filter</button>
            </div>
        </form>
    </div>

    <!-- Scholarships Table -->
    <div class="card border-0 shadow-sm">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle text-sm mb-0">
                    <thead class="table-light">
                        <tr>
                            <th class="ps-4 py-3 border-0 font-outfit fw-bold text-muted">Scholarship</th>
                            <th class="border-0 font-outfit fw-bold text-muted">Provider</th>
                            <th class="border-0 font-outfit fw-bold text-muted">Category</th>
                            <th class="border-0 font-outfit fw-bold text-muted">Amount</th>
                            <th class="border-0 font-outfit fw-bold text-muted">Deadline</th>
                            <th class="border-0 font-outfit fw-bold text-muted">Status</th>
                            <th class="border-0 font-outfit fw-bold text-muted text-center">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($scholarships ?? [] as $sch): 
                            $statusColors = ['open' => 'success', 'closed' => 'danger', 'upcoming' => 'info'];
                            $statusColor = $statusColors[$sch['status']] ?? 'secondary';
                        ?>
                            <tr>
                                <td class="ps-4">
                                    <span class="fw-bold font-outfit text-dark d-block"><?= htmlspecialchars($sch['name']) ?></span>
                                    <small class="text-muted"><?= htmlspecialchars(mb_strimwidth($sch['description'] ?? '', 0, 60, '...')) ?></small>
                                </td>
                                <td class="text-dark font-outfit"><?= htmlspecialchars($sch['provider']) ?></td>
                                <td>
                                    <span class="badge bg-warning-subtle text-warning rounded-pill font-outfit text-xs"><?= htmlspecialchars($sch['category']) ?></span>
                                </td>
                                <td class="fw-bold text-success font-outfit"><?= !empty($sch['value_amount']) ? htmlspecialchars($sch['value_currency'] . ' ' . number_format($sch['value_amount'], 2)) : 'Varies' ?></td>
                                <td class="text-muted"><?= !empty($sch['application_deadline']) ? date('d M Y', strtotime($sch['application_deadline'])) : 'N/A' ?></td>
                                <td>
                                    <span class="badge bg-<?= $statusColor ?>-subtle text-<?= $statusColor ?> rounded-pill font-outfit"><?= ucfirst($sch['status']) ?></span>
                                    <?php if (!$sch['is_active']): ?>
                                        <span class="badge bg-secondary-subtle text-secondary rounded-pill font-outfit ms-1">Hidden</span>
                                    <?php endif; ?>
                                </td>
                                <td class="text-center">
                                    <div class="d-flex justify-content-center gap-1">
                                        <a href="<?= APP_URL ?>/scholarships/<?= htmlspecialchars($sch['slug']) ?>" target="_blank" class="btn btn-sm btn-outline-secondary rounded-pill px-2" title="Preview"><i class="fa-solid fa-eye"></i></a>
                                        <a href="<?= APP_URL ?>/admin/scholarships/<?= $sch['id'] ?>/edit" class="btn btn-sm btn-outline-warning rounded-pill px-2" title="Edit"><i class="fa-solid fa-pen"></i></a>
                                        <form action="<?= APP_URL ?>/admin/scholarships/<?= $sch['id'] ?>" method="POST" onsubmit="return confirm('Delete this scholarship?')">
                                            <?= csrf_field() ?><input type="hidden" name="_method" value="DELETE">
                                            <button class="btn btn-sm btn-outline-danger rounded-pill px-2" title="Delete"><i class="fa-solid fa-trash"></i></button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($scholarships)): ?>
                            <tr><td colspan="7" class="text-center py-5 text-muted">No scholarships found.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Pagination -->
    <?php if (($pagination['total_pages'] ?? 1) > 1): ?>
        <nav class="mt-4 d-flex justify-content-center">
            <ul class="pagination pagination-sm gap-1">
                <?php for ($p = 1; $p <= $pagination['total_pages']; $p++): ?>
                    <li class="page-item <?= $p == ($pagination['current_page'] ?? 1) ? 'active' : '' ?>">
                        <a class="page-link rounded-circle border-0 shadow-sm" href="<?= APP_URL ?>/admin/scholarships?page=<?= $p ?>&search=<?= urlencode($search ?? '') ?>"><?= $p ?></a>
                    </li>
                <?php endfor; ?>
            </ul>
        </nav>
    <?php endif; ?>
</div>
