<?php
/**
 * Admin Scholarship Create/Edit Form
 */
$isEdit = !empty($scholarship['id']);
?>
<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>
                <?= $isEdit ? 'Edit Scholarship' : 'Add New Scholarship' ?>
            </h2>
        </div>
        <a href="<?= APP_URL ?>/admin/scholarships" 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>

    <form action="<?= APP_URL ?>/admin/scholarships<?= $isEdit ? '/' . $scholarship['id'] . '/edit' : '/create' ?>" method="POST" enctype="multipart/form-data">
        <?= csrf_field() ?>

        <div class="row g-4">
            <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>Scholarship Details</h5>
                        <div class="row g-3">
                            <div class="col-12">
                                <label class="form-label fw-bold font-outfit text-sm">Scholarship Name <span class="text-danger">*</span></label>
                                <input type="text" name="name" class="form-control" required value="<?= htmlspecialchars($scholarship['name'] ?? '') ?>" placeholder="e.g. Ghana Education Trust Fund Scholarship">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Provider / Organisation <span class="text-danger">*</span></label>
                                <input type="text" name="provider" class="form-control" required value="<?= htmlspecialchars($scholarship['provider'] ?? '') ?>" placeholder="e.g. GETFUND">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Category <span class="text-danger">*</span></label>
                                <select name="category" class="form-select" required>
                                    <option value="">Select category</option>
                                    <?php foreach (['Government', 'NGO', 'University', 'International', 'Corporate', 'Merit-Based', 'Need-Based', 'STEM', 'Arts & Humanities'] as $cat): ?>
                                        <option value="<?= $cat ?>" <?= ($scholarship['category'] ?? '') === $cat ? 'selected' : '' ?>><?= $cat ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-12">
                                <label class="form-label fw-bold font-outfit text-sm">Description <span class="text-danger">*</span></label>
                                <textarea name="description" class="form-control" rows="4" required placeholder="Describe the scholarship..."><?= htmlspecialchars($scholarship['description'] ?? '') ?></textarea>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Amount / Value</label>
                                <input type="text" name="amount" class="form-control" value="<?= htmlspecialchars($scholarship['amount'] ?? '') ?>" placeholder="e.g. Full tuition, GHS 5,000/year">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Application Deadline</label>
                                <input type="date" name="deadline" class="form-control" value="<?= htmlspecialchars($scholarship['deadline'] ?? '') ?>">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Application URL</label>
                                <input type="url" name="application_url" class="form-control" value="<?= htmlspecialchars($scholarship['application_url'] ?? '') ?>" placeholder="https://...">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Level of Study</label>
                                <select name="level" class="form-select">
                                    <option value="">All Levels</option>
                                    <?php foreach (['Undergraduate', 'Postgraduate', 'PhD', 'Vocational', 'All Levels'] as $lev): ?>
                                        <option value="<?= $lev ?>" <?= ($scholarship['level'] ?? '') === $lev ? 'selected' : '' ?>><?= $lev ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-12">
                                <label class="form-label fw-bold font-outfit text-sm">Eligibility Requirements</label>
                                <textarea name="eligibility" class="form-control" rows="3" placeholder="GPA requirements, nationality, programme restrictions..."><?= htmlspecialchars($scholarship['eligibility'] ?? '') ?></textarea>
                            </div>
                            <div class="col-12">
                                <label class="form-label fw-bold font-outfit text-sm">Required Documents</label>
                                <textarea name="required_documents" class="form-control" rows="3" placeholder="List documents required for application..."><?= htmlspecialchars($scholarship['required_documents'] ?? '') ?></textarea>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Right: Settings & Targeting -->
            <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-image text-primary me-2"></i>Branding</h6>
                        <div class="mb-3">
                            <label class="form-label fw-bold font-outfit text-sm">Provider Logo</label>
                            <?php if (!empty($scholarship['logo'])): ?>
                                <img src="<?= htmlspecialchars($scholarship['logo']) ?>" alt="Logo" class="d-block mb-2 rounded-2" width="60" height="60" style="object-fit:contain;">
                            <?php endif; ?>
                            <input type="file" name="logo" class="form-control" accept="image/*">
                        </div>
                    </div>
                </div>

                <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-crosshairs text-warning me-2"></i>AI Matching Criteria</h6>
                        <div class="mb-3">
                            <label class="form-label fw-bold font-outfit text-sm">Target SHS Programmes</label>
                            <input type="text" name="target_programmes" class="form-control text-sm" value="<?= htmlspecialchars($scholarship['target_programmes'] ?? '') ?>" placeholder="e.g. General Science, Arts (comma-separated)">
                            <small class="text-muted">Leave blank to match all programmes.</small>
                        </div>
                        <div class="mb-3">
                            <label class="form-label fw-bold font-outfit text-sm">Minimum GPA</label>
                            <input type="number" name="min_gpa" class="form-control" min="0" max="4" step="0.1" value="<?= htmlspecialchars($scholarship['min_gpa'] ?? '') ?>" placeholder="e.g. 3.0">
                        </div>
                        <div class="mb-3">
                            <label class="form-label fw-bold font-outfit text-sm">Required Region(s)</label>
                            <input type="text" name="required_region" class="form-control text-sm" value="<?= htmlspecialchars($scholarship['required_region'] ?? '') ?>" placeholder="Leave blank for all regions">
                        </div>
                    </div>
                </div>

                <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="form-check form-switch mb-3">
                            <input class="form-check-input" type="checkbox" name="is_active" id="is_active" value="1" <?= ($scholarship['is_active'] ?? 1) ? 'checked' : '' ?>>
                            <label class="form-check-label font-outfit fw-bold" for="is_active">Active (visible to students)</label>
                        </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($scholarship['is_featured']) ? 'checked' : '' ?>>
                            <label class="form-check-label font-outfit fw-bold" for="is_featured">Feature on homepage</label>
                        </div>
                    </div>
                </div>

                <div class="d-grid gap-2">
                    <button type="submit" class="btn btn-warning rounded-pill py-2 font-outfit fw-bold text-dark">
                        <i class="fa-solid fa-floppy-disk me-2"></i><?= $isEdit ? 'Update Scholarship' : 'Save Scholarship' ?>
                    </button>
                    <a href="<?= APP_URL ?>/admin/scholarships" class="btn btn-outline-secondary rounded-pill py-2 font-outfit fw-bold">Cancel</a>
                </div>
            </div>
        </div>
    </form>
</div>
