<?php
/**
 * Admin Programme Create / Edit Form
 */
$isEdit      = !empty($programme['id']);
$formAction  = APP_URL . '/admin/universities/' . $university['id'] . '/programmes' . ($isEdit ? '/' . $programme['id'] . '/edit' : '/create');
?>
<div class="container-fluid py-4 px-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <a href="<?= APP_URL ?>/admin/universities/<?= $university['id'] ?>/programmes"
               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 Programmes
            </a>
            <h2 class="fw-bold font-outfit text-dark m-0">
                <i class="fa-solid fa-book text-primary me-2"></i>
                <?= $isEdit ? 'Edit Programme' : 'Add New Programme' ?>
            </h2>
            <p class="text-muted mb-0 text-sm"><?= htmlspecialchars($university['name']) ?></p>
        </div>
    </div>

    <?php if (!empty($errors)): ?>
        <div class="alert alert-danger rounded-3 mb-4 border-0 shadow-sm">
            <ul class="mb-0"><?php foreach ($errors as $e): ?><li><?= htmlspecialchars($e) ?></li><?php endforeach; ?></ul>
        </div>
    <?php endif; ?>

    <form action="<?= $formAction ?>" method="POST">
        <?= csrf_field() ?>
        <div class="row g-4">
            <!-- Left: Core Details -->
            <div class="col-lg-8">
                <div class="card border-0 shadow-sm mb-4">
                    <div class="card-body p-4">
                        <h5 class="fw-bold font-outfit text-dark mb-4">
                            <i class="fa-solid fa-info-circle text-info me-2"></i>Programme Details
                        </h5>
                        <div class="row g-3">
                            <div class="col-12">
                                <label class="form-label fw-bold font-outfit text-sm">Programme Name <span class="text-danger">*</span></label>
                                <input type="text" name="name" class="form-control" required
                                       value="<?= htmlspecialchars($programme['name'] ?? '') ?>"
                                       placeholder="e.g. BSc Computer Science">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Degree Level <span class="text-danger">*</span></label>
                                <select name="degree_level" class="form-select" required>
                                    <?php foreach (['certificate' => 'Certificate', 'diploma' => 'Diploma', 'bachelor' => "Bachelor's", 'master' => "Master's", 'phd' => 'PhD'] as $val => $label): ?>
                                        <option value="<?= $val ?>" <?= ($programme['degree_level'] ?? 'bachelor') === $val ? 'selected' : '' ?>>
                                            <?= $label ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Duration (Years)</label>
                                <input type="number" name="duration_years" class="form-control" min="1" max="10"
                                       value="<?= htmlspecialchars((string)($programme['duration_years'] ?? '4')) ?>">
                            </div>
                            <?php if (!empty($faculties)): ?>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Faculty / School</label>
                                <select name="faculty_id" class="form-select">
                                    <option value="">— No Faculty —</option>
                                    <?php foreach ($faculties as $fac): ?>
                                        <option value="<?= $fac['id'] ?>" <?= ($programme['faculty_id'] ?? '') == $fac['id'] ? 'selected' : '' ?>>
                                            <?= htmlspecialchars($fac['name']) ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <?php endif; ?>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Min. WASSCE Aggregate</label>
                                <input type="number" name="min_aggregate" class="form-control" min="6" max="36"
                                       value="<?= htmlspecialchars((string)($programme['min_aggregate'] ?? '')) ?>"
                                       placeholder="e.g. 24">
                                <small class="text-muted">Leave blank if not applicable.</small>
                            </div>
                            <div class="col-12">
                                <label class="form-label fw-bold font-outfit text-sm">Programme Description</label>
                                <textarea name="description" class="form-control" rows="4"
                                          placeholder="Brief overview of this programme..."><?= htmlspecialchars($programme['description'] ?? '') ?></textarea>
                            </div>
                            <div class="col-12">
                                <label class="form-label fw-bold font-outfit text-sm">Admission Requirements</label>
                                <textarea name="admission_requirements" class="form-control" rows="3"
                                          placeholder="Specific requirements for this programme..."><?= htmlspecialchars($programme['admission_requirements'] ?? '') ?></textarea>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Right: Settings -->
            <div class="col-lg-4">
                <div class="card border-0 shadow-sm mb-4">
                    <div class="card-body p-4">
                        <h6 class="fw-bold font-outfit text-dark mb-3">
                            <i class="fa-solid fa-sliders text-warning me-2"></i>Settings
                        </h6>
                        <div class="mb-3">
                            <label class="form-label fw-bold font-outfit text-sm">Local Tuition (GHS/yr)</label>
                            <input type="number" name="tuition_local" class="form-control" min="0" step="100"
                                   value="<?= htmlspecialchars((string)($programme['tuition_local'] ?? '')) ?>"
                                   placeholder="e.g. 3500">
                        </div>
                        <div class="mb-3">
                            <label class="form-label fw-bold font-outfit text-sm">Application Deadline</label>
                            <input type="date" name="application_deadline" class="form-control"
                                   value="<?= htmlspecialchars($programme['application_deadline'] ?? '') ?>">
                        </div>
                        <div class="form-check form-switch mb-3">
                            <input class="form-check-input" type="checkbox" name="is_active" id="is_active" value="1"
                                   <?= ($programme['is_active'] ?? 1) ? 'checked' : '' ?>>
                            <label class="form-check-label font-outfit fw-bold" for="is_active">
                                Active (visible to students)
                            </label>
                        </div>
                    </div>
                </div>

                <div class="d-grid gap-2">
                    <button type="submit" class="btn btn-primary rounded-pill py-2 font-outfit fw-bold shadow-sm">
                        <i class="fa-solid fa-floppy-disk me-2"></i>
                        <?= $isEdit ? 'Update Programme' : 'Save Programme' ?>
                    </button>
                    <a href="<?= APP_URL ?>/admin/universities/<?= $university['id'] ?>/programmes"
                       class="btn btn-outline-secondary rounded-pill py-2 font-outfit fw-bold">Cancel</a>
                </div>
            </div>
        </div>
    </form>
</div>
