"use client"
import { useState, useEffect } from "react"
import { useRouter } from "next/navigation"
import { ProtectedRoute } from "@/components/protected-route"
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 { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
import { Alert, AlertDescription } from "@/components/ui/alert"
import { Separator } from "@/components/ui/separator"
import { User, Lock, ArrowLeft, Save } from "lucide-react"
import Link from "next/link"
import { useAuth } from "@/lib/hooks/use-auth"
export default function SettingsPage() {
return (
)
}
function SettingsContent() {
const { user, logout } = useAuth()
const router = useRouter()
const [isLoading, setIsLoading] = useState(false)
const [message, setMessage] = useState("")
const [error, setError] = useState("")
// Profile form state
const [profileData, setProfileData] = useState({
name: "",
email: "",
department: "",
})
// Password form state
const [passwordData, setPasswordData] = useState({
currentPassword: "",
newPassword: "",
confirmPassword: "",
})
const departments = ["人力資源部", "資訊技術部", "財務部", "行銷部", "業務部", "研發部", "客服部", "其他"]
useEffect(() => {
if (user) {
setProfileData({
name: user.name,
email: user.email,
department: user.department,
})
}
}, [user])
const handleProfileUpdate = async () => {
setError("")
setMessage("")
setIsLoading(true)
try {
// Get all users
const users = JSON.parse(localStorage.getItem("hr_users") || "[]")
// Check if email is already taken by another user
const emailExists = users.some((u: any) => u.email === profileData.email && u.id !== user?.id)
if (emailExists) {
setError("該電子郵件已被其他用戶使用")
return
}
// Update user data
const updatedUsers = users.map((u: any) =>
u.id === user?.id
? { ...u, name: profileData.name, email: profileData.email, department: profileData.department }
: u,
)
localStorage.setItem("hr_users", JSON.stringify(updatedUsers))
// Update current user session
const updatedUser = {
...user!,
name: profileData.name,
email: profileData.email,
department: profileData.department,
}
localStorage.setItem("hr_current_user", JSON.stringify(updatedUser))
setMessage("個人資料已成功更新")
// Refresh page to update user context
setTimeout(() => {
window.location.reload()
}, 1500)
} catch (err) {
setError("更新失敗,請稍後再試")
} finally {
setIsLoading(false)
}
}
const handlePasswordChange = async () => {
setError("")
setMessage("")
if (passwordData.newPassword !== passwordData.confirmPassword) {
setError("新密碼確認不一致")
return
}
if (passwordData.newPassword.length < 6) {
setError("新密碼長度至少需要6個字元")
return
}
setIsLoading(true)
try {
// Get all users
const users = JSON.parse(localStorage.getItem("hr_users") || "[]")
// Find current user and verify current password
const currentUser = users.find((u: any) => u.id === user?.id)
if (!currentUser || currentUser.password !== passwordData.currentPassword) {
setError("目前密碼不正確")
return
}
// Update password
const updatedUsers = users.map((u: any) => (u.id === user?.id ? { ...u, password: passwordData.newPassword } : u))
localStorage.setItem("hr_users", JSON.stringify(updatedUsers))
setPasswordData({
currentPassword: "",
newPassword: "",
confirmPassword: "",
})
setMessage("密碼已成功更新")
} catch (err) {
setError("密碼更新失敗,請稍後再試")
} finally {
setIsLoading(false)
}
}
if (!user) return null
return (
{/* Header */}
{/* Profile Settings */}
個人資料
更新您的個人資訊
setProfileData({ ...profileData, name: e.target.value })}
placeholder="請輸入您的姓名"
/>
setProfileData({ ...profileData, email: e.target.value })}
placeholder="請輸入電子郵件"
/>
{user.role === "admin" ? "管理員" : "一般用戶"}
角色由管理員設定,無法自行修改
{/* Password Settings */}
密碼設定
更改您的登入密碼
setPasswordData({ ...passwordData, currentPassword: e.target.value })}
placeholder="請輸入目前密碼"
/>
setPasswordData({ ...passwordData, newPassword: e.target.value })}
placeholder="請輸入新密碼(至少6個字元)"
/>
setPasswordData({ ...passwordData, confirmPassword: e.target.value })}
placeholder="請再次輸入新密碼"
/>
{/* Account Info */}
帳戶資訊
您的帳戶詳細資訊
用戶ID:
{user.id}
建立時間:
{new Date(user.createdAt).toLocaleDateString()}
{/* Messages */}
{message && (
{message}
)}
{error && (
{error}
)}
)
}