'use client';
import { useState } from 'react';
import { BuildIbansDataTableAdd } from './table/data-table';
import { BuildIbansForm } from './form';
import { useGraphQlBuildIbansList } from '../queries';
import { useSearchParams, useRouter } from 'next/navigation';
import { Button } from '@/components/ui/button';

const PageAddBuildIbans = () => {
    const [page, setPage] = useState(1);
    const [limit, setLimit] = useState(10);
    const [sort, setSort] = useState({ createdAt: 'desc' });
    const [filters, setFilters] = useState({});

    const searchParams = useSearchParams();
    const router = useRouter();
    const buildId = searchParams?.get('build');
    const { data, isLoading, error, refetch } = useGraphQlBuildIbansList({ limit, skip: (page - 1) * limit, sort, filters: { ...filters, buildId } });
    const noUUIDFound = <>
        <div>Back To Build IBANs. No uuid is found on headers</div>
        <Button onClick={() => router.push(`/build-ibans?build=${buildId}`)}>Back to Build IBANs</Button>
    </>
    if (!buildId) { return noUUIDFound }
    return (
        <>
            <BuildIbansDataTableAdd data={data?.data || []} totalCount={data?.totalCount || 0} currentPage={page} pageSize={limit} onPageChange={setPage} onPageSizeChange={setLimit} refetchTable={refetch} buildId={buildId} />
            <BuildIbansForm refetchTable={refetch} buildId={buildId} />
        </>
    )
}

export { PageAddBuildIbans };
