import InputError from '@/components/input-error';
import TextLink from '@/components/text-link';
import { Button } from '@/components/ui/button';
import AuthLayout from '@/layouts/auth-layout';
import { Head, useForm } from '@inertiajs/react';
import { FormEventHandler } from 'react';

export default function FindWorkspace({ domain }: { domain: string }) {
    const form = useForm({ subdomain: '' });

    const submit: FormEventHandler = (e) => {
        e.preventDefault();
        form.post(route('workspace.go'));
    };

    return (
        <AuthLayout title="Log in to your workspace" description="Enter your workspace address.">
            <Head title="Find your workspace" />
            <form onSubmit={submit} className="flex flex-col gap-5">
                <div className="grid gap-2">
                    <div className="border-input focus-within:ring-ring/50 flex items-center rounded-md border shadow-xs focus-within:ring-[3px]">
                        <input
                            required
                            autoFocus
                            value={form.data.subdomain}
                            onChange={(e) => form.setData('subdomain', e.target.value.toLowerCase().trim())}
                            className="h-9 min-w-0 flex-1 bg-transparent px-3 text-sm outline-none"
                            placeholder="acme"
                            aria-label="Workspace address"
                        />
                        <span className="text-muted-foreground pr-3 text-sm">.{domain}</span>
                    </div>
                    <InputError message={form.errors.subdomain} />
                </div>
                <Button type="submit" disabled={form.processing}>
                    Continue
                </Button>
                <p className="text-muted-foreground text-center text-sm">
                    New to Tawkzo? <TextLink href={route('signup')}>Create a workspace</TextLink>
                </p>
            </form>
        </AuthLayout>
    );
}
