"use client" import { useState, useCallback, useEffect } from "react" import { Sidebar } from "@/components/sidebar" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Textarea } from "@/components/ui/textarea" import { Progress } from "@/components/ui/progress" import { Badge } from "@/components/ui/badge" import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs" import { Upload, FileText, Video, LinkIcon, X, CheckCircle, AlertCircle, Play, ExternalLink } from "lucide-react" import { useToast } from "@/hooks/use-toast" import { useDropzone } from "react-dropzone" interface UploadedFile { id: string name: string size: number type: string status: "uploading" | "completed" | "error" progress: number url?: string file?: File // 儲存原始文件對象 } export default function UploadPage() { const [files, setFiles] = useState([]) const [websiteUrl, setWebsiteUrl] = useState("") const [projectTitle, setProjectTitle] = useState("") const [projectDescription, setProjectDescription] = useState("") const [isAnalyzing, setIsAnalyzing] = useState(false) const [analysisProgress, setAnalysisProgress] = useState(0) const { toast } = useToast() // 動態進度條效果 useEffect(() => { let progressInterval: NodeJS.Timeout | null = null if (isAnalyzing) { setAnalysisProgress(0) progressInterval = setInterval(() => { setAnalysisProgress((prev) => { if (prev >= 90) { // 在90%時停止,等待實際完成 return prev } // 模擬不規則的進度增長 const increment = Math.random() * 8 + 2 // 2-10之間的隨機增量 return Math.min(prev + increment, 90) }) }, 500) // 每500ms更新一次 } else { setAnalysisProgress(0) } return () => { if (progressInterval) { clearInterval(progressInterval) } } }, [isAnalyzing]) const onDrop = useCallback((acceptedFiles: File[]) => { const newFiles: UploadedFile[] = acceptedFiles.map((file) => ({ id: Date.now().toString() + Math.random().toString(36).substr(2, 9), name: file.name, size: file.size, type: file.type, status: "completed", // 直接標記為完成,因為我們會在評審時處理文件 progress: 100, file: file, // 儲存原始文件對象 })) setFiles((prev) => [...prev, ...newFiles]) console.log('📁 文件已準備就緒:', newFiles.map(f => f.name).join(', ')) }, []) const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop, accept: { "application/vnd.ms-powerpoint": [".ppt"], "application/vnd.openxmlformats-officedocument.presentationml.presentation": [".pptx"], "video/*": [".mp4", ".avi", ".mov", ".wmv", ".flv", ".webm"], "application/pdf": [".pdf"], }, maxSize: 100 * 1024 * 1024, // 100MB }) const removeFile = (fileId: string) => { setFiles((prev) => prev.filter((file) => file.id !== fileId)) } const formatFileSize = (bytes: number) => { if (bytes === 0) return "0 Bytes" const k = 1024 const sizes = ["Bytes", "KB", "MB", "GB"] const i = Math.floor(Math.log(bytes) / Math.log(k)) return Number.parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i] } const getFileIcon = (type: string) => { if (type.includes("presentation") || type.includes("powerpoint")) { return } if (type.includes("video")) { return