"use client" import { useState, useEffect } from "react" import { TestLayout } from "@/components/test-layout" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group" import { Label } from "@/components/ui/label" import { Progress } from "@/components/ui/progress" import { useRouter } from "next/navigation" import { calculateCombinedScore } from "@/lib/utils/score-calculator" import { useAuth } from "@/lib/hooks/use-auth" interface LogicQuestion { id: number question: string option_a: string option_b: string option_c: string option_d: string option_e: string correct_answer: 'A' | 'B' | 'C' | 'D' | 'E' explanation?: string created_at: string } interface CreativeQuestion { id: number statement: string category: 'innovation' | 'imagination' | 'flexibility' | 'originality' is_reverse: boolean created_at: string } type TestPhase = "logic" | "creative" | "completed" export default function CombinedTestPage() { const router = useRouter() const { user } = useAuth() const [phase, setPhase] = useState("logic") const [currentQuestion, setCurrentQuestion] = useState(0) const [logicAnswers, setLogicAnswers] = useState>({}) const [creativeAnswers, setCreativeAnswers] = useState>({}) const [timeRemaining, setTimeRemaining] = useState(45 * 60) // 45 minutes total const [logicQuestions, setLogicQuestions] = useState([]) const [creativeQuestions, setCreativeQuestions] = useState([]) const [isLoading, setIsLoading] = useState(true) const [isSubmitting, setIsSubmitting] = useState(false) // Load questions from database useEffect(() => { const loadQuestions = async () => { try { // Load logic questions const logicResponse = await fetch('/api/logic-questions') const logicData = await logicResponse.json() // Load creative questions const creativeResponse = await fetch('/api/creative-questions') const creativeData = await creativeResponse.json() if (logicData.success && creativeData.success) { setLogicQuestions(logicData.questions) setCreativeQuestions(creativeData.questions) } else { console.error('Failed to load questions:', logicData.error || creativeData.error) } } catch (error) { console.error('Error loading questions:', error) } finally { setIsLoading(false) } } loadQuestions() }, []) // Timer effect useEffect(() => { if (logicQuestions.length === 0 && creativeQuestions.length === 0) return const timer = setInterval(() => { setTimeRemaining((prev) => { if (prev <= 1) { handleSubmit() return 0 } return prev - 1 }) }, 1000) return () => clearInterval(timer) }, [logicQuestions, creativeQuestions]) const formatTime = (seconds: number) => { const mins = Math.floor(seconds / 60) const secs = seconds % 60 return `${mins.toString().padStart(2, "0")}:${secs.toString().padStart(2, "0")}` } const getCurrentQuestions = () => { return phase === "logic" ? logicQuestions : creativeQuestions } const getTotalQuestions = () => { return logicQuestions.length + creativeQuestions.length } const getOverallProgress = () => { const logicCompleted = phase === "logic" ? currentQuestion : logicQuestions.length const creativeCompleted = phase === "creative" ? currentQuestion : 0 return logicCompleted + creativeCompleted } const handleAnswerChange = (value: string) => { if (phase === "logic") { setLogicAnswers((prev) => ({ ...prev, [currentQuestion]: value, })) } else { setCreativeAnswers((prev) => ({ ...prev, [currentQuestion]: Number.parseInt(value), })) } } const handleNext = () => { const currentQuestions = getCurrentQuestions() if (currentQuestion < currentQuestions.length - 1) { setCurrentQuestion((prev) => prev + 1) } else if (phase === "logic") { // Switch to creative phase setPhase("creative") setCurrentQuestion(0) } else { // Complete the test handleSubmit() } } const handlePrevious = () => { if (currentQuestion > 0) { setCurrentQuestion((prev) => prev - 1) } else if (phase === "creative") { // Go back to logic phase setPhase("logic") setCurrentQuestion(logicQuestions.length - 1) } } const handleSubmit = async () => { console.log('🔍 開始提交綜合測試...') console.log('用戶狀態:', user) if (!user) { console.log('❌ 用戶未登入') alert('請先登入') return } console.log('✅ 用戶已登入,用戶ID:', user.id) setIsSubmitting(true) try { // Calculate logic score let logicCorrect = 0 logicQuestions.forEach((question, index) => { if (logicAnswers[index] === question.correct_answer) { logicCorrect++ } }) const logicScore = Math.round((logicCorrect / logicQuestions.length) * 100) // Calculate creativity score let creativityTotal = 0 const processedCreativeAnswers: Record = {} creativeQuestions.forEach((question, index) => { const answer = creativeAnswers[index] || 1 const processedScore = question.is_reverse ? 6 - answer : answer creativityTotal += processedScore processedCreativeAnswers[index] = processedScore }) const creativityMaxScore = creativeQuestions.length * 5 const creativityScore = Math.round((creativityTotal / creativityMaxScore) * 100) // Calculate combined score const combinedResult = calculateCombinedScore(logicScore, creativityScore) // Store results in localStorage (for backward compatibility) const results = { type: "combined", logicScore, creativityScore, overallScore: combinedResult.overallScore, level: combinedResult.level, description: combinedResult.description, breakdown: combinedResult.breakdown, logicAnswers, creativeAnswers: processedCreativeAnswers, logicCorrect, creativityTotal, creativityMaxScore, completedAt: new Date().toISOString(), } localStorage.setItem("combinedTestResults", JSON.stringify(results)) console.log('✅ 結果已儲存到 localStorage') // Upload to database console.log('🔄 開始上傳到資料庫...') const uploadData = { userId: user.id, logicScore, creativityScore, overallScore: combinedResult.overallScore, level: combinedResult.level, description: combinedResult.description, logicBreakdown: { correct: logicCorrect, total: logicQuestions.length, answers: logicAnswers }, creativityBreakdown: { total: creativityTotal, maxScore: creativityMaxScore, answers: processedCreativeAnswers }, balanceScore: combinedResult.breakdown.balance, completedAt: new Date().toISOString() } console.log('上傳數據:', uploadData) const uploadResponse = await fetch('/api/test-results/combined', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(uploadData) }) console.log('📡 API 響應狀態:', uploadResponse.status) const uploadResult = await uploadResponse.json() console.log('📡 API 響應內容:', uploadResult) if (uploadResult.success) { console.log('✅ 綜合測試結果已上傳到資料庫') console.log('測試結果ID:', uploadResult.data.testResult.id) } else { console.error('❌ 上傳到資料庫失敗:', uploadResult.error) // 即使上傳失敗,也繼續顯示結果 } router.push("/results/combined") } catch (error) { console.error('❌ 提交測驗失敗:', error) alert('提交測驗失敗,請重試') } finally { setIsSubmitting(false) } } const currentQuestions = getCurrentQuestions() const currentQ = currentQuestions[currentQuestion] const isLastQuestion = phase === "creative" && currentQuestion === creativeQuestions.length - 1 const hasAnswer = phase === "logic" ? logicAnswers[currentQuestion] !== undefined : creativeAnswers[currentQuestion] !== undefined const getPhaseTitle = () => { if (phase === "logic") return "第一部分:邏輯思維測試" return "第二部分:創意能力測試" } const getQuestionNumber = () => { if (phase === "logic") return currentQuestion + 1 return logicQuestions.length + currentQuestion + 1 } if (isLoading) { return ( router.push("/")} >

