'use client' import { useState, useEffect } from 'react' import apiClient from '@/lib/api-client' import AlertDialog from '@/components/ui/AlertDialog' interface Department { id?: number parent_id: number | null code: string name: string name_en: string | null email_domain: string | null email_address: string | null email_quota_mb: number description: string | null is_active: boolean } interface ParentDepartment { id: number name: string depth: number } interface Props { department: Department | null parentDepartments: ParentDepartment[] onClose: () => void onSave: (isEdit: boolean) => void } export default function DepartmentFormModal({ department: editingDepartment, parentDepartments, onClose, onSave }: Props) { const [formData, setFormData] = useState({ parent_id: null, code: '', name: '', name_en: null, email_domain: null, email_address: null, email_quota_mb: 5120, description: null, is_active: true, }) const [loading, setLoading] = useState(false) const [isTopLevel, setIsTopLevel] = useState(true) // 對話框狀態 const [alertDialog, setAlertDialog] = useState<{ isOpen: boolean title: string message: string type: 'info' | 'warning' | 'error' | 'success' }>({ isOpen: false, title: '', message: '', type: 'info', }) // 如果是編輯模式,填入現有資料 useEffect(() => { if (editingDepartment) { setFormData(editingDepartment) setIsTopLevel(editingDepartment.parent_id === null) } }, [editingDepartment]) const handleChange = (field: keyof Department, value: any) => { setFormData(prev => ({ ...prev, [field]: value })) } const handleParentChange = (value: string) => { const parentId = value === '' ? null : parseInt(value) setFormData(prev => ({ ...prev, parent_id: parentId })) setIsTopLevel(parentId === null) } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setLoading(true) try { // 驗證 if (!formData.code || !formData.name) { setAlertDialog({ isOpen: true, title: '欄位驗證', message: '請填寫部門代碼和名稱', type: 'warning', }) setLoading(false) return } // 第一層部門必須填寫郵件網域 if (isTopLevel && !formData.email_domain) { setAlertDialog({ isOpen: true, title: '欄位驗證', message: '第一層部門必須填寫郵件網域', type: 'warning', }) setLoading(false) return } // 準備資料 const submitData: any = { parent_id: formData.parent_id, code: formData.code, name: formData.name, name_en: formData.name_en, email_address: formData.email_address, email_quota_mb: formData.email_quota_mb, description: formData.description, is_active: formData.is_active, } // 只有第一層部門才送 email_domain if (isTopLevel) { submitData.email_domain = formData.email_domain } const isEdit = !!editingDepartment if (isEdit) { // 更新 await apiClient.put(`/departments/${editingDepartment.id}`, submitData) } else { // 新增 await apiClient.post('/departments', submitData) } onSave(isEdit) onClose() } catch (error: any) { console.error('Failed to save department:', error) setAlertDialog({ isOpen: true, title: '儲存失敗', message: error.response?.data?.detail || '儲存失敗,請稍後再試', type: 'error', }) } finally { setLoading(false) } } return (
e.stopPropagation()} > {/* Card Header */}

{formData.name || '部門資料'} - {editingDepartment ? '編輯作業' : '新增作業'}

{/* Card Body - Scrollable */}
{/* Row 1: 上層部門 + 部門代碼 */}
handleChange('code', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm text-gray-900 placeholder:text-gray-500" placeholder="例如: BD" required />
{/* Row 2: 部門名稱 + 英文名稱 */}
handleChange('name', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm text-gray-900 placeholder:text-gray-500" placeholder="例如: 業務發展部" required />
handleChange('name_en', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm text-gray-900 placeholder:text-gray-500" placeholder="例如: Business Development" />
{/* Row 3: 郵件網域 (只在第一層顯示) */} {isTopLevel && (
handleChange('email_domain', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm text-gray-900 placeholder:text-gray-500" placeholder="例如: ease.taipei" required />
)} {/* Row 4: 部門信箱 + 信箱配額 */}
handleChange('email_address', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm text-gray-900 placeholder:text-gray-500" placeholder="例如: bd@ease.taipei" />
handleChange('email_quota_mb', parseInt(e.target.value))} className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm text-gray-900 placeholder:text-gray-500" min={0} />
{/* 說明 */}