<script setup lang="ts">
import { useForm } from '@inertiajs/vue3';
import { Save } from '@lucide/vue';
import { computed } from 'vue';
import { toast } from 'vue-sonner';
import { Button } from '@/components/ui/button';
import {
    Dialog,
    DialogContent,
    DialogFooter,
    DialogHeader,
    DialogTitle,
} from '@/components/ui/dialog';
import {
    Field,
    FieldError,
    FieldGroup,
    FieldLabel,
    FieldSet,
} from '@/components/ui/field';
import { Input } from '@/components/ui/input';
import { useFormDialog } from '@/composables/useFormDialog';
import { FIELD_LIMITS } from '@/lib/field-limits';
import { formErrors } from '@/lib/form';
import { store, update } from '@/routes/admin/master/categories';
import type { CategoryFormData, CategoryListItem } from '@/types/category';

const open = defineModel<boolean>('open', { default: false });

const props = defineProps<{
    category?: CategoryListItem | null;
}>();

const isEditing = computed(() => props.category != null);

const form = useForm<CategoryFormData>({
    name: '',
});

function resetForm() {
    form.reset();
    form.clearErrors();
}

function populateForm(category: CategoryListItem | null | undefined) {
    resetForm();

    if (!category) {
        return;
    }

    form.name = category.name;
}

useFormDialog({
    open,
    source: () => props.category,
    populate: populateForm,
    reset: resetForm,
});

function submit() {
    const options = {
        preserveScroll: true,
        onSuccess: () => {
            open.value = false;
        },
        onError: (errors: any) => {
            if (errors.system) {
                toast.error(errors.system);
            }
        },
    };

    if (isEditing.value && props.category) {
        form.put(update.url(props.category.id), options);

        return;
    }

    form.post(store.url(), options);
}
</script>

<template>
    <Dialog v-model:open="open">
        <DialogContent class="sm:max-w-md">
            <DialogHeader>
                <DialogTitle>{{ isEditing ? 'Ubah Kategori' : 'Tambah Kategori' }}</DialogTitle>
            </DialogHeader>

            <form @submit.prevent="submit">
                <FieldGroup>
                    <FieldSet class="grid gap-4">
                        <Field>
                            <FieldLabel for="category-name" required>Nama</FieldLabel>
                            <Input id="category-name" v-model="form.name" type="text" placeholder="Contoh: Daster"
                                autofocus :maxlength="FIELD_LIMITS.categoryName" />
                            <FieldError :errors="formErrors(form, 'name')" />
                        </Field>
                    </FieldSet>
                </FieldGroup>

                <DialogFooter class="mt-6">
                    <Button type="button" variant="outline" :disabled="form.processing" @click="open = false">
                        Batal
                    </Button>
                    <Button type="submit" :disabled="form.processing">
                        <Save class="size-4" />
                        {{ form.processing ? 'Menyimpan...' : 'Simpan' }}
                    </Button>
                </DialogFooter>
            </form>
        </DialogContent>
    </Dialog>
</template>
