/** * Phase 2: 資料庫連接設定 * * 功能: * 1. 填寫資料庫連接資訊 * 2. 測試資料庫連接 * 3. 將設定寫入 .env 檔案 */ 'use client' import { useState, useEffect } from 'react' import { useRouter } from 'next/navigation' interface DatabaseConfig { host: string port: number database: string user: string password: string } interface TestResult { success: boolean message: string db_version?: string connection_info?: string } export default function Phase2Database() { const router = useRouter() const [config, setConfig] = useState({ host: '10.1.0.20', port: 5433, database: 'hr_portal', user: 'admin', password: '', }) const [testing, setTesting] = useState(false) const [testResult, setTestResult] = useState(null) const [setupPending, setSetupPending] = useState(false) const [loading, setLoading] = useState(true) const [alreadyConfigured, setAlreadyConfigured] = useState(false) // 載入已儲存的配置 useEffect(() => { loadSavedConfig() }, []) const loadSavedConfig = async () => { try { setLoading(true) const response = await fetch('http://10.1.0.245:10181/api/v1/installation/get-config/database') if (!response.ok) { throw new Error(`HTTP ${response.status}`) } const data = await response.json() if (data.configured && data.config) { setConfig({ host: data.config.host || '10.1.0.20', port: parseInt(data.config.port) || 5433, database: data.config.database || 'hr_portal', user: data.config.user || 'admin', password: data.config.password === '****' ? '' : (data.config.password || ''), }) setAlreadyConfigured(true) } } catch (error) { console.error('[Database] Failed to load saved config:', error) } finally { setLoading(false) } } const handleTest = async () => { try { setTesting(true) setTestResult(null) const response = await fetch('http://10.1.0.245:10181/api/v1/installation/test-database', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(config), }) const data = await response.json() if (!response.ok) { setTestResult({ success: false, message: data.detail || `HTTP ${response.status}`, }) return } setTestResult(data) if (data.success) { setSetupPending(true) } } catch (error) { setTestResult({ success: false, message: error instanceof Error ? error.message : '連接失敗', }) } finally { setTesting(false) } } const handleSetup = async () => { try { const response = await fetch('http://10.1.0.245:10181/api/v1/installation/setup-database', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(config), }) const data = await response.json() if (!response.ok) { alert(`設定失敗: ${data.detail}`) return } alert('資料庫設定成功!') router.push('/installation/phase3-keycloak') } catch (error) { alert(`設定失敗: ${error instanceof Error ? error.message : '未知錯誤'}`) } } if (loading) { return (

載入已儲存的配置...

) } return (
{/* Progress Bar */}
進度 2 / 7
{/* Main Card */}
🗄️

Phase 2: 資料庫連接設定

設定 PostgreSQL 主資料庫

{alreadyConfigured && (

已載入先前儲存的配置 - 您可以查看或修改下方設定

)}

說明:PostgreSQL 是 HR Portal 的主要資料庫,用於儲存員工、部門、權限等核心資料。

{/* Form */}
setConfig({ ...config, host: e.target.value })} className="w-full px-4 py-2 bg-gray-700 border border-gray-600 rounded-lg text-gray-100 placeholder-gray-400 focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="例如: 10.1.0.20" />
setConfig({ ...config, port: parseInt(e.target.value) || 5432 })} className="w-full px-4 py-2 bg-gray-700 border border-gray-600 rounded-lg text-gray-100 placeholder-gray-400 focus:ring-2 focus:ring-blue-500 focus:border-transparent" />
setConfig({ ...config, database: e.target.value })} className="w-full px-4 py-2 bg-gray-700 border border-gray-600 rounded-lg text-gray-100 placeholder-gray-400 focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="hr_portal" />
setConfig({ ...config, user: e.target.value })} className="w-full px-4 py-2 bg-gray-700 border border-gray-600 rounded-lg text-gray-100 placeholder-gray-400 focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="admin" />
setConfig({ ...config, password: e.target.value })} className="w-full px-4 py-2 bg-gray-700 border border-gray-600 rounded-lg text-gray-100 placeholder-gray-400 focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="資料庫密碼" />
{/* Test Result */} {testResult && (
{testResult.success ? '✅' : '❌'}

{testResult.success ? '連接成功' : '連接失敗'}

{testResult.message}

{testResult.success && testResult.db_version && (

資料庫版本: {testResult.db_version}

{testResult.connection_info &&

連接資訊: {testResult.connection_info}

}
)}
)} {/* Action Buttons */}
) }