Files
star-erp/resources/js/Pages/Inventory/Count/Index.tsx
sky121113 2efaded77b
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 50s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
統一庫存盤點與盤調 UI 及邏輯:修正狀態顯示、操作權限與列表樣式
2026-01-29 13:41:31 +08:00

382 lines
19 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout';
import { Head, Link, useForm, router } from '@inertiajs/react';
import { useState, useCallback, useEffect } from 'react';
import { debounce } from "lodash";
import { SearchableSelect } from "@/Components/ui/searchable-select";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/Components/ui/table';
import { Button } from '@/Components/ui/button';
import { Input } from '@/Components/ui/input';
import { Badge } from '@/Components/ui/badge';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/Components/ui/dialog";
import { Label } from '@/Components/ui/label';
import {
Plus,
Search,
X,
ClipboardCheck,
Eye,
Pencil,
Trash2
} from 'lucide-react';
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/Components/ui/alert-dialog";
import Pagination from '@/Components/shared/Pagination';
import { Can } from '@/Components/Permission/Can';
export default function Index({ auth, docs, warehouses, filters }: any) {
const [isCreateDialogOpen, setIsCreateDialogOpen] = useState(false);
const [deleteId, setDeleteId] = useState<string | null>(null);
const { data, setData, post, processing, reset, errors, delete: destroy } = useForm({
warehouse_id: '',
remarks: '',
});
const [searchTerm, setSearchTerm] = useState(filters.search || "");
const [warehouseFilter, setWarehouseFilter] = useState(filters.warehouse_id || "all");
const [perPage, setPerPage] = useState(filters.per_page || "10");
// Sync state with props
useEffect(() => {
setSearchTerm(filters.search || "");
setWarehouseFilter(filters.warehouse_id || "all");
setPerPage(filters.per_page || "10");
}, [filters]);
// Debounced Search Handler
const debouncedSearch = useCallback(
debounce((term: string, warehouse: string) => {
router.get(
route('inventory.count.index'),
{ ...filters, search: term, warehouse_id: warehouse === "all" ? "" : warehouse },
{ preserveState: true, replace: true, preserveScroll: true }
);
}, 500),
[filters]
);
const handleSearchChange = (term: string) => {
setSearchTerm(term);
debouncedSearch(term, warehouseFilter);
};
const handleFilterChange = (value: string) => {
setWarehouseFilter(value);
router.get(
route('inventory.count.index'),
{ ...filters, warehouse_id: value === "all" ? "" : value },
{ preserveState: false, replace: true, preserveScroll: true }
);
};
const handleClearSearch = () => {
setSearchTerm("");
router.get(
route('inventory.count.index'),
{ ...filters, search: "", warehouse_id: warehouseFilter === "all" ? "" : warehouseFilter },
{ preserveState: true, replace: true, preserveScroll: true }
);
};
const handlePerPageChange = (value: string) => {
setPerPage(value);
router.get(
route('inventory.count.index'),
{ ...filters, per_page: value },
{ preserveState: false, replace: true, preserveScroll: true }
);
};
const handleCreate = (e) => {
e.preventDefault();
post(route('inventory.count.store'), {
onSuccess: () => {
setIsCreateDialogOpen(false);
reset();
},
});
};
const confirmDelete = (id: string) => {
setDeleteId(id);
};
const handleDelete = () => {
if (deleteId) {
destroy(route('inventory.count.destroy', [deleteId]), {
onSuccess: () => setDeleteId(null),
onError: () => setDeleteId(null),
});
}
};
const getStatusBadge = (status) => {
switch (status) {
case 'draft':
return <Badge variant="secondary">稿</Badge>;
case 'counting':
return <Badge className="bg-blue-500 hover:bg-blue-600"></Badge>;
case 'completed':
return <Badge className="bg-green-500 hover:bg-green-600"></Badge>;
case 'adjusted':
return <Badge className="bg-purple-500 hover:bg-purple-600">調</Badge>;
case 'cancelled':
return <Badge variant="destructive"></Badge>;
default:
return <Badge variant="outline">{status}</Badge>;
}
};
return (
<AuthenticatedLayout
breadcrumbs={[
{ label: '商品與庫存與管理', href: '#' },
{ label: '庫存盤點', href: route('inventory.count.index'), isPage: true },
]}
>
<Head title="庫存盤點" />
<div className="container mx-auto p-6 max-w-7xl">
<div className="flex items-center justify-between mb-6">
<div>
<h1 className="text-2xl font-bold text-grey-0 flex items-center gap-2">
<ClipboardCheck className="h-6 w-6 text-primary-main" />
</h1>
<p className="text-gray-500 mt-1">
</p>
</div>
</div>
{/* Toolbar */}
<div className="bg-white rounded-lg shadow-sm border p-4 mb-6">
<div className="flex flex-col md:flex-row gap-4">
{/* Search */}
<div className="flex-1 relative">
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 h-4 w-4" />
<Input
placeholder="搜尋盤點單號、備註..."
value={searchTerm}
onChange={(e) => handleSearchChange(e.target.value)}
className="pl-10 pr-10 h-9"
/>
{searchTerm && (
<button
onClick={handleClearSearch}
className="absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-400 hover:text-gray-600"
>
<X className="h-4 w-4" />
</button>
)}
</div>
{/* Warehouse Filter */}
<SearchableSelect
value={warehouseFilter}
onValueChange={handleFilterChange}
options={[
{ label: "所有倉庫", value: "all" },
...warehouses.map((w: any) => ({ label: w.name, value: w.id.toString() }))
]}
placeholder="選擇倉庫"
className="w-full md:w-[200px] h-9"
/>
{/* Action Buttons */}
<div className="flex gap-2 w-full md:w-auto">
<Can permission="inventory.view">
<Dialog open={isCreateDialogOpen} onOpenChange={setIsCreateDialogOpen}>
<DialogTrigger asChild>
<Button className="flex-1 md:flex-none button-filled-primary">
<Plus className="w-4 h-4 mr-2" />
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-[425px]">
<form onSubmit={handleCreate}>
<DialogHeader>
<DialogTitle></DialogTitle>
<DialogDescription>
</DialogDescription>
</DialogHeader>
<div className="grid gap-4 py-4">
<div className="space-y-2">
<Label htmlFor="warehouse"></Label>
<SearchableSelect
value={data.warehouse_id}
onValueChange={(val) => setData('warehouse_id', val)}
options={warehouses.map((w: any) => ({ label: w.name, value: w.id.toString() }))}
placeholder="請選擇倉庫"
className="h-9"
/>
{errors.warehouse_id && <p className="text-red-500 text-sm">{errors.warehouse_id}</p>}
</div>
<div className="space-y-2">
<Label htmlFor="remarks"></Label>
<Input
id="remarks"
className="h-9"
value={data.remarks}
onChange={(e) => setData('remarks', e.target.value)}
/>
</div>
</div>
<DialogFooter>
<Button type="button" variant="outline" className="button-outlined-primary" onClick={() => setIsCreateDialogOpen(false)}>
</Button>
<Button type="submit" className="button-filled-primary" disabled={processing || !data.warehouse_id}>
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
</Can>
</div>
</div>
</div>
<div className="bg-white rounded-xl border border-gray-200 shadow-sm overflow-hidden">
<Table>
<TableHeader className="bg-gray-50">
<TableRow>
<TableHead className="w-[50px] text-center">#</TableHead>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead className="text-center"></TableHead>
</TableRow>
</TableHeader>
<TableBody>
{docs.data.length === 0 ? (
<TableRow>
<TableCell colSpan={9} className="text-center h-24 text-gray-500">
</TableCell>
</TableRow>
) : (
docs.data.map((doc, index) => (
<TableRow key={doc.id}>
<TableCell className="text-gray-500 font-medium text-center">
{(docs.current_page - 1) * docs.per_page + index + 1}
</TableCell>
<TableCell className="font-medium text-primary-main">{doc.doc_no}</TableCell>
<TableCell>{doc.warehouse_name}</TableCell>
<TableCell>{getStatusBadge(doc.status)}</TableCell>
<TableCell className="text-gray-500 text-sm">{doc.snapshot_date}</TableCell>
<TableCell>
<span className="font-medium text-gray-700">{doc.counted_items}</span>
<span className="text-gray-400 mx-1">/</span>
<span className="text-gray-500">{doc.total_items}</span>
</TableCell>
<TableCell className="text-gray-500 text-sm">{doc.completed_at || '-'}</TableCell>
<TableCell className="text-sm">{doc.created_by}</TableCell>
<TableCell className="text-center">
<div className="flex items-center justify-center gap-2">
<Can permission="inventory.view">
<Link href={route('inventory.count.show', [doc.id])}>
<Button
variant="outline"
size="sm"
className="button-outlined-primary"
title={['completed', 'adjusted'].includes(doc.status) ? '查閱' : '盤點'}
>
{['completed', 'adjusted'].includes(doc.status) ? (
<Eye className="w-4 h-4 ml-0.5" />
) : (
<Pencil className="w-4 h-4 ml-0.5" />
)}
</Button>
</Link>
{!['completed', 'adjusted'].includes(doc.status) && (
<Button
variant="outline"
size="sm"
className="button-outlined-error"
title="作廢"
onClick={() => confirmDelete(doc.id)}
>
<Trash2 className="w-4 h-4 ml-0.5" />
</Button>
)}
</Can>
</div>
</TableCell>
</TableRow>
))
)}
</TableBody>
</Table>
</div>
<div className="mt-4 flex flex-col sm:flex-row items-center justify-between gap-4">
<div className="flex items-center gap-4">
<div className="flex items-center gap-2 text-sm text-gray-500">
<span></span>
<SearchableSelect
value={perPage}
onValueChange={handlePerPageChange}
options={[
{ label: "10", value: "10" },
{ label: "20", value: "20" },
{ label: "50", value: "50" },
{ label: "100", value: "100" }
]}
className="w-[100px] h-8"
showSearch={false}
/>
<span></span>
</div>
<span className="text-sm text-gray-500"> {docs.total} </span>
</div>
<Pagination links={docs.links} />
</div>
<AlertDialog open={!!deleteId} onOpenChange={(open) => !open && setDeleteId(null)}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle></AlertDialogTitle>
<AlertDialogDescription>
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel></AlertDialogCancel>
<AlertDialogAction onClick={handleDelete} className="bg-red-600 hover:bg-red-700"></AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
</AuthenticatedLayout>
);
}