'use client'; import ActionPopMenu from '@/components/common/ActionPopMenu'; import { MyTable, ColumnData } from '@/components/common/MyTable'; import StatusChangePopup from '@/components/common/StatusChangePopup'; import BaseButton from '@/components/ui-kit/BaseButton'; import BaseIconButton from '@/components/ui-kit/BaseIconButton'; import BaseInput from '@/components/ui-kit/BaseInput'; import BasePagination from '@/components/ui-kit/BasePagination'; import { useAuthContext } from '@/context/auth-context'; import { BoxStatus, BoxStatusList, IBox } from '@/data/box/box.model'; import { box_requests } from '@/data/box/box.requests'; import { IRealBox } from '@/data/real-box/real-box.model'; import { real_box_requests } from '@/data/real-box/real-box.requests'; import { DEFAULT_PAGE_SIZE, pageLinks } from '@/helpers/constants'; import useDebouncedInput from '@/hooks/useDebouncedInput'; import useInput from '@/hooks/useInput'; import { useMyNavigation } from '@/hooks/useMyNavigation'; import { useMyTranslation } from '@/hooks/useMyTranslation'; import useRequest from '@/hooks/useRequest'; import { file_service } from '@/services/file-service'; import { notifyUnknownError } from '@/services/notification'; import { getBoxStatusStyles, getStatusColor } from '@/theme/getStatusBoxStyles'; import { Add, QrCode, AddCircleOutline, Circle, Delete, Download, Edit, FilterList, FilterListOff, Search, PlusOne } from '@mui/icons-material'; import { Box, Button, Stack, Tooltip, Typography } from '@mui/material'; import { useRouter } from 'next/navigation'; import { log } from 'node:console'; import React, { useEffect, useMemo, useState } from 'react'; type Props = {}; const DashboardRealBoxesPage = (props: Props) => { const t = useMyTranslation(); const navigation = useMyNavigation(); const { isAdmin } = useAuthContext(); const [page, setPage] = useState(1); const [pageSize] = useState(DEFAULT_PAGE_SIZE); const { value: keyword, onChange: handleKeyword, setValue: setKeyword } = useInput(''); const [boxStatusFilter, setBoxStatusFilter] = useState(undefined); const [deleteIds, setDeleteIds] = useState([]); const [downloadIds, setDownloadIds] = useState([]); const [changeStatusIds, setChangeStatusIds] = useState([]); const boxStatusOptions = useMemo(() => { const p = ['READY_TO_INVOICE'] as BoxStatus[]; if (isAdmin) { p.push('READY'); } return p; }, [isAdmin]); const getBoxesQuery = useRequest( () => real_box_requests.getAll({ page: page, cargoId: keyword, status: boxStatusFilter, direction: 'desc', sort: 'id', }), { dependencies: [page, boxStatusFilter], selectData(data) { return data.data.data; }, } ); const { data: list, totalElements, totalPages, } = useMemo(() => { if (getBoxesQuery.data?.data) { return { data: getBoxesQuery.data.data, totalElements: getBoxesQuery.data.totalElements, totalPages: getBoxesQuery.data.totalPages, }; } else { return { data: [], totalElements: 0, totalPages: 0, }; } }, [getBoxesQuery]); const loading = getBoxesQuery.loading; const handleChange = (newPage: number) => { setTimeout(() => { setPage(newPage); }, 100); }; const resetFilter = () => { setPage(1); setKeyword(''); setBoxStatusFilter(undefined); }; const onDelete = async (id: number) => { if (deleteIds.includes(id)) return; try { setDeleteIds(p => [...p, id]); await real_box_requests.delete({ boxId: id }); getBoxesQuery.refetch(); } catch (error) { notifyUnknownError(error); } finally { setDeleteIds(prev => prev.filter(i => i !== id)); } }; const onDownloadExcel = async (id: number, name: string) => { if (downloadIds.includes(id)) return; try { setDownloadIds(p => [...p, id]); const response = await real_box_requests.downloadExcel({ boxId: id }); const file = new File([response.data], `${name}.xlsx`, { type: response.data.type }); file_service.download(file); } catch (error) { notifyUnknownError(error); } finally { setDownloadIds(prev => prev.filter(i => i !== id)); } }; const onDownloadQrCode = async (id: number) => { if (downloadIds.includes(id)) return; try { setDownloadIds(p => [...p, id]); const response = await real_box_requests.downloadQrCode({ boxId: id }); console.log(response, "rres"); const file = new File([response.data], 'qr.png', { type: response.data.type }); file_service.download(file); } catch (error) { notifyUnknownError(error); console.log(error); } finally { setDownloadIds(prev => prev.filter(i => i !== id)); } }; const onChangeStatus = async (id: number, newStatus: BoxStatus) => { if (changeStatusIds.includes(id)) return; try { setChangeStatusIds(p => [...p, id]); await box_requests.changeStatus({ packetId: id, status: newStatus }); getBoxesQuery.refetch(); } catch (error) { notifyUnknownError(error); } finally { setChangeStatusIds(prev => prev.filter(i => i !== id)); } }; useEffect(() => { const timeoutId = setTimeout(() => { setPage(1); getBoxesQuery.refetch(); }, 350); return () => clearTimeout(timeoutId); }, [keyword]); // No, PartyName, PacketName, PartyTozaOg'irlik, CountOfItems, WeightOfItems, CargoID, PassportNameFamily - PacketStatusForInvoice const columns: ColumnData[] = [ { label: t('No'), width: 100, renderCell(data, rowIndex) { return (page - 1) * pageSize + rowIndex + 1; }, }, { label: t("qr_code"), width: 120, renderCell: data => { return ; }, }, { dataKey: 'id', label: t("add_more"), width: 120, renderCell: data => { return ; }, }, { dataKey: 'boxName', label: t('cargo_id'), width: 120, }, { dataKey: "boxName", label: t('party_name'), width: 120, }, // { // dataKey: 'name', // label: t('name'), // width: 120, // }, // { // dataKey: 'packetNetWeight', // label: t("weight"), // width: 120, // }, { dataKey: 'packetsCount', label: t('count_of_items'), width: 120, }, // { // dataKey: 'totalNetWeight', // label: t("party_weight"), // width: 120, // }, // { // dataKey: 'passportName', // label: t('client'), // width: 120, // }, // { // dataKey: 'status', // label: t('status'), // width: 240, // renderHeaderCell(rowIndex) { // return ( // // {t('box_status')} // { // return { // icon: , // label: t(stat), // onClick() { // setBoxStatusFilter(stat); // setPage(1); // }, // }; // })} // mainIcon={} // placement={{ // anchorOrigin: { // vertical: 'bottom', // horizontal: 'center', // }, // transformOrigin: { // horizontal: 'center', // vertical: 'top', // }, // }} // /> // // ); // }, // renderCell(data) { // return ( // { // return { // icon: ( // // ), // label: t(stat), // onClick: () => { // onChangeStatus(data.id, stat); // }, // }; // })} // /> // ); // }, // }, { label: '', width: 100, numeric: true, renderCell(data, rowIndex) { return ( , label: t('edit'), onClick: () => { navigation.push(pageLinks.dashboard.real_boxes.edit(data.id)); }, }, { icon: , label: t('delete'), onClick: () => { onDelete(data.id); }, dontCloseOnClick: true, loading: deleteIds.includes(data.id), }, { icon: , label: t('download_excel'), onClick: () => { onDownloadExcel(data.id, data.boxName); }, loading: downloadIds.includes(data.id), dontCloseOnClick: true, }, ]} /> ); }, }, ]; return ( } href={pageLinks.dashboard.real_boxes.create}> {t('create_box')} {t('boxes')} , }} placeholder={t('filter_packet_name')} value={keyword} onChange={e => { setKeyword(e.target.value); }} /> } size='small' onClick={resetFilter}> {t('reset_filter')} ); }; export default DashboardRealBoxesPage;