import { Head, router } from '@inertiajs/react';
import { Filter, Plus, X } from 'lucide-react';
import { useCallback, useMemo, useState } from 'react';
import { CardTable } from '@/components/card-table';
import { useCardTableExpand } from '@/components/hooks/use-card-table-expand';
import { ConfirmDialog } from '@/components/confirm-dialog';
import { Button } from '@/components/ui/button';
import {
Combobox,
ComboboxContent,
ComboboxEmpty,
ComboboxInput,
ComboboxItem,
ComboboxList,
} from '@/components/ui/combobox';
import {
Popover,
PopoverContent,
PopoverTrigger,
} from '@/components/ui/popover';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import {
destroy,
create as productCreate,
index as productIndex,
edit as productEdit,
toggleStatus,
} from '@/routes/admin/master/products';
import {
destroy as variantDestroy,
edit as variantEdit,
} from '@/routes/admin/master/products/variants';
import type { Product, ProductVariant } from './columns';
import { ProductCardRow } from './product-card';
import { VariantSubRow } from './variant/sub-row';
type Props = {
products: {
data: Product[];
current_page: number;
last_page: number;
per_page: number;
total: number;
};
categories: {
id: number;
name: string;
}[];
filters: {
status?: string;
name?: string;
stock?: string;
category?: string;
};
};
export default function ProductIndex({ products, categories, filters }: Props) {
const [deleting, setDeleting] = useState(null);
const [deletingVariant, setDeletingVariant] = useState<{
product: Product;
variant: ProductVariant;
} | null>(null);
const [filterOpen, setFilterOpen] = useState(false);
const [search, setSearch] = useState('');
const expand = useCardTableExpand(true);
const hasActiveFilters =
filters.status || filters.name || filters.stock || filters.category;
const pagination = {
current_page: products.current_page,
last_page: products.last_page,
per_page: products.per_page,
total: products.total,
};
const productNames = useMemo(() => {
const names = products.data.map((p) => p.name);
return [...new Set(names)].sort();
}, [products.data]);
const selectedCategory = useMemo(
() =>
categories.find((c) => String(c.id) === filters.category) ?? null,
[categories, filters.category],
);
function applyFilter(key: string, value: string) {
const newFilters = { ...filters };
if (value === '' || value === 'all') {
delete newFilters[key as keyof typeof newFilters];
} else {
newFilters[key as keyof typeof newFilters] = value;
}
router.get(productIndex(), newFilters, {
preserveState: true,
replace: true,
});
}
function clearFilters() {
router.get(
productIndex(),
{},
{
preserveState: true,
replace: true,
},
);
setFilterOpen(false);
}
function handlePageChange(page: number) {
router.get(
productIndex.url(),
{
page,
per_page: pagination.per_page,
search,
...filters,
},
{ preserveState: true, replace: true },
);
}
function handlePerPageChange(perPage: number) {
router.get(
productIndex.url(),
{
page: 1,
per_page: perPage,
search,
...filters,
},
{ preserveState: true, replace: true },
);
}
const handleSearchChange = useCallback(
(value: string) => {
setSearch(value);
router.get(
productIndex.url(),
{
page: 1,
per_page: pagination.per_page,
search: value,
...filters,
},
{ preserveState: true, replace: true },
);
},
[pagination.per_page, filters],
);
function handleDelete() {
if (!deleting) {
return;
}
router.delete(destroy.url(deleting.id), {
onSuccess: () => setDeleting(null),
});
}
function handleDeleteVariant() {
if (!deletingVariant) {
return;
}
router.delete(
variantDestroy.url({
product: deletingVariant.product.id,
variant: deletingVariant.variant.id,
}),
{
onSuccess: () => setDeletingVariant(null),
},
);
}
const filterToolbar = (
Filter
{hasActiveFilters && (
)}
applyFilter('name', (value as string) ?? '')
}
>
Tidak ada produk ditemukan.
{(name) => (
{name}
)}
Berdasarkan stok bagus
cat.name}
value={selectedCategory}
onValueChange={(value) =>
applyFilter(
'category',
value ? String(value.id) : '',
)
}
>
Tidak ada kategori ditemukan.
{(cat) => (
{cat.name}
)}
);
return (
<>
p.id}
expandedKeys={expand.expandedKeys}
onToggleExpand={expand.toggleExpand}
searchValue={search}
onSearchChange={handleSearchChange}
searchPlaceholder="Cari produk..."
pagination={pagination}
onPageChange={handlePageChange}
onPerPageChange={handlePerPageChange}
toolbar={filterToolbar}
renderCard={({
item,
index,
isExpanded,
onToggleExpand,
}) => (
{
window.location.href = productEdit.url(p.id);
}}
onDelete={(p) => setDeleting(p)}
toggleStatusUrl={(id) => toggleStatus.url(id)}
/>
)}
renderSubContent={(product) => (
{
window.location.href = variantEdit.url({
product: p.id,
variant: v.id,
});
}}
onDeleteVariantClick={(p, v) =>
setDeletingVariant({ product: p, variant: v })
}
/>
)}
/>
{
if (!open) {
setDeleting(null);
}
}}
title="Hapus Produk"
description={`Apakah Anda yakin ingin menghapus produk "${deleting?.name}"? Tindakan ini tidak dapat dibatalkan.`}
confirmLabel="Hapus"
onConfirm={handleDelete}
/>
{
if (!open) {
setDeletingVariant(null);
}
}}
title="Hapus Varian"
description={`Apakah Anda yakin ingin menghapus varian "${deletingVariant?.variant.name}" dari produk "${deletingVariant?.product.name}"? Tindakan ini tidak dapat dibatalkan.`}
confirmLabel="Hapus"
onConfirm={handleDeleteVariant}
/>
>
);
}