import { FileUploadInput } from '@admin/components/ui/forms/file-upload-input';
import { FormGroup } from '@admin/components/ui/forms/form-group';
import { formClassName, formHelpClassName } from '@admin/components/ui/field-styles';
import { useFormResponse } from '@admin/hooks/use-form-response';
import { useSingletonPermissions } from '@admin/hooks/use-singleton-permissions';
import { dashboardPersistentLayout } from '@admin/layouts/dashboard-page-layout';
import { cn } from '@admin/lib/utils';
import { Head, useForm } from '@inertiajs/react';
import { FormEvent } from 'react';

type FooterPayload = {
    background_image: string;
    blurb: string;
};

type EditFooterProps = {
    footer: FooterPayload;
    exists: boolean;
};

function EditFooter({ footer, exists }: EditFooterProps) {
    const { canEdit } = useSingletonPermissions('footer-setting');
    const { data, setData, put, processing, errors } = useForm(footer);

    const action = useFormResponse({
        loadingMessage: exists ? 'Saving changes...' : 'Saving footer...',
    });

    const handleSubmit = (event: FormEvent) => {
        event.preventDefault();

        put('/admin/api/footer_settings', {
            onStart: action.onStart,
            onSuccess: action.onSuccess,
            onError: action.onError,
            preserveScroll: true,
        });
    };

    return (
        <>
            <Head title="Footer" />

            <div className="grid grid-cols-12 gap-6">
                <div className="col-span-12">
                    <p className={cn('mb-4', formHelpClassName)}>
                        The background sits behind the public footer. The text appears under the logo.
                    </p>

                    <form className={formClassName} noValidate onSubmit={handleSubmit}>
                        <FileUploadInput
                            label="Footer background (1920×1080 px recommended)"
                            inputId="footer-background"
                            uploadUrl="/admin/api/upload_footer_background"
                            previewUrl={data.background_image ? `/storage/${data.background_image}` : '/assets/images/footer/footer-bg-2.jpg'}
                            error={errors.background_image}
                            helpText="Until a photo is uploaded, the site uses the current footer background."
                            onSuccess={(filename) => setData('background_image', filename)}
                        />

                        <FormGroup
                            as="textarea"
                            label="Text under the logo"
                            error={errors.blurb}
                            inputProps={{
                                value: data.blurb,
                                rows: 4,
                                onChange: (event) => setData('blurb', event.target.value),
                            }}
                        />

                        {canEdit && (
                            <button type="submit" className="ti-btn ti-btn-primary" disabled={processing}>
                                {exists ? 'Save changes' : 'Save'}
                            </button>
                        )}
                        {!canEdit && <p className={formHelpClassName}>You do not have permission to edit the footer.</p>}
                    </form>
                </div>
            </div>
        </>
    );
}

export default Object.assign(EditFooter, {
    layout: dashboardPersistentLayout('Footer', 'Background photo and the text under the logo'),
});
