import { ConfirmDialog } from '@/components/confirm-dialog';
import { DataTable } from '@/components/data-table';
import { DatePicker } from '@/components/date-picker';
import InputError from '@/components/input-error';
import { Button } from '@/components/ui/button';
import {
    Dialog,
    DialogContent,
    DialogFooter,
    DialogHeader,
    DialogTitle,
} from '@/components/ui/dialog';
import { Label } from '@/components/ui/label';
import { approve, destroy, index as leaveRequestIndex, reject, store, update } from '@/routes/admin/hr/leave-requests';
import { Form, Head, router } from '@inertiajs/react';
import { Plus } from 'lucide-react';
import { useEffect, useState } from 'react';
import type { LeaveRequest } from './columns';
import { createLeaveRequestColumns } from './columns';

type Props = {
    leaveRequests: LeaveRequest[];
};

export default function LeaveRequestIndex({ leaveRequests }: Props) {
    const [createOpen, setCreateOpen] = useState(false);
    const [editing, setEditing] = useState<LeaveRequest | null>(null);
    const [deleting, setDeleting] = useState<LeaveRequest | null>(null);
    const [approving, setApproving] = useState<LeaveRequest | null>(null);
    const [rejecting, setRejecting] = useState<LeaveRequest | null>(null);
    const [startDate, setStartDate] = useState<Date | undefined>(undefined);
    const [endDate, setEndDate] = useState<Date | undefined>(undefined);
    const [editingStartDate, setEditingStartDate] = useState<Date | undefined>(undefined);
    const [editingEndDate, setEditingEndDate] = useState<Date | undefined>(undefined);

    useEffect(() => {
        if (editing) {
            setEditingStartDate(new Date(editing.start_date));
            setEditingEndDate(new Date(editing.end_date));
        } else {
            setEditingStartDate(undefined);
            setEditingEndDate(undefined);
        }
    }, [editing]);

    function handleDelete() {
        if (!deleting) {
            return;
        }

        router.delete(destroy(deleting.id), {
            onSuccess: () => setDeleting(null),
        });
    }

    function handleApprove() {
        if (!approving) {
            return;
        }

        router.post(approve(approving.id), {}, {
            onSuccess: () => setApproving(null),
        });
    }

    function handleReject() {
        if (!rejecting) {
            return;
        }

        router.post(reject(rejecting.id), {}, {
            onSuccess: () => setRejecting(null),
        });
    }

    const columns = createLeaveRequestColumns({
        handleEdit: (leaveRequest) => setEditing(leaveRequest),
        handleDeleteClick: (leaveRequest) => setDeleting(leaveRequest),
        handleApprove: (leaveRequest) => setApproving(leaveRequest),
        handleReject: (leaveRequest) => setRejecting(leaveRequest),
    });

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

            <div className="flex h-full flex-1 flex-col gap-6 overflow-x-auto p-4 md:p-6">
                <div className="flex items-center justify-between">
                    <div>
                        <h2 className="text-2xl font-semibold tracking-tight">
                            Cuti
                        </h2>
                    </div>
                    <Dialog open={createOpen} onOpenChange={(open) => {
                        setCreateOpen(open);
                        if (!open) {
                            setStartDate(undefined);
                            setEndDate(undefined);
                        }
                    }}>
                        <Button asChild>
                            <button
                                type="button"
                                onClick={() => setCreateOpen(true)}
                            >
                                <Plus className="h-4 w-4" />
                                Tambah
                            </button>
                        </Button>
                        <DialogContent>
                            <Form action={store()} resetOnSuccess onSuccess={() => setCreateOpen(false)}>
                                {({ errors, processing }) => {

                                    return (
                                        <>
                                            <DialogHeader>
                                                <DialogTitle>Tambah Permohonan Cuti</DialogTitle>
                                            </DialogHeader>
                                            <div className="grid gap-4 py-4">
                                                <div className="grid gap-2">
                                                    <Label>
                                                        Tanggal Mulai{' '} <span className="text-destructive">*</span>
                                                    </Label>
                                                    <input type="hidden" name="start_date" value={startDate ? startDate.toISOString().split('T')[0] : ''} />
                                                    <DatePicker
                                                        value={startDate}
                                                        onChange={setStartDate}
                                                        placeholder="Pilih tanggal mulai"
                                                        min={new Date()}
                                                    />
                                                    <InputError message={errors.start_date} />
                                                </div>
                                                <div className="grid gap-2">
                                                    <Label>
                                                        Tanggal Selesai{' '} <span className="text-destructive">*</span>
                                                    </Label>
                                                    <input type="hidden" name="end_date" value={endDate ? endDate.toISOString().split('T')[0] : ''} />
                                                    <DatePicker
                                                        value={endDate}
                                                        onChange={setEndDate}
                                                        placeholder="Pilih tanggal selesai"
                                                        min={startDate}
                                                    />
                                                    <InputError message={errors.end_date} />
                                                </div>
                                            </div>
                                            <DialogFooter>
                                                <Button
                                                    type="button"
                                                    variant="outline"
                                                    onClick={() => setCreateOpen(false)}
                                                >
                                                    Batal
                                                </Button>
                                                <Button
                                                    type='submit'
                                                    disabled={processing}
                                                >
                                                    {processing
                                                        ? 'Menyimpan...'
                                                        : 'Simpan'}
                                                </Button>
                                            </DialogFooter>
                                        </>
                                    );
                                }}
                            </Form>
                        </DialogContent>
                    </Dialog>
                </div>

                <DataTable
                    columns={columns}
                    data={leaveRequests}
                    searchKey="employee.user.user_profile.full_name"
                    searchPlaceholder="Cari nama karyawan..."
                    emptyText="Belum ada data permohonan cuti."
                />

                <Dialog
                    open={editing !== null}
                    onOpenChange={(open) => {
                        if (!open) {
                            setEditing(null);
                            setEditingStartDate(undefined);
                            setEditingEndDate(undefined);
                        }
                    }}
                >
                    <DialogContent>
                        {editing && (
                            <Form action={update(editing.id)} resetOnSuccess onSuccess={() => {
                                setEditing(null);
                                setEditingStartDate(undefined);
                                setEditingEndDate(undefined);
                            }}>
                                {({ errors, processing }) => {

                                    return (
                                        <>
                                            <DialogHeader>
                                                <DialogTitle>Edit Permohonan Cuti</DialogTitle>
                                            </DialogHeader>
                                            <div className="grid gap-4 py-4">
                                                <div className="grid gap-2">
                                                    <Label>Tanggal Mulai{' '} <span className="text-destructive">*</span></Label>
                                                    <input type="hidden" name="start_date" value={editingStartDate ? editingStartDate.toISOString().split('T')[0] : ''} />
                                                    <DatePicker
                                                        value={editingStartDate}
                                                        onChange={setEditingStartDate}
                                                        placeholder="Pilih tanggal mulai"
                                                        min={new Date()}
                                                    />
                                                    <InputError message={errors.start_date} />
                                                </div>
                                                <div className="grid gap-2">
                                                    <Label>Tanggal Selesai{' '} <span className="text-destructive">*</span></Label>
                                                    <input type="hidden" name="end_date" value={editingEndDate ? editingEndDate.toISOString().split('T')[0] : ''} />
                                                    <DatePicker
                                                        value={editingEndDate}
                                                        onChange={setEditingEndDate}
                                                        placeholder="Pilih tanggal selesai"
                                                        min={editingStartDate}
                                                    />
                                                    <InputError message={errors.end_date} />
                                                </div>
                                            </div>
                                            <DialogFooter>
                                                <Button
                                                    type="button"
                                                    variant="outline"
                                                    onClick={() => {
                                                        setEditing(null);
                                                    }}
                                                >
                                                    Batal
                                                </Button>
                                                <Button
                                                    type="submit"
                                                    disabled={processing}
                                                >
                                                    {processing
                                                        ? 'Menyimpan...'
                                                        : 'Simpan'}
                                                </Button>
                                            </DialogFooter>
                                        </>
                                    );
                                }}
                            </Form>
                        )}
                    </DialogContent>
                </Dialog>

                <ConfirmDialog
                    open={deleting !== null}
                    onOpenChange={(open) => {
                        if (!open) {
                            setDeleting(null);
                        }
                    }}
                    title="Hapus Permohonan Cuti"
                    description={`Apakah Anda yakin ingin menghapus permohonan cuti ini? Tindakan ini tidak dapat dibatalkan.`}
                    confirmLabel="Hapus"
                    onConfirm={handleDelete}
                />

                <ConfirmDialog
                    open={approving !== null}
                    onOpenChange={(open) => {
                        if (!open) {
                            setApproving(null);
                        }
                    }}
                    title="Setujui Permohonan Cuti"
                    description={`Apakah Anda yakin ingin menyetujui permohonan cuti ini?`}
                    confirmLabel="Setujui"
                    onConfirm={handleApprove}
                />

                <ConfirmDialog
                    open={rejecting !== null}
                    onOpenChange={(open) => {
                        if (!open) {
                            setRejecting(null);
                        }
                    }}
                    title="Tolak Permohonan Cuti"
                    description={`Apakah Anda yakin ingin menolak permohonan cuti ini?`}
                    confirmLabel="Tolak"
                    onConfirm={handleReject}
                />
            </div>
        </>
    );
}

LeaveRequestIndex.layout = {
    breadcrumbs: [
        {
            title: 'HR',
            href: leaveRequestIndex(),
        },
        {
            title: 'Cuti',
            href: leaveRequestIndex(),
        },
    ],
};
