import AppLayout from '@/layouts/app-layout';
import { type BreadcrumbItem, PaginatedTasks } from '@/types';
import { Head, router, Link } from '@inertiajs/react';
import { type Task as ResidencyProgram } from '@/types';
import { toast } from 'sonner';
import Table from '@/components/Table';
import { Edit, Trash } from 'lucide-react';
import { Button, buttonVariants } from '@/components/ui/button';
import {
    Card,
    CardContent,
    CardFooter,
    CardHeader,
    CardTitle,
} from "@/components/ui/card"
import { Eye } from 'lucide-react';

const breadcrumbs: BreadcrumbItem[] = [
    {
        title: 'Residency Program',
        href: '/residency_program'
    },
];
interface Props {
    programs: PaginatedTasks;
}

const columns = [
    { label: 'Name', key: 'name', sortable: true },
    { label: 'Email', key: 'email', sortable: true },
    { label: 'Actions', key: 'actions' },
];

export default function Index({ programs }: Props) {
    const queryParams = new URLSearchParams(window.location.search);
    const search = queryParams.get('search') || '';
    const deleteTask = (id: number) => {
        if (confirm('Are you sure?')) {
            router.delete(route('residency_program.destroy', { id }), {
                onSuccess: () => toast.success('Program deleted successfully!'),
                onError: () => toast.error('Failed to delete the program.'),
            });
        }
    };

    return (
        <AppLayout breadcrumbs={[{ title: 'Residency Program', href: '/residency_program' }]}>
            <Head title="Residency Program List" />

            <div className="m-5">
                <Card>
                    <CardHeader>
                        <div className="flex items-center justify-between">
                            <h1 className="text-2xl font-semibold text-gray-800 dark:text-white">
                                Residency Program List
                            </h1>
                            <Link className={(buttonVariants({ variant: 'default', className: 'ml-auto' }))} href={route('residency_program.create')}>
                                Residency Program Create
                            </Link>
                        </div>
                    </CardHeader>
                    <CardContent>
                        <Table<ResidencyProgram>
                            data={programs.data}
                            total={programs.total}
                            currentPage={programs.current_page}
                            rowsPerPage={programs.per_page}
                            columns={columns}
                            searchableKeys={['name']}
                            renderCell={(key, value, row) => {

                                if (key === 'actions') {
                                    return (
                                        <div className="flex gap-2">
                                            <Link
                                                href={route('residency_program.edit', row.id)}
                                                className={buttonVariants({ variant: 'secondary', size: 'icon' })}
                                            >
                                                <Edit className="w-4 h-4 text-blue-500" />
                                            </Link>
                                            <button
                                                className={(buttonVariants({ variant: 'secondary', size: 'icon' }))}
                                                onClick={() => deleteTask(row.id)}
                                            >
                                                <Trash className="w-4 h-4 text-red-500" />
                                            </button>
                                        </div>
                                    );
                                }

                                return value;
                            }}
                            onPageChange={(page) => {
                                const params = Object.fromEntries(new URLSearchParams(window.location.search).entries());
                                router.get(window.location.pathname, {
                                    ...params,
                                    page,
                                }, { preserveState: true, replace: true });
                            }}
                            onPerPageChange={(perPage) => {
                                const params = Object.fromEntries(new URLSearchParams(window.location.search).entries());
                                router.get(window.location.pathname, {
                                    ...params,
                                    page: 1,
                                    per_page: perPage,
                                }, { preserveState: true, replace: true });
                            }}
                            onSearchChange={(searchVal) => {
                                const params = Object.fromEntries(new URLSearchParams(window.location.search).entries());
                                if (searchVal) {
                                    params.search = searchVal;
                                } else {
                                    delete params.search;
                                }
                                router.get(window.location.pathname, {
                                    ...params,
                                    page: 1,
                                }, { preserveState: true, replace: true });
                            }}
                            searchValue={search}
                        />
                    </CardContent>
                    {/*<CardFooter className="">*/}

                    {/*</CardFooter>*/}
                </Card>



            </div>
        </AppLayout>
    );
}

