<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 { DatePicker } from '@/components/ui/date-picker';
import {
    Dialog,
    DialogContent,
    DialogFooter,
    DialogHeader,
    DialogTitle,
} from '@/components/ui/dialog';
import {
    Field,
    FieldError,
    FieldGroup,
    FieldLabel,
    FieldSet,
} from '@/components/ui/field';
import { useFormDialog } from '@/composables/useFormDialog';
import { formErrors } from '@/lib/form';
import { store, update } from '@/routes/admin/hr/leave_requests';
import type { LeaveRequestFormData, LeaveRequestListItem } from '@/types/leave-request';

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

const props = defineProps<{
    leaveRequest?: LeaveRequestListItem | null;
}>();

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

const form = useForm<LeaveRequestFormData>({
    start_date: '',
    end_date: '',
});

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

function populateForm(leaveRequest: LeaveRequestListItem | null | undefined) {
    resetForm();

    if (!leaveRequest) {
        return;
    }

    form.start_date = leaveRequest.start_date_input;
    form.end_date = leaveRequest.end_date_input;
}

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

            <form @submit.prevent="submit">
                <FieldGroup>
                    <FieldSet class="grid gap-4">
                        <p class="text-sm text-muted-foreground">
                            Pengajuan cuti harus diajukan minimal H-1 (tanggal mulai paling cepat besok).
                        </p>
                        <Field>
                            <FieldLabel for="leave-start-date" required>Tanggal Mulai</FieldLabel>
                            <DatePicker id="leave-start-date" v-model="form.start_date" autofocus />
                            <FieldError :errors="formErrors(form, 'start_date')" />
                        </Field>
                        <Field>
                            <FieldLabel for="leave-end-date" required>Tanggal Selesai</FieldLabel>
                            <DatePicker id="leave-end-date" v-model="form.end_date" />
                            <FieldError :errors="formErrors(form, 'end_date')" />
                        </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...' : 'Ajukan' }}
                    </Button>
                </DialogFooter>
            </form>
        </DialogContent>
    </Dialog>
</template>
