import { useState } from "react"
import api from "../../services/api"
import { useNavigate } from "react-router-dom"

export default function AdminLogin() {
  const [email, setEmail] = useState("")
  const [password, setPassword] = useState("")
  const navigate = useNavigate()

  const login = () => {
    api.post("http://localhost:5000/api/auth/admin/login", { email, password })
      .then(res => {
        localStorage.setItem("adminToken", res.data.token)
        navigate("/admin")
      })
      .catch(() => alert("Invalid credentials"))
  }

  return (
    <div style={{ padding: 20 }}>
      <h3>Admin Login</h3>

      <input
        placeholder="Email"
        value={email}
        onChange={e => setEmail(e.target.value)}
      />

      <br /><br />

      <input
        type="password"
        placeholder="Password"
        value={password}
        onChange={e => setPassword(e.target.value)}
      />

      <br /><br />

      <button onClick={login}>Login</button>
    </div>
  )
}
