<?php require_once '../includes/header.php'; ?>

<h1 class="h2 mb-4">Tutorials</h1>

<?php $flash = getFlash(); if ($flash): ?>
<div class="alert alert-<?= $flash['type'] ?> alert-dismissible fade show" role="alert">
    <?= htmlspecialchars($flash['message']) ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<div class="card shadow-sm">
    <div class="card-header bg-white d-flex justify-content-between align-items-center">
        <h5 class="mb-0">All Tutorials</h5>
        <a href="tutorial_add.php" class="btn btn-primary btn-sm"><i class="bi bi-plus-circle"></i> Add Tutorial</a>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead class="table-light">
                    <tr>
                        <th>ID</th>
                        <th>Thumb</th>
                        <th>Title</th>
                        <th>YouTube URL</th>
                        <th>Sort Order</th>
                        <th>Active</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                <?php
                $tuts = $pdo->query("SELECT * FROM tutorials ORDER BY sort_order ASC, created_at DESC")->fetchAll();
                if (empty($tuts)): ?>
                    <tr><td colspan="7" class="text-center py-3">No tutorials found.</td></tr>
                <?php else: foreach ($tuts as $t): ?>
                    <tr>
                        <td><?= $t['id'] ?></td>
                        <td>
                            <?php if ($t['thumbnail']): ?>
                                <img src="../assets/uploads/<?= htmlspecialchars($t['thumbnail']) ?>" width="60" style="object-fit:cover;" class="rounded">
                            <?php else: ?>
                                <span class="text-muted">—</span>
                            <?php endif; ?>
                        </td>
                        <td><?= htmlspecialchars($t['title']) ?></td>
                        <td><a href="<?= htmlspecialchars($t['youtube_url']) ?>" target="_blank">Watch</a></td>
                        <td><?= $t['sort_order'] ?></td>
                        <td><?= $t['is_active'] ? '<span class="badge bg-success">Yes</span>' : '<span class="badge bg-secondary">No</span>' ?></td>
                        <td class="text-end">
                            <a href="tutorial_edit.php?id=<?= $t['id'] ?>" class="btn btn-sm btn-warning"><i class="bi bi-pencil"></i></a>
                            <a href="tutorial_delete.php?id=<?= $t['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Delete this tutorial?')"><i class="bi bi-trash"></i></a>
                        </td>
                    </tr>
                <?php endforeach; endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php require_once '../includes/footer.php'; ?>