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

type ShippingMethod = { id: number; name: string; code: string; provider: string | null; type: string; price: string; free_from: string | null; is_active: boolean };

export default function ShippingMethodForm({ shippingMethod }: { shippingMethod: ShippingMethod | null }) {
    const { data, setData, transform, post, put, processing, errors } = useForm({
        name: shippingMethod?.name ?? '',
        code: shippingMethod?.code ?? '',
        provider: shippingMethod?.provider ?? '',
        type: shippingMethod?.type ?? 'fixed',
        price: shippingMethod?.price ?? '0.00',
        free_from: shippingMethod?.free_from ?? '',
        is_active: shippingMethod?.is_active ?? true,
    });

    function submit(event: FormEvent) {
        event.preventDefault();
        transform((values) => ({ ...values, free_from: values.free_from || null }));
        shippingMethod ? put(`/admin/shipping-methods/${shippingMethod.id}`) : post('/admin/shipping-methods');
    }

    return (
        <>
            <Head title={shippingMethod ? 'Edit Shipping Method' : 'Create Shipping Method'} />
            <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">{shippingMethod ? 'Edit shipping method' : 'Create shipping method'}</h1>
                    <div className="mt-5 grid gap-4 md:grid-cols-2">
                        <Field label="Name" error={errors.name}><input className="input" value={data.name} onChange={(event) => setData('name', event.target.value)} /></Field>
                        <Field label="Code" error={errors.code}><input className="input" value={data.code} onChange={(event) => setData('code', event.target.value)} /></Field>
                        <Field label="Provider" error={errors.provider}><input className="input" value={data.provider} onChange={(event) => setData('provider', event.target.value)} /></Field>
                        <Field label="Type" error={errors.type}><select className="input" value={data.type} onChange={(event) => setData('type', event.target.value)}><option value="fixed">Fixed</option><option value="free">Free</option><option value="courier">Courier</option><option value="office_pickup">Office pickup</option><option value="local_pickup">Local pickup</option></select></Field>
                        <Field label="Price" error={errors.price}><input className="input" type="number" min="0" step="0.01" value={data.price} onChange={(event) => setData('price', event.target.value)} /></Field>
                        <Field label="Free from" error={errors.free_from}><input className="input" type="number" min="0" step="0.01" value={data.free_from} onChange={(event) => setData('free_from', 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/shipping-methods" 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>;
}
