<script setup lang="ts">
import { useForm } from '@inertiajs/vue3';
import { Save } from '@lucide/vue';
import { computed } from 'vue';
import { toast } from 'vue-sonner';
import { PhoneNumberInput } from '@/components/form/phone-number-input';
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 { Textarea } from '@/components/ui/textarea';
import { useFormDialog } from '@/composables/useFormDialog';
import { FIELD_LIMITS } from '@/lib/field-limits';
import { formErrors } from '@/lib/form';
import { store, update } from '@/routes/admin/master/suppliers';
import type { SupplierFormData, SupplierListItem } from '@/types/supplier';

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

const props = defineProps<{
    supplier?: SupplierListItem | null;
}>();

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

const form = useForm<SupplierFormData>({
    name: '',
    phone_number: '',
    address: '',
});

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

function populateForm(supplier: SupplierListItem | null | undefined) {
    resetForm();

    if (!supplier) {
        return;
    }

    form.name = supplier.name;
    form.phone_number = supplier.phone_number;
    form.address = supplier.address;
}

useFormDialog({
    open,
    source: () => props.supplier,
    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.supplier) {
        form.put(update.url(props.supplier.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 Supplier' : 'Tambah Supplier' }}</DialogTitle>
            </DialogHeader>

            <form @submit.prevent="submit">
                <FieldGroup>
                    <FieldSet class="grid gap-4">
                        <Field>
                            <FieldLabel for="supplier-name" required>Nama</FieldLabel>
                            <Input id="supplier-name" v-model="form.name" type="text"
                                placeholder="Contoh: PT Sumber Makmur" autofocus :maxlength="FIELD_LIMITS.name" />
                            <FieldError :errors="formErrors(form, 'name')" />
                        </Field>
                        <Field>
                            <FieldLabel for="supplier-phone-number">Nomor Telepon</FieldLabel>
                            <PhoneNumberInput id="supplier-phone-number" v-model="form.phone_number"
                                placeholder="Contoh: 08123456789" />
                            <FieldError :errors="formErrors(form, 'phone_number')" />
                        </Field>
                        <Field>
                            <FieldLabel for="supplier-address">Alamat</FieldLabel>
                            <Textarea id="supplier-address" v-model="form.address" rows="3"
                                placeholder="Contoh: Jl. Merdeka No. 45, Bandung" />
                            <FieldError :errors="formErrors(form, 'address')" />
                        </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>
