<?php
$pageTitle = 'Success Stories';
require_once dirname(__DIR__) . '/includes/header.php';

if (isset($_GET['delete'])) {
    db()->query("DELETE FROM success_stories WHERE id=".(int)$_GET['delete']);
    flash('Story deleted.','success');
    redirect(ADMIN_URL.'/pages/success.php');
}

$stories = db()->query("SELECT * FROM success_stories ORDER BY sort_order,id DESC")->fetch_all(MYSQLI_ASSOC);
?>
<div class="page-header">
    <h1>Success Stories</h1>
    <div class="page-header-actions">
        <a href="success-add.php" class="btn btn-primary"><i class="fas fa-plus"></i> Add Story</a>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <i class="fas fa-star" style="color:var(--accent)"></i>
        <h2>All Success Stories</h2>
        <span style="color:var(--muted);font-size:13px;margin-left:auto"><?= count($stories) ?> stories</span>
    </div>
    <div class="table-wrap">
        <?php if (empty($stories)): ?>
        <div style="padding:48px;text-align:center;color:var(--muted)">
            <i class="fas fa-star" style="font-size:36px;opacity:.3;display:block;margin-bottom:12px"></i>
            No success stories yet. <a href="success-add.php" style="color:var(--brand)">Add first story</a>
        </div>
        <?php else: ?>
        <table>
            <thead>
                <tr><th>Photo</th><th>Student</th><th>Destination</th><th>University</th><th>Year</th><th>Status</th><th>Actions</th></tr>
            </thead>
            <tbody>
            <?php foreach ($stories as $s): ?>
            <tr>
                <td><?php if($s['photo']): ?><img src="<?=e(SITE_URL.'/'.$s['photo'])?>" style="width:40px;height:40px;border-radius:50%;object-fit:cover;border:2px solid var(--border)"><?php else: ?><div style="width:40px;height:40px;background:#f3f4f6;border-radius:50%;display:grid;place-items:center;color:#9ca3af;font-weight:700"><?=strtoupper(substr($s['student_name'],0,1))?></div><?php endif; ?></td>
                <td style="font-weight:600"><?= e($s['student_name']) ?></td>
                <td style="color:var(--muted)"><?= e($s['destination']) ?></td>
                <td style="color:var(--muted);font-size:13px;max-width:160px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap"><?= e($s['university']) ?></td>
                <td><?= $s['year'] ?: '—' ?></td>
                <td><span class="badge <?= $s['status']==='active'?'badge-green':'badge-yellow' ?>"><?= ucfirst($s['status']) ?></span></td>
                <td>
                    <div style="display:flex;gap:6px">
                        <a href="success-edit.php?id=<?= $s['id'] ?>" class="btn btn-secondary btn-sm btn-icon" title="Edit"><i class="fas fa-edit"></i></a>
                        <button onclick="confirmDelete('success.php?delete=<?= $s['id'] ?>','<?= e(addslashes($s['student_name'])) ?>')" class="btn btn-danger btn-sm btn-icon" title="Delete"><i class="fas fa-trash"></i></button>
                    </div>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>
    </div>
</div>

<?php require_once dirname(__DIR__) . '/includes/footer.php'; ?>
