'use client';
import { useState } from "react";
import { useGraphQlBuildPartsList } from "./queries";
import { LivingSpaceBuildPartsDataTable } from "./data-table";


const PageLivingSpacePartsTableSection = (
    { buildId, partID, setPartID, setIsPartsEnabled }: {
        buildId: string;
        partID: string | null;
        setPartID: (id: string | null) => void;
        setIsPartsEnabled: (enabled: boolean) => void;
    }
) => {
    const [page, setPage] = useState(1);
    const [limit, setLimit] = useState(10);
    const [sort, setSort] = useState({ createdAt: 'desc' });
    const [filters, setFilters] = useState({});

    const { data, isLoading, error, refetch } = useGraphQlBuildPartsList({ limit, skip: (page - 1) * limit, sort, filters: { ...filters, buildId: buildId } });

    const handlePageChange = (newPage: number) => { setPage(newPage) };
    const handlePageSizeChange = (newSize: number) => { setLimit(newSize); setPage(1) };
    if (isLoading) { return <div className="flex items-center justify-center p-8">Loading...</div> }
    if (error) { return <div className="flex items-center justify-center p-8 text-red-500">Error loading users</div> }

    return <>
        <LivingSpaceBuildPartsDataTable
            data={data?.data || []} totalCount={data?.totalCount || 0} currentPage={page} pageSize={limit} onPageChange={handlePageChange} onPageSizeChange={handlePageSizeChange}
            refetchTable={refetch} buildId={buildId} partID={partID} setPartID={setPartID} />
    </>;

}

export default PageLivingSpacePartsTableSection;