import React, { useState } from "react";
import { useNavigate, useLocation } from "react-router-dom";

export default function AdminLogin() {
  const [password, setPassword] = useState("");
  const navigate = useNavigate();
  const location = useLocation();
  const from = location.state?.from?.pathname || "/admin"; // redirect after login

  const handleLogin = (e) => {
    e.preventDefault();

    if (password === "Laxmi@#$2026") {
      sessionStorage.setItem("admin", "true"); // session-based login
      navigate(from, { replace: true });
    } else {
      alert("Wrong password");
    }
  };

  return (
    <div className="min-h-screen flex items-center justify-center bg-gray-50">
      <form
        onSubmit={handleLogin}
        className="bg-white p-8 rounded shadow-md w-full max-w-sm"
      >
        <h2 className="text-xl font-bold mb-4">LCEPL ADMIN LOGIN</h2>
        <input
          type="password"
          placeholder="Enter LCEPL ADMIN password"
          value={password}
          onChange={(e) => setPassword(e.target.value)}
          className="w-full border px-3 py-2 rounded mb-4"
          required
        />
        <button
          type="submit"
          className="w-full bg-blue-600 text-white py-2 rounded hover:bg-blue-700"
        >
          Login
        </button>
      </form>
    </div>
  );
}