載入題目中...

) } if (logicQuestions.length === 0 || creativeQuestions.length === 0) { return ( router.push("/")} >

無法載入題目,請稍後再試

) } return ( router.push("/")} >
{/* Phase Indicator */}

{getPhaseTitle()}

{phase === "logic" ? `${currentQuestion + 1}/${logicQuestions.length}` : `${currentQuestion + 1}/${creativeQuestions.length}`}
{phase === "logic" ? (currentQ as LogicQuestion).question : (currentQ as CreativeQuestion).statement} {phase === "logic" && (

請仔細閱讀題目,選擇最正確的答案,每題均為單選。

)} {phase === "creative" && (

請根據您的實際情況,選擇最符合的選項(5=非常符合,1=完全不符合)。

)}
{phase === "logic" ? // Logic question options [ { value: 'A', text: (currentQ as LogicQuestion).option_a }, { value: 'B', text: (currentQ as LogicQuestion).option_b }, { value: 'C', text: (currentQ as LogicQuestion).option_c }, { value: 'D', text: (currentQ as LogicQuestion).option_d }, { value: 'E', text: (currentQ as LogicQuestion).option_e } ].map((option, index) => (
)) : // Creative question options [ { value: "5", label: "我最符合", color: "text-green-600", bgColor: "bg-green-50" }, { value: "4", label: "比較符合", color: "text-green-500", bgColor: "bg-green-50" }, { value: "3", label: "一般", color: "text-yellow-500", bgColor: "bg-yellow-50" }, { value: "2", label: "不太符合", color: "text-orange-500", bgColor: "bg-orange-50" }, { value: "1", label: "與我不符", color: "text-red-500", bgColor: "bg-red-50" }, ].map((option) => (
))}
{/* Navigation */}
{/* Logic questions indicators */} {logicQuestions.map((_, index) => ( ))} {/* Separator */}
{/* Creative questions indicators */} {creativeQuestions.map((_, index) => ( ))}
{isLastQuestion ? ( ) : ( )}
{/* Progress Summary */}
總進度:已完成 {getOverallProgress()} / {getTotalQuestions()} 題
當前階段:{phase === "logic" ? "邏輯思維測試" : "創意能力測試"} ( {Object.keys(phase === "logic" ? logicAnswers : creativeAnswers).length} / {currentQuestions.length} 題)
) }