// AdminLogin.jsx — formulario de acceso al panel
function AdminLogin({ onLogin }) {
  const [email, setEmail] = React.useState('');
  const [password, setPassword] = React.useState('');
  const [busy, setBusy] = React.useState(false);
  const [err, setErr] = React.useState('');

  const submit = async (e) => {
    e.preventDefault();
    setBusy(true); setErr('');
    try {
      await window.supabaseApi.signIn(email.trim(), password);
      onLogin && onLogin();
    } catch (e) {
      setErr(e.message || 'No se pudo iniciar sesión');
    } finally {
      setBusy(false);
    }
  };

  return (
    <div className="im-admin-login">
      <form onSubmit={submit} className="im-admin-login-card">
        <h1 className="im-admin-login-title">Panel IsaMuñoz</h1>
        <p className="im-admin-login-sub">Accede para gestionar el blog</p>
        <label className="im-field">
          <span>Email</span>
          <input type="email" value={email} onChange={(e) => setEmail(e.target.value)} required autoFocus />
        </label>
        <label className="im-field">
          <span>Contraseña</span>
          <input type="password" value={password} onChange={(e) => setPassword(e.target.value)} required />
        </label>
        {err && <div className="im-admin-error">{err}</div>}
        <button type="submit" className="im-btn im-btn-primary" disabled={busy}>
          {busy ? 'Entrando…' : 'Entrar'}
        </button>
      </form>
    </div>
  );
}

window.AdminLogin = AdminLogin;
