import React, { useEffect, useState } from 'react'; import { api, type AppDownloadItem, type AppGuideItem } from '../api'; import { useAuth } from '../auth/AuthContext'; // Icons const IconDownload = () => ( ); const PlatformIcon = ({ platform }: { platform: string }) => { const p = platform.toLowerCase(); if (p.includes('win')) { return ( ); } if (p.includes('mac') || p.includes('apple') || p.includes('ios')) { return ( ); } if (p.includes('linux')) { return ( ); } return ( ); }; export const ApplicationsTab: React.FC = () => { const { staff } = useAuth(); const isSuperAdmin = staff?.email === 'phuocntb@rikkeiacademy.com'; const [activeSubTab, setActiveSubTab] = useState<'apps' | 'guides'>('apps'); // Apps state const [apps, setApps] = useState([]); const [appsLoading, setAppsLoading] = useState(true); // Guides state const [guides, setGuides] = useState([]); const [guidesLoading, setGuidesLoading] = useState(true); // App Modal states const [showAppModal, setShowAppModal] = useState(false); const [editingApp, setEditingApp] = useState(null); const [appName, setAppName] = useState(''); const [appDescription, setAppDescription] = useState(''); const [appDownloadUrl, setAppDownloadUrl] = useState(''); const [appPlatform, setAppPlatform] = useState('Windows'); const [appSaving, setAppSaving] = useState(false); // Guide Modal states const [showGuideModal, setShowGuideModal] = useState(false); const [editingGuide, setEditingGuide] = useState(null); const [guideTitle, setGuideTitle] = useState(''); const [guideUrl, setGuideUrl] = useState(''); const [guideSaving, setGuideSaving] = useState(false); const fetchApps = async () => { try { setAppsLoading(true); const res = await api.listAppDownloads(); setApps(res.data || []); } catch (err) { console.error('Không thể tải danh sách ứng dụng:', err); } finally { setAppsLoading(false); } }; const fetchGuides = async () => { try { setGuidesLoading(true); const res = await api.listAppGuides(); setGuides(res.data || []); } catch (err) { console.error('Không thể tải danh sách hướng dẫn sinh viên:', err); } finally { setGuidesLoading(false); } }; useEffect(() => { fetchApps(); fetchGuides(); }, []); // App Modal Handlers const handleOpenAddApp = () => { setEditingApp(null); setAppName(''); setAppDescription(''); setAppDownloadUrl(''); setAppPlatform('Windows'); setShowAppModal(true); }; const handleOpenEditApp = (app: AppDownloadItem) => { setEditingApp(app); setAppName(app.name); setAppDescription(app.description); setAppDownloadUrl(app.downloadUrl); setAppPlatform(app.platform === 'macOS' ? 'MacOS' : app.platform); setShowAppModal(true); }; const handleSaveApp = async () => { if (!appName.trim() || !appDownloadUrl.trim()) { alert('Vui lòng nhập tên ứng dụng và đường dẫn tải.'); return; } let finalPlatform = appPlatform; if (finalPlatform.toLowerCase() === 'macos') { finalPlatform = 'MacOS'; } try { setAppSaving(true); if (editingApp) { await api.updateAppDownload(editingApp.id, appName.trim(), appDescription.trim(), appDownloadUrl.trim(), finalPlatform); } else { await api.createAppDownload(appName.trim(), appDescription.trim(), appDownloadUrl.trim(), finalPlatform); } setShowAppModal(false); await fetchApps(); } catch (err: any) { alert(err.message || 'Lưu ứng dụng thất bại'); } finally { setAppSaving(false); } }; const handleDeleteApp = async (id: number, name: string) => { if (window.confirm(`Bạn có chắc chắn muốn xóa liên kết tải cho ứng dụng "${name}" không?`)) { try { await api.deleteAppDownload(id); await fetchApps(); } catch (err: any) { alert(err.message || 'Xóa ứng dụng thất bại'); } } }; // Guide Modal Handlers const handleOpenAddGuide = () => { setEditingGuide(null); setGuideTitle(''); setGuideUrl(''); setShowGuideModal(true); }; const handleOpenEditGuide = (guide: AppGuideItem) => { setEditingGuide(guide); setGuideTitle(guide.title); setGuideUrl(guide.url); setShowGuideModal(true); }; const handleSaveGuide = async () => { if (!guideTitle.trim() || !guideUrl.trim()) { alert('Vui lòng điền đầy đủ tiêu đề và đường dẫn hướng dẫn'); return; } try { setGuideSaving(true); if (editingGuide) { await api.updateAppGuide(editingGuide.id, guideTitle.trim(), guideUrl.trim()); } else { await api.createAppGuide(guideTitle.trim(), guideUrl.trim()); } setShowGuideModal(false); await fetchGuides(); } catch (err: any) { alert(err.message || 'Lưu hướng dẫn thất bại'); } finally { setGuideSaving(false); } }; const handleDeleteGuide = async (id: number, title: string) => { if (window.confirm(`Bạn có chắc chắn muốn xóa tài liệu hướng dẫn sinh viên "${title}"?`)) { try { await api.deleteAppGuide(id); await fetchGuides(); } catch (err: any) { alert(err.message || 'Xóa hướng dẫn thất bại'); } } }; return (

