<?php
/**
 * Admin University Create/Edit Form
 * Field names aligned with collectUniversityData() and DB schema.
 */
$isEdit = !empty($university['id']);
$formAction = $isEdit
    ? APP_URL . '/admin/universities/' . $university['id'] . '/edit'
    : APP_URL . '/admin/universities/create';
?>
<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-building-columns text-primary me-2"></i>
                <?= $isEdit ? 'Edit University' : 'Add New University' ?>
            </h2>
            <p class="text-muted mb-0"><?= $isEdit ? 'Update information for ' . htmlspecialchars($university['name']) : 'Fill in the details to add a new university' ?></p>
        </div>
        <a href="<?= APP_URL ?>/admin/universities" class="btn btn-outline-secondary rounded-pill px-4 font-outfit fw-bold">
            <i class="fa-solid fa-arrow-left me-2"></i>Back
        </a>
    </div>

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

    <form action="<?= $formAction ?>" method="POST" enctype="multipart/form-data">
        <?= csrf_field() ?>

        <div class="row g-4">
            <!-- Left: Core Info -->
            <div class="col-lg-8">
                <!-- Basic Information -->
                <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>Basic Information</h5>
                        <div class="row g-3">
                            <div class="col-12">
                                <label class="form-label fw-bold font-outfit text-sm">University Name <span class="text-danger">*</span></label>
                                <input type="text" name="name" class="form-control" required
                                       value="<?= htmlspecialchars($university['name'] ?? '') ?>"
                                       placeholder="e.g. University of Ghana">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Type <span class="text-danger">*</span></label>
                                <select name="university_type" class="form-select" required>
                                    <option value="">Select type</option>
                                    <?php foreach (['public' => 'Public', 'private' => 'Private', 'technical' => 'Technical', 'polytechnic' => 'Polytechnic', 'distance' => 'Distance Learning', 'international' => 'International'] as $val => $label): ?>
                                        <option value="<?= $val ?>" <?= ($university['university_type'] ?? '') === $val ? 'selected' : '' ?>><?= $label ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">City / Location</label>
                                <input type="text" name="city" class="form-control"
                                       value="<?= htmlspecialchars($university['city'] ?? '') ?>"
                                       placeholder="e.g. Legon, Accra">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Country</label>
                                <select name="country_id" class="form-select">
                                    <option value="1">Ghana</option>
                                    <?php foreach ($countries ?? [] as $country): ?>
                                        <?php if ($country['id'] == 1) continue; ?>
                                        <option value="<?= $country['id'] ?>" <?= ($university['country_id'] ?? 1) == $country['id'] ? 'selected' : '' ?>>
                                            <?= htmlspecialchars($country['name']) ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Region</label>
                                <select name="region_id" class="form-select">
                                    <option value="">Select region</option>
                                    <?php foreach ($regions ?? [] as $reg): ?>
                                        <option value="<?= $reg['id'] ?>" <?= ($university['region_id'] ?? '') == $reg['id'] ? 'selected' : '' ?>>
                                            <?= htmlspecialchars($reg['name']) ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Website URL</label>
                                <input type="url" name="website" class="form-control"
                                       value="<?= htmlspecialchars($university['website'] ?? '') ?>"
                                       placeholder="https://...">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Established Year</label>
                                <input type="number" name="established_year" class="form-control"
                                       min="1800" max="<?= date('Y') ?>"
                                       value="<?= htmlspecialchars($university['established_year'] ?? '') ?>"
                                       placeholder="e.g. 1948">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Student Population</label>
                                <input type="number" name="student_population" class="form-control"
                                       value="<?= htmlspecialchars($university['student_population'] ?? '') ?>"
                                       placeholder="e.g. 38000">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Local Ranking</label>
                                <input type="number" name="ranking_local" class="form-control"
                                       value="<?= htmlspecialchars($university['ranking_local'] ?? '') ?>"
                                       placeholder="e.g. 1">
                            </div>
                            <div class="col-12">
                                <label class="form-label fw-bold font-outfit text-sm">Description / Overview</label>
                                <textarea name="description" class="form-control" rows="4"
                                          placeholder="Brief overview of the university..."><?= htmlspecialchars($university['description'] ?? '') ?></textarea>
                            </div>
                            <div class="col-12">
                                <label class="form-label fw-bold font-outfit text-sm">Accreditation</label>
                                <textarea name="accreditation" class="form-control" rows="2"
                                          placeholder="Accrediting bodies..."><?= htmlspecialchars($university['accreditation'] ?? '') ?></textarea>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Contact Information -->
                <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-address-card text-warning me-2"></i>Contact & Application</h5>
                        <div class="row g-3">
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Contact Email</label>
                                <input type="email" name="contact_email" class="form-control"
                                       value="<?= htmlspecialchars($university['contact_email'] ?? '') ?>"
                                       placeholder="info@university.edu.gh">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Contact Phone</label>
                                <input type="tel" name="contact_phone" class="form-control"
                                       value="<?= htmlspecialchars($university['contact_phone'] ?? '') ?>"
                                       placeholder="+233...">
                            </div>
                            <div class="col-12">
                                <label class="form-label fw-bold font-outfit text-sm">Contact Address</label>
                                <textarea name="contact_address" class="form-control" rows="2"><?= htmlspecialchars($university['contact_address'] ?? '') ?></textarea>
                            </div>
                            <div class="col-12">
                                <label class="form-label fw-bold font-outfit text-sm">Application Portal URL</label>
                                <input type="url" name="application_portal" class="form-control"
                                       value="<?= htmlspecialchars($university['application_portal'] ?? '') ?>"
                                       placeholder="https://apply.university.edu.gh">
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Right: Media & Settings -->
            <div class="col-lg-4">
                <!-- Logo & Banner -->
                <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-image text-primary me-2"></i>Media</h6>
                        <div class="mb-4">
                            <label class="form-label fw-bold font-outfit text-sm">Logo</label>
                            <?php if (!empty($university['logo'])): ?>
                                <img src="<?= htmlspecialchars($university['logo']) ?>" alt="Current Logo"
                                     class="d-block rounded-2 mb-2" width="80" height="80"
                                     style="object-fit:contain;border:1px solid #eee;">
                                <small class="text-muted d-block mb-2">Current logo. Upload below to replace.</small>
                            <?php endif; ?>
                            <input type="file" name="logo" class="form-control" accept="image/*">
                        </div>
                        <div>
                            <label class="form-label fw-bold font-outfit text-sm">Banner Image</label>
                            <?php if (!empty($university['banner'])): ?>
                                <img src="<?= htmlspecialchars($university['banner']) ?>" alt="Current Banner"
                                     class="d-block rounded-2 mb-2 w-100" style="height:80px;object-fit:cover;">
                            <?php endif; ?>
                            <input type="file" name="banner" class="form-control" accept="image/*">
                        </div>
                    </div>
                </div>

                <!-- Settings -->
                <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-toggle-on text-warning me-2"></i>Settings</h6>
                        <div class="mb-3">
                            <label class="form-label fw-bold font-outfit text-sm">Status</label>
                            <select name="status" class="form-select">
                                <option value="active" <?= ($university['status'] ?? 'active') === 'active' ? 'selected' : '' ?>>Active (visible)</option>
                                <option value="inactive" <?= ($university['status'] ?? '') === 'inactive' ? 'selected' : '' ?>>Inactive (hidden)</option>
                            </select>
                        </div>
                        <div class="form-check form-switch mb-3">
                            <input class="form-check-input" type="checkbox" name="is_featured" id="is_featured"
                                   value="1" <?= !empty($university['is_featured']) ? 'checked' : '' ?>>
                            <label class="form-check-label font-outfit fw-bold" for="is_featured">Featured university</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">
                        <i class="fa-solid fa-floppy-disk me-2"></i><?= $isEdit ? 'Update University' : 'Save University' ?>
                    </button>
                    <a href="<?= APP_URL ?>/admin/universities" class="btn btn-outline-secondary rounded-pill py-2 font-outfit fw-bold">Cancel</a>
                </div>

                <?php if ($isEdit): ?>
                <hr class="my-4 opacity-25">
                <div class="card border-0 shadow-sm">
                    <div class="card-body p-4">
                        <h6 class="fw-bold font-outfit text-dark mb-1"><i class="fa-solid fa-list-check text-primary me-2"></i>Content Management</h6>
                        <p class="text-muted text-xs mb-3">Add or manage the programmes and faculties offered by this university.</p>
                        <a href="<?= APP_URL ?>/admin/universities/<?= $university['id'] ?>/programmes"
                           class="btn btn-outline-primary rounded-pill w-100 font-outfit fw-bold btn-sm mb-2">
                            <i class="fa-solid fa-graduation-cap me-2"></i>Manage Programmes
                        </a>
                    </div>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </form>
</div>
