import { Head, Link, useForm } from '@inertiajs/react';
import GuestLayout from "@/Layouts/GuestLayout.jsx";

import InputError from '@/components/InputError';
import Checkbox from '@/components/Checkbox';
import PrimaryButton from '@/components/PrimaryButton';
import TextInput from '@/components/TextInput';
import { Label } from "@/components/ui/label"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card.jsx";
import { Alert, AlertDescription } from "@/components/ui/alert.jsx";

export default function Login({ status, canResetPassword }) {
    const { data, setData, post, processing, errors, reset } = useForm({
        email: '',
        password: '',
        remember: false,
    });

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

        post(route('login'), {
            onFinish: () => reset('password'),
        });
    };

    return (
        <GuestLayout>
            <Head title="Login" />
            <div className="flex flex-col gap-6">
                { status && (<Alert>
                    <AlertDescription>
                        { status }
                    </AlertDescription>
                </Alert> ) }
                <Card>
                    <CardHeader>
                        <CardTitle className="text-2xl">Login</CardTitle>
                        <CardDescription>
                            Enter your email below to login to your account
                        </CardDescription>
                    </CardHeader>
                    <CardContent>
                        <form onSubmit={ submit }>
                            <div className="flex flex-col gap-6">
                                <div className="grid gap-2">
                                    <Label htmlFor="email">E-mail Address</Label>
                                    <TextInput type="email" id="email" placeholder="me@yumj.in" value={ data.email }
                                               tabIndex="1" onChange={ (e) => setData('email', e.target.value) } required />
                                    <InputError message={ errors.email } className="mt-2" />
                                </div>
                                <div className="grid gap-2">
                                    <div className="flex items-center">
                                        <Label htmlFor="password">Password</Label>
                                        <Link href={ route('password.request') }
                                              className="ml-auto inline-block text-sm underline-offset-4 hover:underline">
                                            Forgot your password?
                                        </Link>
                                    </div>
                                    <TextInput id="password" type="password" name="password" value={ data.password }
                                               autoComplete="current-password" tabIndex="2"
                                               onChange={ (e) => setData('password', e.target.value) } />
                                    <InputError message={ errors.password } className="mt-2" />
                                </div>
                                <div className="grid gap-2">
                                    <div className="flex items-center space-x-2">
                                        <Checkbox id="remember" tabIndex="3" name="remember" checked={ data.remember }
                                            onChange={ (e) => setData('remember', e.target.checked) } />
                                        <label htmlFor="remember"
                                               className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70">
                                            Remember me
                                        </label>
                                    </div>
                                </div>
                                <PrimaryButton type="submit" disabled={ processing } tabIndex="4" className="w-full">
                                    Login
                                </PrimaryButton>
                            </div>
                            <div className="mt-4 text-center text-sm">
                                Don&apos;t have an account?&nbsp;
                                <Link href={ route('register') } className="underline underline-offset-4">
                                    Sign up
                                </Link>
                            </div>
                        </form>
                    </CardContent>
                </Card>
            </div>
        </GuestLayout>
    );
}
