import { Head, Link, router } from '@inertiajs/react';
import { Plus } from 'lucide-react';
import { useMemo, useState } from 'react';
import { CardTable } from '@/components/data-display';
import { DeleteConfirmDialog } from '@/components/dialogs';
import { FilterPopover } from '@/components/data-display';
import { useCardTableExpand } from '@/components/hooks/use-card-table-expand';
import { PageHeader } from '@/components/layout';
import { Button } from '@/components/ui/button';
import {
Combobox,
ComboboxContent,
ComboboxEmpty,
ComboboxInput,
ComboboxItem,
ComboboxList,
} from '@/components/ui/combobox';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { useCan } from '@/hooks/use-can';
import { useServerTable } from '@/hooks/use-server-table';
import {
destroy,
create as cuttingCreate,
index as cuttingIndex,
edit as cuttingEdit,
} from '@/routes/admin/manage/cuttings';
import type { Cutting } from './columns';
import { CuttingCardRow } from './cutting-card';
import { CuttingItemSubRow } from './cutting-sub-row';
type Props = {
cuttings: {
data: Cutting[];
current_page: number;
last_page: number;
per_page: number;
total: number;
};
filters: {
product_name?: string;
status?: string;
};
filterOptions: {
statusOptions: Array<{ value: string; label: string }>;
productNames: Array<{ product_name: string }>;
};
};
export default function CuttingIndex({
cuttings,
filters,
filterOptions,
}: Props) {
const { can } = useCan();
const [deleting, setDeleting] = useState(null);
const expand = useCardTableExpand(true);
const pagination = {
current_page: cuttings.current_page,
last_page: cuttings.last_page,
per_page: cuttings.per_page,
total: cuttings.total,
};
const {
search,
filterOpen,
setFilterOpen,
handlePageChange,
handlePerPageChange,
handleSearchChange,
applyFilter,
clearFilters,
} = useServerTable({
route: () => cuttingIndex.url(),
pagination,
filters,
filterWithParams: false,
});
const hasActiveFilters = Boolean(filters.product_name || filters.status);
function handleDelete() {
if (!deleting) {
return;
}
router.delete(destroy.url(deleting.id), {
onSuccess: () => setDeleting(null),
});
}
const selectedProductName = useMemo(
() =>
filterOptions.productNames.find(
(p) => p.product_name === filters.product_name,
) ?? null,
[filterOptions.productNames, filters.product_name],
);
const filterToolbar = (
product.product_name}
value={selectedProductName}
onValueChange={(value) =>
applyFilter(
'product_name',
value ? value.product_name : '',
)
}
>
Tidak ada produk ditemukan.
{(product) => (
{product.product_name}
)}
);
return (
<>
Tambah
) : undefined
}
/>
c.id}
expandedKeys={expand.expandedKeys}
onToggleExpand={expand.toggleExpand}
searchValue={search}
onSearchChange={handleSearchChange}
toolbar={filterToolbar}
pagination={pagination}
onPageChange={handlePageChange}
onPerPageChange={handlePerPageChange}
rowClassName={(c) =>
c.status === 'in_progress'
? 'border-l-4 border-l-yellow-500'
: ''
}
renderCard={({
item,
index,
isExpanded,
onToggleExpand,
}) => (
{
router.visit(cuttingEdit.url(c.id));
}}
onDelete={(c) => setDeleting(c)}
/>
)}
renderSubContent={(cutting) => (
)}
/>
{
if (!open) {
setDeleting(null);
}
}}
title="Hapus Cutting"
description={(cutting) =>
`Apakah Anda yakin ingin menghapus cutting "${cutting.cutting_results?.[0]?.product_name ?? '-'}"? Tindakan ini tidak dapat dibatalkan.`
}
onConfirm={handleDelete}
/>
>
);
}