@extends('layouts.layoutadmin')
@section('title', 'Kelola Video')

@section('styles')
    <link rel="stylesheet" href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css">
    <style>
        /* Custom style untuk dropdown agar warnanya sesuai tone utama */
        select,
        select:focus,
        select:active {
            background: #f4f4f4;
            color: #23272b;
            border: 1px solid #22c1b6;
            border-radius: 8px;
            padding: 10px 14px;
            font-size: 1rem;
            outline: none;
            box-shadow: none;
            transition: border 0.2s;
        }

        select:focus {
            border-color: #1da89c;
        }

        option {
            background: #fff;
            color: #23272b;
        }

        /* Toast Notification Styles */
        .toast-container {
            position: fixed;
            top: 20px;
            right: 20px;
            z-index: 9999;
            display: flex;
            flex-direction: column;
            gap: 10px;
            max-width: 400px;
        }

        .toast {
            background: white;
            padding: 16px 20px;
            border-radius: 8px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
            display: flex;
            align-items: center;
            gap: 12px;
            min-width: 300px;
            animation: slideInRight 0.6s cubic-bezier(0.16, 1, 0.3, 1);
            border-left: 4px solid;
            position: relative;
            overflow: hidden;
            transform: translateX(0);
            will-change: transform, opacity;
        }

        .toast.toast-success {
            border-left-color: #10b981;
        }

        .toast.toast-error {
            border-left-color: #ef4444;
        }

        .toast.toast-info {
            border-left-color: #3b82f6;
        }

        .toast.toast-warning {
            border-left-color: #f59e0b;
        }

        .toast-icon {
            font-size: 24px;
            flex-shrink: 0;
        }

        .toast-success .toast-icon {
            color: #10b981;
        }

        .toast-error .toast-icon {
            color: #ef4444;
        }

        .toast-info .toast-icon {
            color: #3b82f6;
        }

        .toast-warning .toast-icon {
            color: #f59e0b;
        }

        .toast-content {
            flex: 1;
        }

        .toast-message {
            color: #1f2937;
            font-size: 14px;
            line-height: 1.5;
            font-weight: 500;
        }

        .toast-close {
            background: none;
            border: none;
            color: #6b7280;
            font-size: 20px;
            cursor: pointer;
            padding: 0;
            width: 24px;
            height: 24px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 4px;
            transition: all 0.2s;
            flex-shrink: 0;
        }

        .toast-close:hover {
            background: #f3f4f6;
            color: #1f2937;
        }

        .toast-progress {
            position: absolute;
            bottom: 0;
            left: 0;
            height: 3px;
            background: currentColor;
            opacity: 0.3;
            animation: toastProgress 5s linear;
        }

        .toast-success .toast-progress {
            color: #10b981;
        }

        .toast-error .toast-progress {
            color: #ef4444;
        }

        .toast-info .toast-progress {
            color: #3b82f6;
        }

        .toast-warning .toast-progress {
            color: #f59e0b;
        }

        @keyframes slideInRight {
            0% {
                transform: translateX(120%);
                opacity: 0;
            }

            100% {
                transform: translateX(0);
                opacity: 1;
            }
        }

        @keyframes slideOutRight {
            0% {
                transform: translateX(0);
                opacity: 1;
            }

            100% {
                transform: translateX(120%);
                opacity: 0;
            }
        }

        @keyframes toastProgress {
            from {
                width: 100%;
            }

            to {
                width: 0%;
            }
        }

        .toast.hiding {
            animation: slideOutRight 0.4s ease-in forwards;
        }

        body {
            background: #f4f4f4;
            font-family: 'Inter', Arial, sans-serif;
        }

        .admin-dashboard {
            display: flex;
            min-height: 100vh;
            background: #f4f4f4;
        }

        .sidebar {
            width: 260px;
            background: #23272b;
            color: #fff;
            display: flex;
            flex-direction: column;
            padding: 24px 0 0 0;
            border-right: 1px solid #e5e7eb;
        }

        .sidebar .sidebar-header {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 0 24px 24px 24px;
            font-size: 1.1rem;
            font-weight: 700;
        }

        .sidebar .sidebar-header i {
            font-size: 1.6rem;
            color: #22c1b6;
        }

        .sidebar .sidebar-header span {
            font-size: 1.1rem;
            font-weight: 700;
            color: #fff;
        }

        .sidebar-nav {
            flex: 1;
            padding: 0 0 0 0;
        }

        .sidebar-nav ul {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .sidebar-nav li {
            margin-bottom: 4px;
        }

        .sidebar-nav a {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 10px 28px;
            color: #bfc5c9;
            text-decoration: none;
            border-radius: 8px 0 0 8px;
            font-size: 1rem;
            transition: background 0.2s, color 0.2s;
        }

        .sidebar-nav a.active,
        .sidebar-nav a:hover {
            background: #22c1b6;
            color: #fff;
        }

        .sidebar-footer {
            padding: 18px 24px;
            border-top: 1px solid #333;
            background: #23272b;
            display: flex;
            align-items: center;
            gap: 12px;
            font-size: 0.98rem;
        }

        .sidebar-footer .avatar {
            width: 36px;
            height: 36px;
            border-radius: 50%;
            background: #444;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #fff;
            font-size: 1.2rem;
            font-weight: 700;
        }

        .sidebar-footer .user-info {
            display: flex;
            flex-direction: column;
            gap: 2px;
        }

        .sidebar-footer .user-info span {
            color: #fff;
            font-weight: 600;
            font-size: 1rem;
        }

        .sidebar-footer .user-info small {
            color: #bfc5c9;
            font-size: 0.95rem;
        }

        .main {
            flex: 1;
            display: flex;
            flex-direction: column;
            background: #f4f4f4;
        }

        .main-header {
            background: #fff;
            padding: 24px 32px 16px 32px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid #e5e7eb;
        }

        /* Keep admin header/navbar white regardless of site dark/light mode */
        body.dark-mode .admin-dashboard .main-header,
        .admin-dashboard .main-header {
            background: #ffffff !important;
            color: #222 !important;
            border-bottom: 1px solid #e5e7eb !important;
        }

        /* Ensure header child elements keep their intended colors in dark mode */
        body.dark-mode .admin-dashboard .main-header .title,
        body.dark-mode .admin-dashboard .main-header .subtitle,
        body.dark-mode .admin-dashboard .main-header .header-actions,
        body.dark-mode .admin-dashboard .main-header .header-actions * {
            color: inherit !important;
            background: transparent !important;
        }

        .title {
            font-size: 1.4rem;
            font-weight: 700;
            color: #222;
        }

        .subtitle {
            color: #6b7280;
            font-size: 1.05rem;
            margin-top: 2px;
        }

        .header-actions {
            display: flex;
            align-items: center;
            gap: 16px;
        }

        .btn-primary {
            background: #22c1b6;
            color: #fff;
            border: none;
            border-radius: 8px;
            padding: 8px 22px;
            font-weight: 600;
            font-size: 1rem;
            cursor: pointer;
            transition: background 0.2s;
        }

        .btn-primary:hover {
            background: #1da89c;
        }

        .btn-secondary {
            background: #6b7280;
            color: #fff;
            border: none;
            border-radius: 8px;
            padding: 8px 22px;
            font-weight: 600;
            font-size: 1rem;
            cursor: pointer;
            transition: background 0.2s;
            margin-right: 12px;
        }

        .btn-secondary:hover {
            background: #4b5563;
        }

        .btn-danger {
            background: #ef4444;
            color: #fff;
            border: none;
            border-radius: 8px;
            padding: 8px 22px;
            font-weight: 600;
            font-size: 1rem;
            cursor: pointer;
            transition: background 0.2s;
        }

        .btn-danger:hover {
            background: #dc2626;
        }

        .main-content {
            padding: 32px;
        }

        .content-section {
            background: #fff;
            border-radius: 12px;
            box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
            padding: 32px;
            margin-top: 2rem;
        }

        .section-title {
            font-size: 1.3rem;
            font-weight: 700;
            color: #23272b;
            margin-bottom: 1.5rem;
        }

        .badge {
            display: inline-block;
            padding: 6px 16px;
            border-radius: 8px;
            font-weight: 600;
            font-size: 0.95rem;
        }

        .alert-success {
            background: #d1fae5;
            color: #065f46;
            border-radius: 8px;
            padding: 10px 18px;
            margin-bottom: 1rem;
        }

        .alert-error {
            background: #fee2e2;
            color: #991b1b;
            border-radius: 8px;
            padding: 10px 18px;
            margin-bottom: 1rem;
        }

        #videoTable {
            background: #fff;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 1px 6px rgba(0, 0, 0, 0.04);
            margin-top: 16px;
        }

        #videoTable th {
            background: #f8f9fa;
            color: #374151;
            font-weight: 600;
            padding: 12px 16px;
            border-bottom: 2px solid #e5e7eb;
        }

        #videoTable td {
            padding: 12px 16px;
            border-bottom: 1px solid #e5e7eb;
            color: #6b7280;
            vertical-align: middle;
        }

        #videoTable tr:hover {
            background: #f8f9fa;
        }

        .action-buttons {
            display: flex;
            gap: 8px;
        }

        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
        }

        .modal-content {
            background: #fff;
            margin: 10% auto;
            padding: 24px;
            border-radius: 12px;
            width: 90%;
            max-width: 500px;
            position: relative;
        }

        .close {
            position: absolute;
            right: 24px;
            top: 24px;
            font-size: 24px;
            cursor: pointer;
        }

        @media (max-width: 900px) {
            .content-section {
                padding: 1rem 0.5rem;
            }

            .main-header {
                flex-direction: column;
                align-items: flex-start;
                gap: 1rem;
            }
        }
    </style>
