<?php
$pageTitle = 'Edit Success Story';
require_once dirname(__DIR__) . '/includes/header.php';
$id = (int)($_GET['id'] ?? 0);
if (!$id) redirect(ADMIN_URL.'/pages/success.php');
$s = db()->query("SELECT * FROM success_stories WHERE id=$id")->fetch_assoc();
if (!$s) { flash('Not found.','error'); redirect(ADMIN_URL.'/pages/success.php'); }
$thisYear = (int)date('Y');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name   = trim($_POST['student_name'] ?? '');
    $dest   = trim($_POST['destination'] ?? '');
    $uni    = trim($_POST['university'] ?? '');
    $test   = trim($_POST['testimonial'] ?? '');
    $year   = (int)($_POST['year'] ?? date('Y'));
    $status = $_POST['status'] ?? 'active';
    $sort   = (int)($_POST['sort_order'] ?? 0);
    $photo  = handleUpload('photo','success') ?? trim($_POST['photo_url'] ?? $s['photo']);
    $st = db()->prepare("UPDATE success_stories SET student_name=?,destination=?,university=?,testimonial=?,photo=?,year=?,sort_order=?,status=? WHERE id=?");
    $st->bind_param('ssssssssi', $name,$dest,$uni,$test,$photo,$year,$sort,$status,$id);
    $st->execute();
    flash('Story updated!','success');
    redirect(ADMIN_URL.'/pages/success.php');
}
?>
<div class="page-header">
    <a href="success.php" class="btn btn-secondary btn-sm"><i class="fas fa-arrow-left"></i></a>
    <h1>Edit Success Story</h1>
</div>
<div class="card">
    <div class="card-header"><i class="fas fa-edit" style="color:var(--brand)"></i><h2><?=e($s['student_name'])?></h2></div>
    <div class="card-body">
        <form method="POST" enctype="multipart/form-data">
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label">Student Name *</label>
                    <input type="text" name="student_name" class="form-control" value="<?=e($s['student_name'])?>" required>
                </div>
                <div class="form-group">
                    <label class="form-label">Year</label>
                    <input type="number" name="year" class="form-control" value="<?=(int)$s['year']?>" min="2000" max="<?=$thisYear?>">
                </div>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label">Destination</label>
                    <input type="text" name="destination" class="form-control" value="<?=e($s['destination'])?>">
                </div>
                <div class="form-group">
                    <label class="form-label">University</label>
                    <input type="text" name="university" class="form-control" value="<?=e($s['university'])?>">
                </div>
            </div>
            <div class="form-group">
                <label class="form-label">Testimonial</label>
                <textarea name="testimonial" class="form-control" rows="4"><?=e($s['testimonial'])?></textarea>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label">Replace Photo</label>
                    <input type="file" name="photo" class="form-control" accept="image/*" onchange="previewImg(this)">
                    <?php if($s['photo']): ?><img src="<?=e(SITE_URL.'/'.$s['photo'])?>" id="imgPreview" style="margin-top:8px;width:60px;height:60px;border-radius:50%;object-fit:cover">
                    <?php else: ?><img id="imgPreview" style="margin-top:8px;display:none;width:60px;height:60px;border-radius:50%;object-fit:cover"><?php endif; ?>
                </div>
                <div class="form-group">
                    <label class="form-label">Or Photo URL</label>
                    <input type="text" name="photo_url" class="form-control" value="<?=e($s['photo'])?>">
                </div>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label">Status</label>
                    <select name="status" class="form-select">
                        <option value="active" <?=$s['status']==='active'?'selected':''?>>Active</option>
                        <option value="inactive" <?=$s['status']==='inactive'?'selected':''?>>Inactive</option>
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">Sort Order</label>
                    <input type="number" name="sort_order" class="form-control" value="<?=(int)$s['sort_order']?>">
                </div>
            </div>
            <div style="display:flex;gap:10px">
                <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Update</button>
                <a href="success.php" class="btn btn-secondary">Cancel</a>
            </div>
        </form>
    </div>
</div>
<script>function previewImg(i){const r=new FileReader();r.onload=e=>{const p=document.getElementById('imgPreview');p.src=e.target.result;p.style.display='block'};r.readAsDataURL(i.files[0]);}</script>
<?php require_once dirname(__DIR__) . '/includes/footer.php'; ?>
