document.addEventListener('DOMContentLoaded', function() { const videoId = document.getElementById('video-id').value; const actorSearch = document.getElementById('actor-search'); const actorResults = document.getElementById('actor-results'); const actorsList = document.getElementById('actors-list'); // Debounce search let searchTimeout; actorSearch.addEventListener('input', function(e) { clearTimeout(searchTimeout); const query = e.target.value.trim(); if (query.length < 2) { actorResults.innerHTML = ''; actorResults.classList.add('d-none'); return; } searchTimeout = setTimeout(() => { searchActors(query); }, 300); }); // Search for actors function searchActors(query) { fetch(`/admin/adult-videos/search-actors?q=${encodeURIComponent(query)}`) .then(response => response.json()) .then(data => { actorResults.innerHTML = ''; if (data.data && data.data.length > 0) { data.data.forEach(actor => { const item = document.createElement('div'); item.className = 'list-group-item list-group-item-action'; item.textContent = actor.name; item.addEventListener('click', () => addActorToVideo(actor.id, actor.name)); actorResults.appendChild(item); }); actorResults.classList.remove('d-none'); } else { const noResults = document.createElement('div'); noResults.className = 'list-group-item'; noResults.textContent = 'No actors found'; actorResults.appendChild(noResults); actorResults.classList.remove('d-none'); } }); } // Add actor to video function addActorToVideo(actorId, actorName) { fetch(`/admin/adult-videos/${videoId}/actors`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Requested-With': 'XMLHttpRequest' }, body: JSON.stringify({ actor_id: actorId }) }) .then(response => response.json()) .then(data => { if (data.success) { addActorToList(actorId, actorName); actorSearch.value = ''; actorResults.innerHTML = ''; actorResults.classList.add('d-none'); } else { alert('Failed to add actor: ' + (data.error || 'Unknown error')); } }); } // Add actor to the UI list function addActorToList(actorId, actorName) { const item = document.createElement('div'); item.className = 'd-flex justify-content-between align-items-center mb-2'; item.dataset.actorId = actorId; item.innerHTML = ` ${actorName} `; actorsList.appendChild(item); // Add event listener to the remove button item.querySelector('.remove-actor').addEventListener('click', () => removeActor(actorId, item)); } // Remove actor from video function removeActor(actorId, element) { if (confirm('Are you sure you want to remove this actor?')) { fetch(`/admin/adult-videos/${videoId}/actors/${actorId}`, { method: 'DELETE', headers: { 'X-Requested-With': 'XMLHttpRequest' } }) .then(response => response.json()) .then(data => { if (data.success) { element.remove(); } else { alert('Failed to remove actor: ' + (data.error || 'Unknown error')); } }); } } // Load existing actors function loadActors() { fetch(`/admin/adult/${videoId}/actors`) .then(response => response.json()) .then(data => { if (data.data && data.data.length > 0) { data.data.forEach(actor => { addActorToList(actor.id, actor.name); }); } }); } // Initialize loadActors(); });