Ứng Dụng & Hướng Dẫn

Quản lý các công cụ, link tải ứng dụng và các tài liệu hướng dẫn cho sinh viên.

{isSuperAdmin && (
{activeSubTab === 'apps' ? ( ) : ( )}
)}
{activeSubTab === 'apps' && (
{appsLoading && (

Đang tải danh sách ứng dụng...

)} {!appsLoading && apps.length === 0 && (

Chưa có ứng dụng nào được cấu hình tải.

{isSuperAdmin && ( )}
)} {!appsLoading && apps.length > 0 && (
{isSuperAdmin && } {apps.map((app) => ( {isSuperAdmin && ( )} ))}
Nền tảng Ứng dụng Mô tả Đường dẫn tảiThao tác
{app.platform === 'macOS' ? 'MacOS' : app.platform}
{app.name} {app.description || '—'} e.currentTarget.style.textDecoration = 'underline'} onMouseLeave={(e) => e.currentTarget.style.textDecoration = 'none'} > Tải ứng dụng
)}
)} {activeSubTab === 'guides' && (
{guidesLoading && (

Đang tải danh sách tài liệu hướng dẫn...

)} {!guidesLoading && guides.length === 0 && (

Chưa có tài liệu hướng dẫn sinh viên nào.

{isSuperAdmin && ( )}
)} {!guidesLoading && guides.length > 0 && (
{isSuperAdmin && } {guides.map((guide) => { const isVideo = guide.url.toLowerCase().includes('youtube.com') || guide.url.toLowerCase().includes('youtu.be') || guide.url.toLowerCase().includes('drive.google.com/file'); return ( {isSuperAdmin && ( )} ); })}
Loại tài liệu Tiêu đề hướng dẫn sinh viên Đường dẫn liên kếtThao tác
{isVideo ? 'VIDEO' : 'TÀI LIỆU'} {guide.title} e.currentTarget.style.textDecoration = 'underline'} onMouseLeave={(e) => e.currentTarget.style.textDecoration = 'none'} > Xem liên kết →
)}
)}
{/* App Modal */} {showAppModal && (
setShowAppModal(false)}>
e.stopPropagation()}>

{editingApp ? 'Cập nhật ứng dụng' : 'Thêm ứng dụng tải'}

)} {/* Guide Modal */} {showGuideModal && (
setShowGuideModal(false)}>
e.stopPropagation()}>

{editingGuide ? 'Cập nhật hướng dẫn sinh viên' : 'Thêm tài liệu hướng dẫn sinh viên'}

)}
); };