<script setup lang="ts">
import { useForm } from '@inertiajs/vue3';
import { Save } from '@lucide/vue';
import { computed, watch } 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 { formErrors } from '@/lib/form';
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;
}

watch(
    () => props.leaveRequest,
    (leaveRequest) => {
        populateForm(leaveRequest);
    },
);

watch(open, (isOpen) => {
    if (isOpen) {
        populateForm(props.leaveRequest);
    } else {
        resetForm();
    }
});

function submit() {
    const options = {
        preserveScroll: true,
        onSuccess: () => {
            open.value = false;
        },
        onError: () => {
            toast.error('Gagal menyimpan data. Periksa kembali formulir.');
        },
    };

    if (isEditing.value && props.leaveRequest) {
        form.put(`/admin/hr/leave-requests/${props.leaveRequest.id}`, options);
    } else {
        form.post('/admin/hr/leave-requests', 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>
