<?php
/**
 * Admin — Manage Programmes for a University
 */
$degreeLabels = [
    'certificate' => 'Certificate',
    'diploma'     => 'Diploma',
    'bachelor'    => "Bachelor's",
    'master'      => "Master's",
    'phd'         => 'PhD',
];
?>
<div class="container-fluid py-4 px-4">
    <!-- Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <a href="<?= APP_URL ?>/admin/universities" class="text-muted text-xs font-outfit fw-bold text-decoration-none mb-1 d-block">
                <i class="fa-solid fa-arrow-left me-1"></i> Back to Universities
            </a>
            <h2 class="fw-bold font-outfit text-dark m-0">
                <i class="fa-solid fa-list-check text-primary me-2"></i>
                Programmes — <?= htmlspecialchars($university['name']) ?>
            </h2>
            <p class="text-muted mb-0 text-sm">Manage academic programmes offered by this university</p>
        </div>
        <div class="d-flex gap-2">
            <a href="<?= APP_URL ?>/admin/universities/<?= $university['id'] ?>/edit" class="btn btn-outline-secondary rounded-pill px-4 font-outfit fw-bold btn-sm">
                <i class="fa-solid fa-pen me-1"></i> Edit University
            </a>
            <a href="<?= APP_URL ?>/admin/universities/<?= $university['id'] ?>/programmes/create" class="btn btn-primary rounded-pill px-4 font-outfit fw-bold btn-sm shadow-sm">
                <i class="fa-solid fa-plus me-1"></i> Add Programme
            </a>
        </div>
    </div>

    <?php if ($msg = $_SESSION['flash']['success'] ?? null): unset($_SESSION['flash']['success']); ?>
        <div class="alert alert-success border-0 shadow-sm mb-4 rounded-3"><i class="fa-solid fa-circle-check me-2"></i><?= htmlspecialchars($msg) ?></div>
    <?php endif; ?>

    <!-- Stats bar -->
    <div class="row g-3 mb-4">
        <div class="col-auto">
            <div class="card border-0 shadow-sm px-4 py-3 d-flex flex-row align-items-center gap-3">
                <div class="badge-icon-box bg-primary text-white rounded-circle"><i class="fa-solid fa-list"></i></div>
                <div>
                    <div class="fw-bold font-outfit text-dark fs-4"><?= count($programmes) ?></div>
                    <small class="text-muted">Total Programmes</small>
                </div>
            </div>
        </div>
        <div class="col-auto">
            <div class="card border-0 shadow-sm px-4 py-3 d-flex flex-row align-items-center gap-3">
                <div class="badge-icon-box bg-success text-white rounded-circle"><i class="fa-solid fa-eye"></i></div>
                <div>
                    <div class="fw-bold font-outfit text-dark fs-4"><?= count(array_filter($programmes, fn($p) => $p['is_active'])) ?></div>
                    <small class="text-muted">Active</small>
                </div>
            </div>
        </div>
    </div>

    <!-- Programme Table -->
    <div class="card border-0 shadow-sm">
        <div class="card-body p-0">
            <?php if (empty($programmes)): ?>
                <div class="text-center py-5">
                    <i class="fa-solid fa-folder-open text-muted opacity-50" style="font-size:3rem;"></i>
                    <p class="mt-3 text-muted fw-medium">No programmes added yet.</p>
                    <a href="<?= APP_URL ?>/admin/universities/<?= $university['id'] ?>/programmes/create" class="btn btn-primary rounded-pill px-4 font-outfit fw-bold">
                        <i class="fa-solid fa-plus me-2"></i>Add First Programme
                    </a>
                </div>
            <?php else: ?>
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">
                        <thead class="bg-light border-bottom">
                            <tr>
                                <th class="px-4 py-3 font-outfit text-sm fw-bold text-dark">Programme Name</th>
                                <th class="py-3 font-outfit text-sm fw-bold text-dark">Faculty</th>
                                <th class="py-3 font-outfit text-sm fw-bold text-dark">Degree</th>
                                <th class="py-3 font-outfit text-sm fw-bold text-dark">Duration</th>
                                <th class="py-3 font-outfit text-sm fw-bold text-dark">Min. Aggregate</th>
                                <th class="py-3 font-outfit text-sm fw-bold text-dark">Status</th>
                                <th class="py-3 font-outfit text-sm fw-bold text-dark text-center">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($programmes as $prog): ?>
                                <tr>
                                    <td class="px-4 py-3">
                                        <span class="fw-bold text-dark font-outfit"><?= htmlspecialchars($prog['name']) ?></span>
                                        <?php if (!empty($prog['description'])): ?>
                                            <br><small class="text-muted"><?= htmlspecialchars(substr($prog['description'], 0, 60)) ?>...</small>
                                        <?php endif; ?>
                                    </td>
                                    <td class="py-3 text-muted text-sm"><?= htmlspecialchars($prog['faculty_name'] ?? '—') ?></td>
                                    <td class="py-3">
                                        <span class="badge bg-primary-subtle text-primary rounded-pill font-outfit text-xs px-2">
                                            <?= $degreeLabels[$prog['degree_level']] ?? $prog['degree_level'] ?>
                                        </span>
                                    </td>
                                    <td class="py-3 text-sm text-dark"><?= $prog['duration_years'] ?> yr<?= $prog['duration_years'] > 1 ? 's' : '' ?></td>
                                    <td class="py-3 text-sm text-dark"><?= $prog['min_aggregate'] ?? '—' ?></td>
                                    <td class="py-3">
                                        <?php if ($prog['is_active']): ?>
                                            <span class="badge bg-success-subtle text-success rounded-pill text-xs px-2">Active</span>
                                        <?php else: ?>
                                            <span class="badge bg-secondary-subtle text-secondary rounded-pill text-xs px-2">Hidden</span>
                                        <?php endif; ?>
                                    </td>
                                    <td class="py-3 text-center">
                                        <div class="d-flex justify-content-center gap-1">
                                            <a href="<?= APP_URL ?>/admin/universities/<?= $university['id'] ?>/programmes/<?= $prog['id'] ?>/edit"
                                               class="btn btn-sm btn-outline-primary rounded-pill px-2" title="Edit">
                                                <i class="fa-solid fa-pen"></i>
                                            </a>
                                            <form action="<?= APP_URL ?>/admin/universities/<?= $university['id'] ?>/programmes/<?= $prog['id'] ?>/delete"
                                                  method="POST" onsubmit="return confirm('Delete this programme?')">
                                                <?= csrf_field() ?><input type="hidden" name="_method" value="DELETE">
                                                <button type="submit" 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; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>
