import Pagination from '@mui/material/Pagination';
import { setCurrentPage } from '@/redux/features/workers-slice';
import { load } from '@/redux/features/workers-slice'
import { useDispatch } from 'react-redux';
import { AppDispatch , useAppSelector } from '@/redux/store';

export default function ListPagination() {

    const dispatch = useDispatch<AppDispatch>();

    const currentPage = useAppSelector((state) => state.workersReducer.value.currentPage)
    const total = useAppSelector((state) => state.workersReducer.value.total)

    const handlePagination = (event: React.ChangeEvent<unknown>, value: number) => {
        dispatch(load({page:value}));
        dispatch(setCurrentPage(value));
    };

    return (
        <>
            {Math.ceil(total / 10) > 1 ? (
                <div className="flex justify-center items-center py-12">
                    <Pagination
                        sx={{ direction: 'ltr' }}
                        count={Math.ceil(total / 10)}
                        page={currentPage}
                        onChange={handlePagination}
                        variant="outlined"
                        shape="rounded"
                    />
                </div>
            ) : (
                <></>
            )}
        </>
    );
}