@endsection

@section('content')

    <div class="admin-dashboard">
        <!-- memanggil komponen side bar dari admin.components.sidebar -->
        @include('admin.components.sidebar')
        <main class="main">
            <div class="main-header">
                <div>
                    <div class="title">Kelola Video</div>
                    <div class="subtitle">Manajemen Video Edukasi & Dokumentasi</div>
                </div>
                <div class="header-actions" style="display:flex;gap:12px;">
                    <button type="button" class="btn-primary" onclick="openAddVideoModal()">
                        <i class="fas fa-plus"></i> Tambah Video
                    </button>
                    <button type="button" class="btn-secondary" onclick="loadVideos()">
                        <i class="fas fa-sync"></i> Refresh
                    </button>
                </div>
            </div>
            <div class="main-content">
                <!-- Toast Container -->
                <div class="toast-container" id="toast-container"></div>
                <div class="content-section"
                    style="background:#fff;border-radius:16px;padding:2rem 2.5rem;box-shadow:0 2px 12px rgba(0,0,0,0.08);margin-top:2rem;">
                    <div class="section-title" style="font-size:1.3rem;font-weight:700;color:#23272b;margin-bottom:1.5rem;">
                        Daftar Video</div>
                    <div id="videoListContainer"></div>
                    <style>
                        /* Tambahkan jarak antara fitur DataTables dan tabel */
                        .dataTables_wrapper .dataTables_length,
                        .dataTables_wrapper .dataTables_filter {
                            margin-bottom: 18px !important;
                        }

                        .dataTables_wrapper .dataTables_filter {
                            margin-left: 1rem;
                        }

                        .dataTables_wrapper .dataTables_length {
                            margin-right: 1rem;
                        }

                        /* Hilangkan background pada kolom yang sedang disorting */
                        table.dataTable thead th.sorting,
                        table.dataTable thead th.sorting_asc,
                        table.dataTable thead th.sorting_desc {
                            background: none !important;
                        }

                        /* Tampilkan segitiga unicode untuk sort, bukan kotak */
                        table.dataTable thead th.sorting:after {
                            content: '\25B4';
                            /* segitiga abu-abu ke atas */
                            color: #bfc5c9;
                            font-size: 0.9em;
                            margin-left: 6px;
                            opacity: 0.7;
                        }

                        table.dataTable thead th.sorting_asc:after {
                            content: '\25B2';
                            /* segitiga solid ke atas */
                            color: #22c1b6;
                            font-size: 0.9em;
                            margin-left: 6px;
                        }

                        table.dataTable thead th.sorting_desc:after {
                            content: '\25BC';
                            /* segitiga solid ke bawah */
                            color: #22c1b6;
                            font-size: 0.9em;
                            margin-left: 6px;
                        }

                        /* Sembunyikan segitiga kedua jika ada */
                        table.dataTable thead th.sorting:before,
                        table.dataTable thead th.sorting_asc:before,
                        table.dataTable thead th.sorting_desc:before {
                            display: none !important;
                        }
                    </style>
                </div>
            </div>

            <!-- Add/Edit Video Modal -->
            <div id="videoModal" class="modal" style="display:none;">
                <div class="modal-content" style="max-width:480px;">
                    <div class="modal-header">
                        <div class="modal-title" id="videoModalTitle">Tambah Video</div>
                        <span class="close" onclick="closeVideoModal()">&times;</span>
                    </div>
                    <div class="form-container">
                        <form id="videoForm">
                            <input type="hidden" id="video_id" name="id">
                            <div class="form-group">
                                <label for="video_name">Judul Video</label>
                                <input type="text" id="video_name" name="name" required maxlength="255"
                                    style="width:100%;padding:10px 14px;border-radius:8px;border:1px solid #d1d5db;">
                            </div>
                            <div class="form-group">
                                <label for="video_description">Deskripsi</label>
                                <textarea id="video_description" name="description" maxlength="1000"
                                    style="width:100%;padding:10px 14px;border-radius:8px;border:1px solid #d1d5db;"></textarea>
                            </div>
                            <div class="form-group">
                                <label for="video_link">Link Video (YouTube/URL)</label>
                                <input type="url" id="video_link" name="link_video" required
                                    style="width:100%;padding:10px 14px;border-radius:8px;border:1px solid #d1d5db;">
                            </div>
                            <div class="form-group">
                                <label for="video_is_inherited">Status Distribusi</label>
                                <select id="video_is_inherited" name="is_inherited" style="width:100%;">
                                    <option value="0">Tidak Diturunkan</option>
                                    <option value="1">Diturunkan</option>
                                </select>
                            </div>
                            <div class="form-actions"
                                style="display:flex;justify-content:flex-end;gap:12px;margin-top:18px;">
                                <button type="button" class="btn-secondary" onclick="closeVideoModal()">Batal</button>
                                <button type="submit" class="btn-primary">Simpan</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </main>
    </div>

    <script>
        const baseUrl = '{{ env('BASE_URL') }}';
        let videoList = [];

        function showAlert(message, type = 'success') {
            const toastContainer = document.getElementById('toast-container');

            // Create toast element
            const toast = document.createElement('div');
            toast.className = `toast toast-${type}`;

            // Determine icon based on type
            let icon = '';
            switch (type) {
                case 'success':
                    icon = '<i class="fas fa-check-circle"></i>';
                    break;
                case 'error':
                    icon = '<i class="fas fa-exclamation-circle"></i>';
                    break;
                case 'warning':
                    icon = '<i class="fas fa-exclamation-triangle"></i>';
                    break;
                case 'info':
                    icon = '<i class="fas fa-info-circle"></i>';
                    break;
                default:
                    icon = '<i class="fas fa-check-circle"></i>';
            }

            toast.innerHTML = `
		<div class="toast-icon">${icon}</div>
		<div class="toast-content">
			<div class="toast-message">${message}</div>
		</div>
		<button class="toast-close" onclick="this.parentElement.remove()">
			<i class="fas fa-times"></i>
		</button>
		<div class="toast-progress"></div>
	`;

            // Add toast with slight delay for smoother appearance
            requestAnimationFrame(() => {
                toastContainer.appendChild(toast);
            });

            // Auto remove after 5 seconds
            setTimeout(() => {
                toast.classList.add('hiding');
                setTimeout(() => {
                    if (toast.parentElement) {
                        toast.remove();
                    }
                }, 400);
            }, 5000);
        }

        function getAuthToken() {
            // Coba dari localStorage, session, atau window
            return localStorage.getItem('accessToken') || localStorage.getItem('auth_token') || '';
        }

        async function loadVideos() {
            try {
                const token = getAuthToken();
                const res = await fetch(baseUrl + '/api/admin/video', {
                    headers: {
                        'Authorization': `Bearer ${token}`
                    }
                });
                if (res.status === 401) {
                    if (window.handleSessionExpired) window.handleSessionExpired();
                    return;
                }
                const data = await res.json();
                videoList = Array.isArray(data) ? data : (data.data || []);
                renderVideoList();
            } catch (err) {
                showAlert('Gagal memuat video: ' + err.message, 'error');
            }
        }

        function renderVideoList() {
            const container = document.getElementById('videoListContainer');
            if (!videoList || videoList.length === 0) {
                container.innerHTML = '<div style="padding:2rem;text-align:center;color:#9ca3af;">Belum ada video.</div>';
                return;
            }
            // Ambil site id admin dari local/session (samakan dengan berita)
            let adminSiteId = null;
            const sessionSiteId = '{{ session('auth.user.site_id') ?? '' }}';
            if (sessionSiteId) {
                adminSiteId = parseInt(sessionSiteId);
            } else {
                const userData = localStorage.getItem('auth_user');
                if (userData) {
                    try {
                        const user = JSON.parse(userData);
                        adminSiteId = user.site_id;
                    } catch (e) {
                        adminSiteId = null;
                    }
                }
            }

            container.innerHTML = `<table id="videoTable" class="display" style="width:100%;border-radius:12px;overflow:hidden;">
		<thead>
			<tr style="background:#f3f4f6;color:#23272b;font-weight:600;">
				<th>Judul</th>
				<th>Deskripsi</th>
				<th class="no-sort">Preview</th>
				<th>Distribusi</th>
				<th>Site ID</th>
				<th>Tanggal</th>
				<th class="no-sort">Aksi</th>
			</tr>
		</thead>
		<tbody>
		${videoList.map(video => {
			const isMine = String(video.site_id) === String(adminSiteId);
			return `
    			<tr>
    				<td><strong>${video.name}</strong></td>
    				<td>${video.description || ''}</td>
    				<td>
    					${video.link_video ? `<iframe width="180" height="100" src="${getEmbedUrl(video.link_video)}" frameborder="0" allowfullscreen style="border-radius:8px;border:1px solid #e5e7eb;"></iframe>` : '-'}
    				</td>
    				<td>
    					<span class="badge" style="display:inline-block;padding:6px 16px;border-radius:8px;font-weight:600;font-size:0.95rem;${video.is_inherited ? 'background:#14b8a6;color:#fff;' : 'background:#f59e42;color:#fff;'}">
    						${video.is_inherited ? 'Diturunkan' : 'Tidak'}
    					</span>
    				</td>
    				<td>
    					<span style="color:black" class="badge ${isMine ? 'badge-success' : 'badge-warning'}">Site ${video.site_id || 'N/A'}</span>
    				</td>
    				<td>${video.createdAt ? new Date(video.createdAt).toLocaleDateString('id-ID') : '-'}</td>
    				<td class="action-buttons" style="display:flex;gap:8px;">
    					${isMine ? `
						<button class="btn-primary" style="padding:6px 18px;border-radius:8px;font-weight:600;font-size:0.95rem;margin-right:6px;" onclick="openEditVideoModal(${video.id})"><i class="fas fa-edit"></i> Edit</button>
						<button class="btn-danger" style="padding:6px 18px;border-radius:8px;font-weight:600;font-size:0.95rem;" onclick="deleteVideo(${video.id})"><i class="fas fa-trash"></i> Hapus</button>
					` : `
						<span class="badge badge-warning">Read Only</span>
					`}
    				</td>
    			</tr>
    			`;
		}).join('')}
		</tbody></table>`;
            // Activate DataTables
            function activateDataTable() {
                $('#videoTable').DataTable({
                    columnDefs: [{
                            orderable: false,
                            targets: [2, 5]
                        } // Preview dan Aksi tidak bisa sort
                    ],
                    order: [
                        [0, 'asc']
                    ], // Default sort by Judul
                    language: {
                        search: "Cari:",
                        lengthMenu: "Tampilkan _MENU_ entri",
                        info: "Menampilkan _START_ sampai _END_ dari _TOTAL_ entri",
                        paginate: {
                            previous: "Sebelumnya",
                            next: "Berikutnya"
                        }
                    }
                });
            }
            if (window.$ && window.$.fn.dataTable) {
                activateDataTable();
            } else {
                // If jQuery/DataTables not loaded, try to load
                const scriptJq = document.createElement('script');
                scriptJq.src = 'https://code.jquery.com/jquery-3.6.0.min.js';
                scriptJq.onload = () => {
                    const scriptDt = document.createElement('script');
                    scriptDt.src = 'https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js';
                    scriptDt.onload = () => {
                        activateDataTable();
                    };
                    document.body.appendChild(scriptDt);
                };
                document.body.appendChild(scriptJq);
            }
        }

        function getEmbedUrl(url) {
            // If YouTube, convert to embed
            const ytMatch = url.match(/(?:youtube\.com\/watch\?v=|youtu\.be\/)([\w-]+)/);
            if (ytMatch) return `https://www.youtube.com/embed/${ytMatch[1]}`;
            return url;
        }

        function openAddVideoModal() {
            document.getElementById('videoModalTitle').textContent = 'Tambah Video';
            document.getElementById('videoForm').reset();
            document.getElementById('video_id').value = '';
            document.getElementById('videoModal').style.display = 'block';
        }

        function openEditVideoModal(id) {
            const video = videoList.find(v => v.id === id);
            if (!video) return;
            document.getElementById('videoModalTitle').textContent = 'Edit Video';
            document.getElementById('video_id').value = video.id;
            document.getElementById('video_name').value = video.name;
            document.getElementById('video_description').value = video.description || '';
            document.getElementById('video_link').value = video.link_video;
            document.getElementById('video_is_inherited').value = video.is_inherited ? '1' : '0';
            document.getElementById('videoModal').style.display = 'block';
        }

        function closeVideoModal() {
            document.getElementById('videoModal').style.display = 'none';
        }

        document.getElementById('videoForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            const id = document.getElementById('video_id').value;
            const name = document.getElementById('video_name').value;
            const description = document.getElementById('video_description').value;
            const link_video = document.getElementById('video_link').value;
            const is_inherited = document.getElementById('video_is_inherited').value === '1';
            const payload = {
                name,
                description,
                link_video,
                is_inherited
            };
            const token = getAuthToken();
            try {
                let res;
                const headers = {
                    'Content-Type': 'application/json',
                    'Authorization': `Bearer ${token}`
                };
                if (id) {
                    res = await fetch(baseUrl + '/api/admin/video/' + id, {
                        method: 'PUT',
                        headers,
                        body: JSON.stringify(payload)
                    });
                } else {
                    res = await fetch(baseUrl + '/api/admin/video', {
                        method: 'POST',
                        headers,
                        body: JSON.stringify(payload)
                    });
                }
                if (res.status === 401) {
                    if (window.handleSessionExpired) window.handleSessionExpired();
                    return;
                }
                if (!res.ok) throw new Error('Gagal menyimpan data');
                showAlert('Video berhasil disimpan!', 'success');
                closeVideoModal();
                loadVideos();
            } catch (err) {
                showAlert('Gagal simpan: ' + err.message, 'error');
            }
        });

        async function deleteVideo(id) {
            if (!confirm('Hapus video ini?')) return;
            const token = getAuthToken();
            try {
                const res = await fetch(baseUrl + '/api/admin/video/' + id, {
                    method: 'DELETE',
                    headers: {
                        'Authorization': `Bearer ${token}`
                    }
                });
                if (res.status === 401) {
                    if (window.handleSessionExpired) window.handleSessionExpired();
                    return;
                }
                if (!res.ok) throw new Error('Gagal hapus data');
                showAlert('Video berhasil dihapus!', 'success');
                loadVideos();
            } catch (err) {
                showAlert('Gagal hapus: ' + err.message, 'error');
            }
        }

        document.addEventListener('DOMContentLoaded', loadVideos);
    </script>
@endsection
