import { Head, Link, useForm } from '@inertiajs/react';
import { ChevronDown } from 'lucide-react';
import UserController from '@/actions/App/Http/Controllers/Admin/UserController';
import InputError from '@/components/input-error';
import MemberPage from '@/components/member/member-page';
import PasswordInput from '@/components/password-input';
import RolePicker from '@/components/role-picker';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { create, index } from '@/routes/admin/users';
import type { SelectOption } from '@/types';

export default function UsersCreate({
    roles,
    branches,
}: {
    roles: string[];
    branches: SelectOption[];
}) {
    const form = useForm({
        name: '',
        email: '',
        phone: '',
        password: '',
        password_confirmation: '',
        role: '',
        branch_id: '',
    });

    const submit = (event: React.FormEvent) => {
        event.preventDefault();
        form.submit(UserController.store(), {
            onError: () => form.reset('password', 'password_confirmation'),
        });
    };

    return (
        <>
            <Head title="Register user" />

            <MemberPage
                title="Register user"
                description="Create an account for a member or a member of staff. Their email is marked as verified."
            >
                <form onSubmit={submit} className="grid max-w-3xl gap-5">
                    <section className="grid gap-4 rounded-landing-lg border border-surface-200 bg-white p-5 sm:grid-cols-2 sm:p-6">
                        <h2 className="font-display text-base font-semibold text-ink sm:col-span-2">
                            Account details
                        </h2>
                        <div className="grid content-start gap-2 sm:col-span-2">
                            <Label htmlFor="name" required>
                                Full name
                            </Label>
                            <Input
                                id="name"
                                value={form.data.name}
                                onChange={(event) =>
                                    form.setData('name', event.target.value)
                                }
                                required
                                maxLength={255}
                                autoComplete="off"
                                placeholder="e.g. Hauwa Garba"
                            />
                            <InputError message={form.errors.name} />
                        </div>
                        <div className="grid content-start gap-2">
                            <Label htmlFor="email" required>
                                Email
                            </Label>
                            <Input
                                id="email"
                                type="email"
                                value={form.data.email}
                                onChange={(event) =>
                                    form.setData('email', event.target.value)
                                }
                                required
                                maxLength={255}
                                autoComplete="off"
                                placeholder="name@example.com"
                            />
                            <InputError message={form.errors.email} />
                        </div>
                        <div className="grid content-start gap-2">
                            <Label htmlFor="phone">Phone</Label>
                            <Input
                                id="phone"
                                type="tel"
                                value={form.data.phone}
                                onChange={(event) =>
                                    form.setData('phone', event.target.value)
                                }
                                maxLength={15}
                                placeholder="08012345678"
                            />
                            <InputError message={form.errors.phone} />
                        </div>
                        <div className="grid content-start gap-2">
                            <Label htmlFor="password" required>
                                Password
                            </Label>
                            <PasswordInput
                                id="password"
                                value={form.data.password}
                                onChange={(event) =>
                                    form.setData('password', event.target.value)
                                }
                                required
                                autoComplete="new-password"
                            />
                            <InputError message={form.errors.password} />
                        </div>
                        <div className="grid content-start gap-2">
                            <Label htmlFor="password_confirmation" required>
                                Confirm password
                            </Label>
                            <PasswordInput
                                id="password_confirmation"
                                value={form.data.password_confirmation}
                                onChange={(event) =>
                                    form.setData(
                                        'password_confirmation',
                                        event.target.value,
                                    )
                                }
                                required
                                autoComplete="new-password"
                            />
                        </div>
                        {branches.length > 0 && (
                            <div className="grid content-start gap-2 sm:col-span-2">
                                <Label htmlFor="branch_id">Branch</Label>
                                <div className="relative">
                                    <select
                                        id="branch_id"
                                        value={form.data.branch_id}
                                        onChange={(event) =>
                                            form.setData(
                                                'branch_id',
                                                event.target.value,
                                            )
                                        }
                                        className="h-10 w-full appearance-none rounded-md border border-input bg-white pr-9 pl-3 text-sm text-ink shadow-xs outline-none focus-visible:border-brand focus-visible:ring-3 focus-visible:ring-brand/15"
                                    >
                                        <option value="">No branch yet</option>
                                        {branches.map((branch) => (
                                            <option
                                                key={branch.value}
                                                value={branch.value}
                                            >
                                                {branch.label}
                                            </option>
                                        ))}
                                    </select>
                                    <ChevronDown className="pointer-events-none absolute top-1/2 right-3 size-4 -translate-y-1/2 text-ink-400" />
                                </div>
                                <InputError message={form.errors.branch_id} />
                            </div>
                        )}
                        <p className="text-xs text-ink-400 sm:col-span-2">
                            Share the password with the person privately and ask
                            them to change it in Settings after they log in.
                        </p>
                    </section>

                    <section className="grid gap-3 rounded-landing-lg border border-surface-200 bg-white p-5 sm:p-6">
                        <div>
                            <h2 className="font-display text-base font-semibold text-ink">
                                Role
                            </h2>
                            <p className="text-xs text-ink-400">
                                Members have no staff access. A role opens the
                                admin pages its permissions allow.
                            </p>
                        </div>
                        <RolePicker
                            roles={roles}
                            value={form.data.role}
                            onChange={(role) => form.setData('role', role)}
                        />
                        <InputError message={form.errors.role} />
                    </section>

                    <div className="flex items-center gap-2">
                        <Button disabled={form.processing}>
                            Register user
                        </Button>
                        <Link
                            href={index()}
                            className="rounded-full px-3.5 py-1.5 text-sm font-semibold text-ink-500 hover:text-ink"
                        >
                            Cancel
                        </Link>
                    </div>
                </form>
            </MemberPage>
        </>
    );
}

UsersCreate.layout = {
    breadcrumbs: [
        { title: 'Users', href: index() },
        { title: 'Register user', href: create() },
    ],
};
