import { Head, useForm } from '@inertiajs/react';
import { FormEventHandler } from 'react';
import AdminLayout from '@/Layouts/AdminLayout';

interface Props {
    settings: Record<string, string>;
}

export default function Edit({ settings }: Props) {
    const { data, setData, patch, processing, errors } = useForm({
        title: settings.title ?? '',
        logo: settings.logo ?? '',
        favicon: settings.favicon ?? '',
        donate_cost: settings.donate_cost ?? '200',
    });

    const submit: FormEventHandler = (e) => {
        e.preventDefault();
        patch(route('admin.settings.update'));
    };

    return (
        <AdminLayout>
            <Head title="Settings" />
            <h1 className="mb-6 text-2xl font-bold">Site settings</h1>

            <form onSubmit={submit} className="max-w-lg space-y-4 rounded-lg bg-gray-900 p-6 ring-1 ring-white/10">
                <div>
                    <label className="mb-1 block text-sm text-gray-300">Site title</label>
                    <input
                        value={data.title}
                        onChange={(e) => setData('title', e.target.value)}
                        className="w-full rounded-md border-gray-700 bg-gray-800 text-white"
                    />
                    {errors.title && <p className="mt-1 text-sm text-red-400">{errors.title}</p>}
                </div>
                <div>
                    <label className="mb-1 block text-sm text-gray-300">Logo path</label>
                    <input
                        value={data.logo}
                        onChange={(e) => setData('logo', e.target.value)}
                        className="w-full rounded-md border-gray-700 bg-gray-800 text-white"
                    />
                </div>
                <div>
                    <label className="mb-1 block text-sm text-gray-300">Favicon path</label>
                    <input
                        value={data.favicon}
                        onChange={(e) => setData('favicon', e.target.value)}
                        className="w-full rounded-md border-gray-700 bg-gray-800 text-white"
                    />
                </div>
                <div>
                    <label className="mb-1 block text-sm text-gray-300">Roulette spin cost</label>
                    <input
                        type="number"
                        value={data.donate_cost}
                        onChange={(e) => setData('donate_cost', e.target.value)}
                        className="w-full rounded-md border-gray-700 bg-gray-800 text-white"
                    />
                    {errors.donate_cost && <p className="mt-1 text-sm text-red-400">{errors.donate_cost}</p>}
                </div>
                <button
                    disabled={processing}
                    className="rounded-md bg-brand-600 px-4 py-2 text-sm font-medium text-white hover:bg-brand-700 disabled:opacity-50"
                >
                    Save settings
                </button>
            </form>
        </AdminLayout>
    );
}
