diff --git a/js/admin.js b/js/admin.js index 6395845..92f39bc 100644 --- a/js/admin.js +++ b/js/admin.js @@ -440,9 +440,9 @@ function normalizeVideothequeRow(row) { } let aspect = row['aspect_ratio'] || row['Aspect_ratio'] || row['AspectRatio'] || ''; - let actors = row['actors'] || row['Actors'] || row['creators'] || ''; + let actors = row['creators'] || row['Actors'] || row['actors'] || ''; let publisher = row['publisher'] || row['Publisher'] || ''; - let director = row['director'] || row['Director'] || row['first_name'] + ' ' + row['last_name'] || ''; + let director = row['first_name'] && row['last_name'] ? (row['first_name'] + ' ' + row['last_name']).trim() : ''; return Object.assign({}, row, { ean: ean, @@ -451,10 +451,111 @@ function normalizeVideothequeRow(row) { aspect_ratio: aspect, actors: actors, publisher: publisher, - director: director.trim() + director: director }); } +function handleVideothequeUpload(input) { + const file = input.files[0]; + if (!file) return; + + const reader = new FileReader(); + reader.onload = async (e) => { + const text = e.target.result; + const parsedData = parseCSV(text); + + if (parsedData.length === 0) { + alert("❌ Fichier CSV vide ou invalide."); + return; + } + + const items = parsedData.map(row => { + const normalizedRow = normalizeVideothequeRow(row); + + const title = row['title'] || row['Title'] || ''; + if (!title) return null; + + const ean = normalizedRow['ean'] || ''; + const publishDate = row['publish_date'] || row['Publish_date'] || row['publishdate'] || ''; + const year = publishDate ? publishDate.split('-')[0] : (row['year'] || row['Year'] || ''); + + const description = row['description'] || row['Description'] || ''; + const length = normalizedRow['length'] || ''; + const discs = normalizedRow['number_of_discs'] || 1; + const aspect = normalizedRow['aspect_ratio'] || ''; + const actors = normalizedRow['actors'] || ''; + const publisher = normalizedRow['publisher'] || ''; + const director = normalizedRow['director'] || ''; + + return { + title: title.trim(), + ean: ean, + year: year, + description: description.trim(), + length: length, + number_of_discs: discs, + aspect_ratio: aspect, + actors: actors, + publisher: publisher, + director: director + }; + }).filter(item => item !== null); + + if (items.length === 0) { + alert("❌ Aucun film valide trouvé dans le fichier CSV."); + return; + } + + showImportModal(items.length, 'videotheque'); + let processed = 0; + + try { + // Traiter par lots de 5 pour éviter la surcharge + for (let i = 0; i < items.length; i += 5) { + const batch = items.slice(i, i + 5); + + const response = await fetch(`${API_URL}?action=import_batch`, { + method: 'POST', + headers: { + 'Authorization': localStorage.getItem('token'), + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ type: 'videotheque', items: batch }) + }); + + if (!response.ok) { + throw new Error(`Erreur serveur ${response.status}`); + } + + const data = await response.json(); + + if (!data.success) { + throw new Error(data.error || "Erreur inconnue"); + } + + processed += batch.length; + updateImportModal(processed, items.length); + + // Petit délai pour éviter le rate limiting + await new Promise(resolve => setTimeout(resolve, 500)); + } + + input.value = ''; + closeImportModal(); + showSuccessModal(`${items.length} édition(s) physique(s) importée(s) avec succès !`); + loadDashboardData(); + + } catch (err) { + console.error("Erreur d'importation : ", err); + closeImportModal(); + alert("❌ Échec de l'import : " + err.message); + input.value = ''; + } + }; + + reader.readAsText(file); +} + function handleVideothequeUpload(input) { const file = input.files[0]; if (!file) return;