'use client';

import { useActionState } from 'react';
import { login } from './actions';

export default function LoginPage() {
  const [state, loginAction, isPending] = useActionState(login, undefined);

  return (
    <div className="login-wrapper">
      <main className="login-outer">
        <div className="login-header">
          <h1 className="login-page-title">Tabletten-Übersicht</h1>
        </div>

        <div className="login-center">
          <div className="login-card">
            <h2 className="login-card-title">Anmeldung</h2>

            <form action={loginAction} className="login-form">
              <div>
                <label htmlFor="username" className="login-label">
                  Benutzername
                </label>
                <input
                  id="username"
                  name="username"
                  type="text"
                  required
                  autoComplete="off"
                  className="login-input"
                  placeholder="Benutzername"
                  disabled={isPending}
                />
              </div>

              <div>
                <label htmlFor="password" className="login-label">
                  Passwort
                </label>
                <input
                  id="password"
                  name="password"
                  type="password"
                  required
                  autoComplete="new-password"
                  className="login-input"
                  placeholder="Passwort"
                  disabled={isPending}
                />
              </div>

              {state?.error && (
                <div className="login-error">
                  {state.error}
                </div>
              )}

              <button
                type="submit"
                disabled={isPending}
                className="login-submit"
              >
                {isPending ? 'Anmeldung läuft...' : 'Anmelden'}
              </button>
            </form>
          </div>
        </div>
      </main>
    </div>
  );
}
