"use client" import { useState } from "react" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Alert, AlertDescription } from "@/components/ui/alert" import { Badge } from "@/components/ui/badge" import { Progress } from "@/components/ui/progress" import { Avatar, AvatarFallback } from "@/components/ui/avatar" import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog" import { Textarea } from "@/components/ui/textarea" import { AlertTriangle, CheckCircle, User, Trophy, LogIn, Loader2, Eye, EyeOff, Lock } from "lucide-react" interface Judge { id: string name: string specialty: string } interface ScoringItem { id: string name: string type: "individual" | "team" status: "pending" | "completed" score?: number submittedAt?: string } export default function JudgeScoringPage() { const [isLoggedIn, setIsLoggedIn] = useState(false) const [judgeId, setJudgeId] = useState("") const [accessCode, setAccessCode] = useState("") const [currentJudge, setCurrentJudge] = useState(null) const [scoringItems, setScoringItems] = useState([]) const [selectedItem, setSelectedItem] = useState(null) const [showScoringDialog, setShowScoringDialog] = useState(false) const [scores, setScores] = useState>({}) const [comments, setComments] = useState("") const [isSubmitting, setIsSubmitting] = useState(false) const [error, setError] = useState("") const [success, setSuccess] = useState("") const [showAccessCode, setShowAccessCode] = useState(false) const [isLoading, setIsLoading] = useState(false) const [competitionRules, setCompetitionRules] = useState([]) const handleLogin = async () => { setError("") setIsLoading(true) if (!judgeId.trim() || !accessCode.trim()) { setError("請填寫評審ID和存取碼") setIsLoading(false) return } if (accessCode !== "judge2024") { setError("存取碼錯誤") setIsLoading(false) return } try { // 獲取評審的評分任務 const response = await fetch(`/api/judge/scoring-tasks?judgeId=${judgeId}&competitionId=0fffae9a-9539-11f0-b5d9-6e36c63cdb98`) const data = await response.json() if (data.success) { setCurrentJudge(data.data.judge) setScoringItems(data.data.tasks) setIsLoggedIn(true) setSuccess("登入成功!") setTimeout(() => setSuccess(""), 3000) // 載入競賽規則 await loadCompetitionRules() } else { setError(data.message || "登入失敗") } } catch (err) { console.error('登入失敗:', err) setError("登入失敗,請重試") } finally { setIsLoading(false) } } const loadCompetitionRules = async () => { try { // 使用正確的競賽ID const response = await fetch('/api/competitions/0fffae9a-9539-11f0-b5d9-6e36c63cdb98/rules') const data = await response.json() if (data.success) { setCompetitionRules(data.data) } } catch (err) { console.error('載入競賽規則失敗:', err) } } const handleStartScoring = async (item: ScoringItem) => { setSelectedItem(item) // 如果是重新評分,嘗試載入現有的評分數據 if (item.status === "completed") { try { // 這裡可以添加載入現有評分數據的邏輯 // 暫時使用默認值 const initialScores: Record = {} if (competitionRules && competitionRules.length > 0) { competitionRules.forEach((rule: any) => { initialScores[rule.name] = 0 }) } else { initialScores.innovation = 0 initialScores.technical = 0 initialScores.usability = 0 initialScores.presentation = 0 initialScores.impact = 0 } setScores(initialScores) setComments("") } catch (err) { console.error('載入現有評分數據失敗:', err) } } else { // 新評分,初始化為0 const initialScores: Record = {} if (competitionRules && competitionRules.length > 0) { competitionRules.forEach((rule: any) => { initialScores[rule.name] = 0 }) } else { initialScores.innovation = 0 initialScores.technical = 0 initialScores.usability = 0 initialScores.presentation = 0 initialScores.impact = 0 } setScores(initialScores) setComments("") } setShowScoringDialog(true) } const handleSubmitScore = async () => { if (!selectedItem || !currentJudge) return setIsSubmitting(true) try { // 計算總分 (1-10分制,轉換為100分制) const totalScore = (Object.values(scores).reduce((a, b) => a + b, 0) / Object.values(scores).length) * 10 // 提交評分到 API const response = await fetch('/api/admin/scoring', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ judgeId: currentJudge.id, participantId: selectedItem.id, participantType: 'app', scores: scores, comments: comments.trim(), competitionId: '0fffae9a-9539-11f0-b5d9-6e36c63cdb98', // 正確的競賽ID isEdit: selectedItem.status === "completed", // 如果是重新評分,標記為編輯模式 recordId: selectedItem.status === "completed" ? selectedItem.id : null }) }) const data = await response.json() if (data.success) { // 更新本地狀態 setScoringItems(prev => prev.map(item => item.id === selectedItem.id ? { ...item, status: "completed", score: totalScore, submittedAt: new Date().toISOString() } : item )) setShowScoringDialog(false) setSelectedItem(null) setScores({}) setComments("") setSuccess("評分提交成功!") setTimeout(() => setSuccess(""), 3000) } else { setError(data.message || "評分提交失敗") } } catch (err) { console.error('評分提交失敗:', err) setError("評分提交失敗,請重試") } finally { setIsSubmitting(false) } } const getProgress = () => { const total = scoringItems.length const completed = scoringItems.filter(item => item.status === "completed").length return { total, completed, percentage: total > 0 ? Math.round((completed / total) * 100) : 0 } } const isFormValid = () => { // 檢查所有評分項目是否都已評分 const rules = competitionRules && competitionRules.length > 0 ? competitionRules : [ { name: "創新性" }, { name: "技術性" }, { name: "實用性" }, { name: "展示效果" }, { name: "影響力" } ] const allScoresFilled = rules.every((rule: any) => scores[rule.name] && scores[rule.name] > 0 ) // 檢查評審意見是否填寫 const commentsFilled = comments.trim().length > 0 return allScoresFilled && commentsFilled } const progress = getProgress() if (!isLoggedIn) { return (
評審評分系統 請輸入您的評審ID和存取碼進行登入
{error && ( {error} )}
setJudgeId(e.target.value)} />
setAccessCode(e.target.value)} className="pl-10 pr-10" />
) } return (
{/* 成功訊息 */} {success && ( {success} )} {/* 評審資訊 */}
{currentJudge?.name.charAt(0)}

{currentJudge?.name}

{currentJudge?.specialty}

{/* 評分進度 */} 評分進度 您的評分任務進度概覽
完成進度 {progress.completed} / {progress.total}
{progress.percentage}%
{/* 評分項目列表 */} 評分項目 請為以下項目進行評分
{scoringItems.map((item) => (
{item.type === "individual" ? ( ) : (
)} {item.display_name || item.name} {item.type === "individual" ? "個人" : "團隊"}
{item.status === "completed" ? (
{item.score}
/ 100
{item.submittedAt ? new Date(item.submittedAt).toLocaleDateString('zh-TW') : ''}
) : ( )}
))}
{/* 評分對話框 */} 評分項目:{selectedItem?.name} 請為此項目進行評分,滿分為10分
{/* 評分項目 */}

評分項目

{(competitionRules && competitionRules.length > 0 ? competitionRules : [ { name: "創新性", description: "創新程度和獨特性" }, { name: "技術性", description: "技術實現的複雜度和品質" }, { name: "實用性", description: "實際應用價值和用戶體驗" }, { name: "展示效果", description: "展示的清晰度和吸引力" }, { name: "影響力", description: "對行業或社會的潛在影響" } ]).map((criterion, index) => (

{criterion.description}

{Array.from({ length: 10 }, (_, i) => i + 1).map((score) => ( ))}
{!scores[criterion.name] && (

請為此項目打分

)}
))}
{/* 評審意見 */}