import { Head, useForm, usePage } from "@inertiajs/react";
import { useState } from "react";
import type { Shared } from "../types";
import { Button, Errors, Field, Icon } from "../components/ui";
export default function Login({
    demo_accounts = [],
    demo_password = null,
}: {
    demo_accounts?: { label: string; email: string }[];
    demo_password?: string | null;
}) {
    const { school } = usePage<Shared>().props;
    const form = useForm({ email: "", password: "", remember: false });
    const [demoRole, setDemoRole] = useState("");
    return (
        <main className="auth-page">
            <Head title="Sign in" />
            <section className="auth-card">
                <div className="brand">
                    <Icon name="book" />
                    {school}
                </div>
                <div>
                    <h1>Welcome back</h1>
                    <p>Sign in to your school workspace.</p>
                </div>
                <Errors />
                <form
                    className="form-stack"
                    onSubmit={(e) => {
                        e.preventDefault();
                        // Autofill may change native values without updating React state.
                        const fields = new FormData(e.currentTarget);
                        const credentials = {
                            email: String(fields.get("email") ?? ""),
                            password: String(fields.get("password") ?? ""),
                            remember: fields.has("remember"),
                        };
                        form.setData(credentials);
                        form.transform(() => credentials);
                        form.post("/login", {
                            onFinish: () => form.reset("password"),
                        });
                    }}
                >
                    {!!demo_accounts.length && (
                        <Field
                            label="Demo role"
                            hint="Choose a role to fill the demo email and password."
                        >
                            <select
                                value={demoRole}
                                onChange={(event) => {
                                    setDemoRole(event.target.value);
                                    form.setData({
                                        email: event.target.value,
                                        password: event.target.value
                                            ? demo_password || ""
                                            : "",
                                        remember: false,
                                    });
                                    form.clearErrors();
                                }}
                            >
                                <option value="">Choose a role to try</option>
                                {demo_accounts.map((account) => (
                                    <option
                                        key={account.email}
                                        value={account.email}
                                    >
                                        {account.label}
                                    </option>
                                ))}
                            </select>
                        </Field>
                    )}
                    <Field label="Email address" name="email">
                        <input
                            name="email"
                            type="email"
                            autoComplete="username"
                            required
                            autoFocus
                            value={form.data.email}
                            onChange={(e) =>
                                form.setData("email", e.target.value)
                            }
                        />
                    </Field>
                    <Field label="Password" name="password">
                        <input
                            name="password"
                            type="password"
                            autoComplete="current-password"
                            required
                            value={form.data.password}
                            onChange={(e) =>
                                form.setData("password", e.target.value)
                            }
                        />
                    </Field>
                    <label className="checkbox">
                        <input
                            name="remember"
                            type="checkbox"
                            checked={form.data.remember}
                            onChange={(e) =>
                                form.setData("remember", e.target.checked)
                            }
                        />
                        Remember me on this device
                    </label>
                    <Button type="submit" loading={form.processing}>
                        Sign in
                    </Button>
                </form>
                <p className="login-note">
                    Need an account or a password reset? Contact your school
                    administrator.
                </p>
            </section>
        </main>
    );
}
