<?php
/**
 * Admin — Video Testimonials Listing
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
?>
<div class="admin-section">
    <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:20px; flex-wrap:wrap; gap:12px;">
        <h2 style="margin:0; font-size:20px;">Video Testimonials</h2>
        <a href="<?= $bp ?>/admin/video-testimonials/create" class="admin-btn" style="background:#4A6CF7; color:#fff; text-decoration:none; padding:8px 18px; border-radius:8px; font-size:13px; display:inline-flex; align-items:center; gap:6px;">
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
            Add Video
        </a>
    </div>

    <?php if (empty($videos)): ?>
    <div style="text-align:center; padding:50px 20px; color:#999;">
        <svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="#ccc" stroke-width="1.5" style="margin-bottom:10px;">
            <polygon points="23 7 16 12 23 17 23 7"/><rect x="1" y="5" width="15" height="14" rx="2" ry="2"/>
        </svg>
        <p style="font-size:16px; margin:0;">No video testimonials yet.</p>
        <p style="font-size:13px; margin:4px 0 0;">Click "Add Video" to create one.</p>
    </div>
    <?php else: ?>
    <div class="admin-table-wrap" style="overflow-x:auto;">
        <table class="admin-table">
            <thead>
                <tr>
                    <th style="width:60px;">Thumb</th>
                    <th>Student Name</th>
                    <th>Course</th>
                    <th>Video URL</th>
                    <th>Status</th>
                    <th>Order</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($videos as $v): ?>
                <tr>
                    <td>
                        <?php
                        $thumb = $v['thumbnail'] ?? '';
                        $ytId = \App\Models\VideoTestimonial::extractYoutubeId($v['video_url']);
                        if ($ytId): ?>
                        <img src="https://img.youtube.com/vi/<?= $ytId ?>/default.jpg" alt="" style="width:50px; height:36px; border-radius:4px; object-fit:cover;">
                        <?php elseif ($thumb && !str_contains($thumb, 'youtube.com')): ?>
                        <img src="<?= upload_url($thumb) ?>" alt="" style="width:50px; height:36px; border-radius:4px; object-fit:cover;">
                        <?php else: ?>
                        <div style="width:50px; height:36px; background:#eee; border-radius:4px; display:flex; align-items:center; justify-content:center;">
                            <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="#999"><polygon points="23 7 16 12 23 17 23 7"/><rect x="1" y="5" width="15" height="14" rx="2" ry="2"/></svg>
                        </div>
                        <?php endif; ?>
                    </td>
                    <td style="font-weight:600;"><?= htmlspecialchars($v['student_name'], ENT_QUOTES, 'UTF-8') ?></td>
                    <td style="color:#888; font-size:13px;"><?= htmlspecialchars($v['course_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                    <td style="max-width:200px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; font-size:12px; color:#4A6CF7;">
                        <a href="<?= htmlspecialchars($v['video_url'], ENT_QUOTES, 'UTF-8') ?>" target="_blank"><?= htmlspecialchars($v['video_url'], ENT_QUOTES, 'UTF-8') ?></a>
                    </td>
                    <td>
                        <?php if ($v['status']): ?>
                        <span style="display:inline-block; padding:3px 10px; border-radius:20px; font-size:11px; font-weight:600; color:#fff; background:#2ED573;">Active</span>
                        <?php else: ?>
                        <span style="display:inline-block; padding:3px 10px; border-radius:20px; font-size:11px; font-weight:600; color:#fff; background:#999;">Inactive</span>
                        <?php endif; ?>
                    </td>
                    <td><?= (int) $v['sort_order'] ?></td>
                    <td>
                        <div style="display:flex; gap:6px; flex-wrap:wrap;">
                            <a href="<?= $bp ?>/admin/video-testimonials/<?= (int) $v['id'] ?>/edit" class="admin-btn admin-btn-sm" style="background:#4A6CF7; color:#fff; text-decoration:none; padding:4px 10px; border-radius:6px; font-size:11px;">Edit</a>
                            <form method="POST" action="<?= $bp ?>/admin/video-testimonials/<?= (int) $v['id'] ?>/toggle-status" style="display:inline;">
                                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                <button type="submit" class="admin-btn admin-btn-sm" style="background:<?= $v['status'] ? '#FFA502' : '#2ED573' ?>; color:#fff; padding:4px 10px; border-radius:6px; font-size:11px; border:none; cursor:pointer;"><?= $v['status'] ? 'Disable' : 'Enable' ?></button>
                            </form>
                            <form method="POST" action="<?= $bp ?>/admin/video-testimonials/<?= (int) $v['id'] ?>/delete" style="display:inline;" onsubmit="return confirm('Delete this video testimonial?');">
                                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                <button type="submit" class="admin-btn admin-btn-sm admin-btn-danger" style="padding:4px 10px; border-radius:6px; font-size:11px; border:none; cursor:pointer;">Delete</button>
                            </form>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
</div>
