"use client" import { useState, 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 { Slider } from "@/components/ui/slider" import { Badge } from "@/components/ui/badge" import { Plus, Trash2, Save, RotateCcw, FileText, Loader2 } from "lucide-react" import { useToast } from "@/hooks/use-toast" interface CriteriaItem { id: number | string name: string description: string weight: number maxScore: number } interface CriteriaTemplate { id: number name: string description: string total_weight: number items: CriteriaItem[] } export default function CriteriaPage() { const [criteria, setCriteria] = useState([]) const [templateName, setTemplateName] = useState("") const [templateDescription, setTemplateDescription] = useState("") const [isLoading, setIsLoading] = useState(true) const [isSaving, setIsSaving] = useState(false) const [currentTemplate, setCurrentTemplate] = useState(null) const { toast } = useToast() // 載入評分標準 useEffect(() => { loadTemplate() }, []) const loadTemplate = async () => { try { setIsLoading(true) const response = await fetch('/api/criteria-templates') const result = await response.json() if (result.success && result.data && result.data.length > 0) { // 載入第一個模板(單一模板模式) const template = result.data[0] setCurrentTemplate(template) setTemplateName(template.name) setTemplateDescription(template.description || '') setCriteria(template.items || []) } else { // 如果沒有模板,載入預設模板 await loadDefaultTemplate() } } catch (error) { console.error('載入評分標準失敗:', error) // 如果載入失敗,嘗試載入預設模板 await loadDefaultTemplate() } finally { setIsLoading(false) } } const loadDefaultTemplate = async () => { try { const response = await fetch('/api/criteria-templates/default') const result = await response.json() if (result.success && result.data) { const template = result.data setCurrentTemplate(template) setTemplateName(template.name) setTemplateDescription(template.description || '') setCriteria(template.items || []) } else { // 如果連預設模板都沒有,使用空模板 setCurrentTemplate(null) setTemplateName('') setTemplateDescription('') setCriteria([]) } } catch (error) { console.error('載入預設評分標準失敗:', error) // 使用空模板 setCurrentTemplate(null) setTemplateName('') setTemplateDescription('') setCriteria([]) } } const addCriteria = () => { const newCriteria: CriteriaItem = { id: Date.now().toString(), name: "", description: "", weight: 10, maxScore: 10, } setCriteria([...criteria, newCriteria]) } const removeCriteria = (id: string) => { setCriteria(criteria.filter((item) => item.id !== id)) } const updateCriteria = (id: string, field: keyof CriteriaItem, value: string | number) => { setCriteria(criteria.map((item) => (item.id === id ? { ...item, [field]: value } : item))) } const updateWeight = (id: string, weight: number[]) => { updateCriteria(id, "weight", Number(weight[0]) || 0) } const totalWeight = criteria.reduce((sum, item) => sum + (Number(item.weight) || 0), 0) const saveCriteria = async () => { if (totalWeight !== 100) { toast({ title: "權重設定錯誤", description: "所有評分項目的權重總和必須等於 100%", variant: "destructive", }) return } if (criteria.some((item) => !item.name.trim())) { toast({ title: "設定不完整", description: "請填寫所有評分項目的名稱", variant: "destructive", }) return } if (!templateName.trim()) { toast({ title: "設定不完整", description: "請填寫模板名稱", variant: "destructive", }) return } try { setIsSaving(true) const templateData = { name: templateName.trim(), description: templateDescription.trim(), items: criteria.map(item => ({ name: item.name.trim(), description: item.description.trim(), weight: item.weight, maxScore: item.maxScore })) } // 單一模板模式,總是使用 POST 進行覆蓋 const url = '/api/criteria-templates' const method = 'POST' const response = await fetch(url, { method, headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(templateData), }) const result = await response.json() if (result.success) { toast({ title: "儲存成功", description: "評分標準已成功儲存", }) // 重新載入資料 await loadTemplate() } else { throw new Error(result.error || '儲存失敗') } } catch (error) { console.error('儲存評分標準失敗:', error) toast({ title: "儲存失敗", description: error instanceof Error ? error.message : "無法儲存評分標準", variant: "destructive", }) } finally { setIsSaving(false) } } const resetToDefault = async () => { try { await loadDefaultTemplate() toast({ title: "已重置", description: "評分標準已重置為預設值", }) } catch (error) { toast({ title: "重置失敗", description: "無法重置評分標準", variant: "destructive", }) } } if (isLoading) { return (

載入評分標準中...

) } return (
{/* Header */}

評分標準設定

自定義評分項目和權重,建立符合您需求的評審標準

{/* Template Name */} 標準模板資訊
setTemplateName(e.target.value)} placeholder="輸入評分標準名稱" />