<?php
require_once __DIR__ . '/../includes/bootstrap.php';
require_admin();

$grade = (string) ($_GET['grade'] ?? '');
$where = [];
$params = [];
if (in_array($grade, ['first_secondary', 'second_secondary'], true)) {
    $where[] = 'grade = ?';
    $params[] = $grade;
}

$sql = 'SELECT * FROM videos';
if ($where) {
    $sql .= ' WHERE ' . implode(' AND ', $where);
}
$sql .= ' ORDER BY grade, sort_order, id';
$stmt = db()->prepare($sql);
$stmt->execute($params);
$videos = $stmt->fetchAll();

$pageTitle = 'إدارة الفيديوهات';
require_once __DIR__ . '/../includes/header.php';
?>
<div class="dashboard-head">
    <div>
        <h1>إدارة فيديوهات كودو</h1>
        <p>أضف روابط فيديوهات YouTube غير المدرجة وحدد الصف والترتيب.</p>
    </div>
    <div class="actions compact-actions">
        <a class="btn btn-outline" href="<?= e(url('admin/dashboard.php')) ?>">إدارة الطلاب</a>
        <a class="btn btn-primary" href="<?= e(url('admin/video_form.php')) ?>">إضافة فيديو</a>
    </div>
</div>

<section class="card">
    <form class="filters" method="get">
        <select name="grade">
            <option value="">كل الصفوف</option>
            <option value="first_secondary" <?= $grade === 'first_secondary' ? 'selected' : '' ?>>الصف الأول الثانوي</option>
            <option value="second_secondary" <?= $grade === 'second_secondary' ? 'selected' : '' ?>>الصف الثاني الثانوي</option>
        </select>
        <button class="btn btn-primary" type="submit">تصفية</button>
        <a class="btn btn-outline" href="<?= e(url('admin/videos.php')) ?>">إلغاء التصفية</a>
    </form>

    <div class="table-wrap">
        <table>
            <thead>
            <tr>
                <th>الفيديو</th>
                <th>الصف</th>
                <th>الترتيب</th>
                <th>النوع</th>
                <th>الحالة</th>
                <th>الإجراءات</th>
            </tr>
            </thead>
            <tbody>
            <?php if (!$videos): ?>
                <tr><td colspan="6">لم تتم إضافة فيديوهات بعد.</td></tr>
            <?php endif; ?>
            <?php foreach ($videos as $video): ?>
                <tr>
                    <td>
                        <div class="video-table-title">
                            <img src="<?= e(youtube_thumbnail_url($video['youtube_id'])) ?>" alt="">
                            <div>
                                <strong><?= e($video['title']) ?></strong>
                                <a href="<?= e(youtube_watch_url($video['youtube_id'])) ?>" target="_blank" rel="noopener">فتح على YouTube</a>
                            </div>
                        </div>
                    </td>
                    <td><?= e(grade_text($video['grade'])) ?></td>
                    <td><?= (int) $video['sort_order'] ?></td>
                    <td><?= (int) $video['is_free'] === 1 ? '<span class="status status-approved">مجاني</span>' : '<span class="status status-pending">مدفوع</span>' ?></td>
                    <td><?= (int) $video['is_active'] === 1 ? '<span class="status status-approved">منشور</span>' : '<span class="status status-suspended">مخفي</span>' ?></td>
                    <td>
                        <div class="actions compact-actions">
                            <a class="btn btn-small btn-outline" href="<?= e(url('admin/video_form.php?id=' . (int) $video['id'])) ?>">تعديل</a>
                            <form method="post" action="<?= e(url('admin/video_action.php')) ?>">
                                <input type="hidden" name="csrf_token" value="<?= e(csrf_token()) ?>">
                                <input type="hidden" name="video_id" value="<?= (int) $video['id'] ?>">
                                <input type="hidden" name="action" value="toggle">
                                <button class="btn btn-small btn-warning" type="submit"><?= (int) $video['is_active'] === 1 ? 'إخفاء' : 'نشر' ?></button>
                            </form>
                            <form method="post" action="<?= e(url('admin/video_action.php')) ?>" onsubmit="return confirm('هل تريد حذف الفيديو من المنصة؟')">
                                <input type="hidden" name="csrf_token" value="<?= e(csrf_token()) ?>">
                                <input type="hidden" name="video_id" value="<?= (int) $video['id'] ?>">
                                <input type="hidden" name="action" value="delete">
                                <button class="btn btn-small btn-danger" type="submit">حذف</button>
                            </form>
                        </div>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>
<?php require_once __DIR__ . '/../includes/footer.php'; ?>
