import { useState } from "react"; import { Navigate, useNavigate } from "react-router-dom"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { AlertCircle, LogIn, ShieldCheck } from "lucide-react"; import { loginApi } from "@/api/auth"; import { ApiError } from "@/api/client"; import { useAuthStore } from "@/hooks/useAuth"; import { Button } from "@/components/ui/button"; const loginSchema = z.object({ email: z.string().email("Enter a valid email address"), password: z.string().min(1, "Password is required"), }); type LoginFormValues = z.infer; const DEMO_ACCOUNTS = [ { label: "System Admin", email: "admin@xyz.demo", hint: "Full system control" }, { label: "Biomedical", email: "bio@xyz.demo", hint: "Assets, PM, repairs" }, { label: "Department User (ICU)", email: "dept@xyz.demo", hint: "ICU complaints & assets" }, { label: "Management", email: "mgmt@xyz.demo", hint: "Reports & oversight" }, { label: "Coordinator", email: "coord@xyz.demo", hint: "Contracts, demo, calibration" }, ]; export function LoginPage() { const navigate = useNavigate(); const { user, token, setAuth } = useAuthStore(); const [serverError, setServerError] = useState(null); const [quickLoadingEmail, setQuickLoadingEmail] = useState(null); const { register, handleSubmit, setValue, formState: { errors, isSubmitting }, } = useForm({ resolver: zodResolver(loginSchema), defaultValues: { email: "", password: "" }, }); if (token && user) { return ; } async function doLogin(email: string, password: string) { setServerError(null); try { const res = await loginApi(email, password); setAuth(res.user, res.access_token); navigate("/", { replace: true }); } catch (err) { if (err instanceof ApiError) { setServerError(err.detail); } else { setServerError("Unable to reach server. Please ensure the backend is running."); } } } async function onSubmit(values: LoginFormValues) { await doLogin(values.email, values.password); } async function handleQuickLogin(email: string) { setValue("email", email); setValue("password", "Demo@123"); setQuickLoadingEmail(email); try { await doLogin(email, "Demo@123"); } finally { setQuickLoadingEmail(null); } } return (
{/* Hospital Branding */}
XYZ Hospital { (e.target as HTMLImageElement).src = "/logo.jpg"; }} className="h-20 mx-auto object-contain mb-3" />

XYZ Hospital

MediAsset AI

Asset Management & Maintenance System

{serverError && (
{serverError}
)}
{errors.email && (

{errors.email.message}

)}
{errors.password && (

{errors.password.message}

)}
{/* Quick Demo Login Panel */}
Quick Demo Login (Password: Demo@123)
{DEMO_ACCOUNTS.map((acct) => ( ))}
{/* Brand & Clearance Disclaimer */}
The name, tagline, logo concept, and palette are proposed brand assets and remain subject to clearance.
); }