import { useState } from 'react'; interface VideoEntry { uid: number; // ID nội bộ để quản lý danh sách link: string; id: string; // YouTube ID sau khi tách title: string; category: string; customCategory: string; level: string; } const CATEGORIES = ["Cardio", "Cơ Bụng", "Yoga", "HIIT", "Ngực", "Chân", "Toàn thân", "Khác"]; const LEVELS = ["Dễ", "Vừa", "Khó"]; export default function Admin() { const [entries, setEntries] = useState([ { uid: Date.now(), link: '', id: '', title: '', category: 'Cardio', customCategory: '', level: 'Vừa' } ]); const [jsonOutput, setJsonOutput] = useState(''); const [copied, setCopied] = useState(false); // Hàm tự động tách ID Youtube từ Link const extractYouTubeID = (url: string) => { const regExp = /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/i; const match = url.match(regExp); return match ? match[1] : ''; }; const handleUpdate = (index: number, field: keyof VideoEntry, value: string) => { const newEntries = [...entries]; newEntries[index][field] = value as never; // Nếu đang dán link, tự động tách ID luôn if (field === 'link') { newEntries[index].id = extractYouTubeID(value); } setEntries(newEntries); }; const handleAddRow = () => { setEntries([ ...entries, { uid: Date.now(), link: '', id: '', title: '', category: 'Cardio', customCategory: '', level: 'Vừa' } ]); }; const handleRemoveRow = (index: number) => { if (entries.length === 1) return; // Giữ lại ít nhất 1 dòng const newEntries = [...entries]; newEntries.splice(index, 1); setEntries(newEntries); }; const handleGenerate = () => { // Lọc bỏ những dòng chưa có ID const validEntries = entries.filter(e => e.id.trim() !== ''); const finalData = validEntries.map((e, index) => ({ id: `vid_${Date.now()}_${index}`, // Sinh ra ID ngẫu nhiên cho video youtube_id: e.id, // Gán mã Youtube ID vào đúng trường youtube_id title: e.title.trim() || "Chưa có tiêu đề", category: e.category === 'Khác' ? e.customCategory.trim() : e.category, level: e.level })); setJsonOutput(JSON.stringify(finalData, null, 2)); setCopied(false); }; const handleCopy = () => { if (!jsonOutput) return; navigator.clipboard.writeText(jsonOutput); setCopied(true); setTimeout(() => setCopied(false), 2000); }; return (

🛠️ Trạm Tạo Dữ Liệu

{entries.map((entry, index) => (
{/* Cột Link */}
handleUpdate(index, 'link', e.target.value)} placeholder="https://youtu.be/..." className="w-full border border-gray-300 rounded-md p-2 text-sm focus:ring-2 focus:ring-blue-500 outline-none" /> {entry.id &&

ID: {entry.id}

}
{/* Cột Tiêu đề */}
handleUpdate(index, 'title', e.target.value)} placeholder="Vd: 10 Phút Đốt Mỡ" className="w-full border border-gray-300 rounded-md p-2 text-sm focus:ring-2 focus:ring-blue-500 outline-none" />
{/* Cột Phân loại */}
{entry.category === 'Khác' && ( handleUpdate(index, 'customCategory', e.target.value)} placeholder="Nhập tag..." className="w-full border border-gray-300 rounded-md p-1 mt-2 text-sm" /> )}
{/* Cột Mức độ */}
))}
{jsonOutput && (
            {jsonOutput}
          
)}
); }