import { Head, Link, useForm } from '@inertiajs/react';
import type { FormEvent } from 'react';

type Category = { id: number; parent_id: number | null; name: string; slug: string; system_type: string | null; description: string | null; seo_title: string | null; meta_description: string | null; is_active: boolean };
type Option = { id: number; name: string };

export default function CategoryForm({ category, categories }: { category: Category | null; categories: Option[] }) {
    const { data, setData, transform, post, put, processing, errors } = useForm({
        parent_id: category?.parent_id ?? '',
        name: category?.name ?? '',
        slug: category?.slug ?? '',
        system_type: category?.system_type ?? '',
        description: category?.description ?? '',
        seo_title: category?.seo_title ?? '',
        meta_description: category?.meta_description ?? '',
        is_active: category?.is_active ?? true,
    });
    function submit(event: FormEvent) {
        event.preventDefault();
        transform((values) => ({ ...values, parent_id: values.parent_id || null }));
        category ? put(`/admin/categories/${category.id}`) : post('/admin/categories');
    }
    return (
        <>
            <Head title={category ? 'Edit Category' : 'Create Category'} />
            <main className="min-h-screen bg-slate-50 px-4 py-6 text-slate-900 sm:px-6 lg:px-8">
                <form onSubmit={submit} className="mx-auto max-w-4xl rounded-lg border border-slate-200 bg-white p-5">
                    <h1 className="text-2xl font-semibold">{category ? 'Edit category' : 'Create category'}</h1>
                    <div className="mt-5 grid gap-4 md:grid-cols-2">
                        <Field label="Parent" error={errors.parent_id}><select className="input" value={data.parent_id} onChange={(event) => setData('parent_id', event.target.value)}><option value="">Top level</option>{categories.map((item) => <option key={item.id} value={item.id}>{item.name}</option>)}</select></Field>
                        <Field label="Name" error={errors.name}><input className="input" value={data.name} onChange={(event) => setData('name', event.target.value)} /></Field>
                        <Field label="Slug" error={errors.slug}><input className="input" value={data.slug} onChange={(event) => setData('slug', event.target.value)} /></Field>
                        <Field label="System type" error={errors.system_type}><input className="input" value={data.system_type} onChange={(event) => setData('system_type', event.target.value)} /></Field>
                        <Field label="SEO title" error={errors.seo_title}><input className="input" value={data.seo_title} onChange={(event) => setData('seo_title', event.target.value)} /></Field>
                        <Field label="Meta description" error={errors.meta_description}><textarea className="input min-h-24" value={data.meta_description} onChange={(event) => setData('meta_description', event.target.value)} /></Field>
                        <Field label="Description" error={errors.description}><textarea className="input min-h-24" value={data.description} onChange={(event) => setData('description', event.target.value)} /></Field>
                        <label className="flex items-center gap-2 text-sm font-medium"><input type="checkbox" checked={data.is_active} onChange={(event) => setData('is_active', event.target.checked)} /> Active</label>
                    </div>
                    <div className="mt-6 flex justify-end gap-2"><Link href="/admin/categories" className="rounded-md border border-slate-300 px-4 py-2 text-sm font-semibold">Cancel</Link><button disabled={processing} className="rounded-md bg-cyan-700 px-4 py-2 text-sm font-semibold text-white disabled:opacity-60">Save</button></div>
                </form>
            </main>
        </>
    );
}

function Field({ label, error, children }: { label: string; error?: string; children: React.ReactNode }) {
    return <label className="grid gap-1 text-sm font-medium text-slate-700"><span>{label}</span>{children}{error ? <span className="text-xs text-red-600">{error}</span> : null}</label>;
}
