2025-12-30 15:03:19 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 採購單管理主頁面
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2026-01-19 17:07:45 +08:00
|
|
|
|
import { useState, useEffect } from "react";
|
2026-01-20 14:03:59 +08:00
|
|
|
|
import { Plus, ShoppingCart, Search, RotateCcw, Calendar, ChevronDown, ChevronUp } from 'lucide-react';
|
2025-12-30 15:03:19 +08:00
|
|
|
|
import { Button } from "@/Components/ui/button";
|
|
|
|
|
|
import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout";
|
|
|
|
|
|
import { Head, router } from "@inertiajs/react";
|
|
|
|
|
|
import PurchaseOrderTable from "@/Components/PurchaseOrder/PurchaseOrderTable";
|
|
|
|
|
|
import type { PurchaseOrder } from "@/types/purchase-order";
|
|
|
|
|
|
import Pagination from "@/Components/shared/Pagination";
|
2026-01-07 13:06:49 +08:00
|
|
|
|
import { getBreadcrumbs } from "@/utils/breadcrumb";
|
2026-01-20 14:03:59 +08:00
|
|
|
|
import { getDateRange } from "@/utils/format";
|
2026-01-13 17:00:58 +08:00
|
|
|
|
import { Can } from "@/Components/Permission/Can";
|
2026-01-13 17:09:52 +08:00
|
|
|
|
import { SearchableSelect } from "@/Components/ui/searchable-select";
|
2026-01-19 17:07:45 +08:00
|
|
|
|
import { Input } from "@/Components/ui/input";
|
|
|
|
|
|
import { Label } from "@/Components/ui/label";
|
|
|
|
|
|
import {
|
|
|
|
|
|
Select,
|
|
|
|
|
|
SelectContent,
|
|
|
|
|
|
SelectItem,
|
|
|
|
|
|
SelectTrigger,
|
|
|
|
|
|
SelectValue,
|
|
|
|
|
|
} from "@/Components/ui/select";
|
2026-01-27 17:23:31 +08:00
|
|
|
|
import { STATUS_OPTIONS } from "@/constants/purchase-order";
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
|
orders: {
|
|
|
|
|
|
data: PurchaseOrder[];
|
|
|
|
|
|
links: any[];
|
|
|
|
|
|
total: number;
|
|
|
|
|
|
from: number;
|
|
|
|
|
|
to: number;
|
|
|
|
|
|
};
|
|
|
|
|
|
filters: {
|
|
|
|
|
|
search?: string;
|
|
|
|
|
|
status?: string;
|
|
|
|
|
|
warehouse_id?: string;
|
2026-01-19 17:07:45 +08:00
|
|
|
|
date_start?: string;
|
|
|
|
|
|
date_end?: string;
|
2025-12-30 15:03:19 +08:00
|
|
|
|
sort_field?: string;
|
|
|
|
|
|
sort_direction?: string;
|
2026-01-13 17:09:52 +08:00
|
|
|
|
per_page?: string;
|
2025-12-30 15:03:19 +08:00
|
|
|
|
};
|
|
|
|
|
|
warehouses: { id: number; name: string }[];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default function PurchaseOrderIndex({ orders, filters, warehouses }: Props) {
|
2026-01-19 17:07:45 +08:00
|
|
|
|
// 篩選狀態
|
|
|
|
|
|
const [search, setSearch] = useState(filters.search || "");
|
|
|
|
|
|
const [status, setStatus] = useState<string>(filters.status || "all");
|
|
|
|
|
|
const [warehouseId, setWarehouseId] = useState<string>(filters.warehouse_id || "all");
|
|
|
|
|
|
const [dateStart, setDateStart] = useState(filters.date_start || "");
|
|
|
|
|
|
const [dateEnd, setDateEnd] = useState(filters.date_end || "");
|
2026-01-13 17:09:52 +08:00
|
|
|
|
const [perPage, setPerPage] = useState<string>(filters.per_page || "10");
|
2026-01-20 14:03:59 +08:00
|
|
|
|
const [dateRangeType, setDateRangeType] = useState('custom');
|
|
|
|
|
|
|
|
|
|
|
|
// Advanced Filter Toggle
|
|
|
|
|
|
const [showAdvancedFilter, setShowAdvancedFilter] = useState(
|
|
|
|
|
|
!!(filters.date_start || filters.date_end)
|
|
|
|
|
|
);
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
2026-01-19 17:07:45 +08:00
|
|
|
|
// 同步 URL 參數到 State (雖有初始值,但若由外部連結進入可確保同步)
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
setSearch(filters.search || "");
|
|
|
|
|
|
setStatus(filters.status || "all");
|
|
|
|
|
|
setWarehouseId(filters.warehouse_id || "all");
|
|
|
|
|
|
setDateStart(filters.date_start || "");
|
|
|
|
|
|
setDateEnd(filters.date_end || "");
|
|
|
|
|
|
setPerPage(filters.per_page || "10");
|
|
|
|
|
|
}, [filters]);
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
2026-01-19 17:07:45 +08:00
|
|
|
|
const handleFilter = () => {
|
|
|
|
|
|
router.get(
|
|
|
|
|
|
route('purchase-orders.index'),
|
|
|
|
|
|
{
|
|
|
|
|
|
search,
|
|
|
|
|
|
status: status === 'all' ? undefined : status,
|
|
|
|
|
|
warehouse_id: warehouseId === 'all' ? undefined : warehouseId,
|
|
|
|
|
|
date_start: dateStart,
|
|
|
|
|
|
date_end: dateEnd,
|
|
|
|
|
|
per_page: perPage,
|
|
|
|
|
|
sort_field: filters.sort_field,
|
|
|
|
|
|
sort_direction: filters.sort_direction,
|
|
|
|
|
|
},
|
|
|
|
|
|
{ preserveState: true, replace: true }
|
|
|
|
|
|
);
|
2025-12-30 15:03:19 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-19 17:07:45 +08:00
|
|
|
|
const handleReset = () => {
|
|
|
|
|
|
setSearch("");
|
|
|
|
|
|
setStatus("all");
|
|
|
|
|
|
setWarehouseId("all");
|
|
|
|
|
|
setDateStart("");
|
|
|
|
|
|
setDateEnd("");
|
2026-01-20 14:03:59 +08:00
|
|
|
|
setDateRangeType("custom");
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
2026-01-19 17:07:45 +08:00
|
|
|
|
router.get(route('purchase-orders.index'));
|
2025-12-30 15:03:19 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-20 14:03:59 +08:00
|
|
|
|
const handleDateRangeChange = (type: string) => {
|
|
|
|
|
|
setDateRangeType(type);
|
|
|
|
|
|
if (type === 'custom') return;
|
|
|
|
|
|
|
|
|
|
|
|
const { start, end } = getDateRange(type);
|
|
|
|
|
|
setDateStart(start);
|
|
|
|
|
|
setDateEnd(end);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-19 17:07:45 +08:00
|
|
|
|
const handlePerPageChange = (value: string) => {
|
|
|
|
|
|
setPerPage(value);
|
|
|
|
|
|
router.get(
|
|
|
|
|
|
route("purchase-orders.index"),
|
|
|
|
|
|
{
|
|
|
|
|
|
...filters,
|
|
|
|
|
|
per_page: value,
|
|
|
|
|
|
},
|
|
|
|
|
|
{ preserveState: false, replace: true, preserveScroll: true }
|
|
|
|
|
|
);
|
2025-12-30 15:03:19 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleNavigateToCreateOrder = () => {
|
2026-01-19 17:07:45 +08:00
|
|
|
|
router.get(route('purchase-orders.create'));
|
2026-01-13 17:09:52 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-12-30 15:03:19 +08:00
|
|
|
|
return (
|
2026-01-07 13:06:49 +08:00
|
|
|
|
<AuthenticatedLayout breadcrumbs={getBreadcrumbs("purchaseOrders")}>
|
2026-01-19 17:07:45 +08:00
|
|
|
|
<Head title="採購單管理" />
|
2025-12-30 15:03:19 +08:00
|
|
|
|
<div className="container mx-auto p-6 max-w-7xl">
|
|
|
|
|
|
<div className="flex items-center justify-between mb-6">
|
|
|
|
|
|
<div>
|
2026-01-13 13:30:51 +08:00
|
|
|
|
<h1 className="text-2xl font-bold text-grey-0 flex items-center gap-2">
|
2026-01-16 14:36:24 +08:00
|
|
|
|
<ShoppingCart className="h-6 w-6 text-primary-main" />
|
2026-01-13 13:30:51 +08:00
|
|
|
|
採購單管理
|
|
|
|
|
|
</h1>
|
|
|
|
|
|
<p className="text-gray-500 mt-1">
|
|
|
|
|
|
建立與管理採購訂單,追蹤入庫狀態
|
|
|
|
|
|
</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="flex gap-2">
|
2026-01-13 17:00:58 +08:00
|
|
|
|
<Can permission="purchase_orders.create">
|
|
|
|
|
|
<Button
|
|
|
|
|
|
onClick={handleNavigateToCreateOrder}
|
|
|
|
|
|
className="gap-2 button-filled-primary"
|
|
|
|
|
|
>
|
|
|
|
|
|
<Plus className="h-4 w-4" />
|
|
|
|
|
|
建立採購單
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</Can>
|
2025-12-30 15:03:19 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-01-19 17:07:45 +08:00
|
|
|
|
{/* 篩選區塊 */}
|
|
|
|
|
|
<div className="bg-white p-5 rounded-lg shadow-sm border border-gray-200 mb-6">
|
2026-01-20 14:03:59 +08:00
|
|
|
|
{/* Row 1: Search, Status, Warehouse */}
|
|
|
|
|
|
<div className="grid grid-cols-1 md:grid-cols-12 gap-4 mb-4">
|
|
|
|
|
|
<div className="md:col-span-4 space-y-1">
|
|
|
|
|
|
<Label className="text-xs font-medium text-grey-1">關鍵字搜尋</Label>
|
2026-01-19 17:07:45 +08:00
|
|
|
|
<div className="relative">
|
2026-01-20 14:03:59 +08:00
|
|
|
|
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 h-4 w-4" />
|
2026-01-19 17:07:45 +08:00
|
|
|
|
<Input
|
|
|
|
|
|
placeholder="搜尋採購單號、廠商..."
|
|
|
|
|
|
value={search}
|
|
|
|
|
|
onChange={(e) => setSearch(e.target.value)}
|
2026-01-20 14:03:59 +08:00
|
|
|
|
className="pl-10 h-9 block"
|
2026-01-19 17:07:45 +08:00
|
|
|
|
onKeyDown={(e) => e.key === 'Enter' && handleFilter()}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-01-20 14:03:59 +08:00
|
|
|
|
<div className="md:col-span-4 space-y-1">
|
|
|
|
|
|
<Label className="text-xs font-medium text-grey-1">訂單狀態</Label>
|
2026-01-19 17:07:45 +08:00
|
|
|
|
<Select value={status} onValueChange={setStatus}>
|
2026-01-20 14:03:59 +08:00
|
|
|
|
<SelectTrigger className="h-9">
|
2026-01-19 17:07:45 +08:00
|
|
|
|
<SelectValue placeholder="選擇狀態" />
|
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
|
<SelectContent>
|
|
|
|
|
|
<SelectItem value="all">全部狀態</SelectItem>
|
2026-01-27 17:23:31 +08:00
|
|
|
|
{STATUS_OPTIONS.map((option) => (
|
|
|
|
|
|
<SelectItem key={option.value} value={option.value}>
|
|
|
|
|
|
{option.label}
|
|
|
|
|
|
</SelectItem>
|
|
|
|
|
|
))}
|
2026-01-19 17:07:45 +08:00
|
|
|
|
</SelectContent>
|
|
|
|
|
|
</Select>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-01-20 14:03:59 +08:00
|
|
|
|
<div className="md:col-span-4 space-y-1">
|
|
|
|
|
|
<Label className="text-xs font-medium text-grey-1">入庫倉庫</Label>
|
2026-01-19 17:07:45 +08:00
|
|
|
|
<SearchableSelect
|
|
|
|
|
|
value={warehouseId}
|
|
|
|
|
|
onValueChange={setWarehouseId}
|
|
|
|
|
|
options={[
|
|
|
|
|
|
{ label: "全部倉庫", value: "all" },
|
|
|
|
|
|
...warehouses.map(w => ({ label: w.name, value: String(w.id) }))
|
|
|
|
|
|
]}
|
|
|
|
|
|
placeholder="選擇倉庫"
|
2026-01-20 14:03:59 +08:00
|
|
|
|
className="w-full h-9"
|
2026-01-19 17:07:45 +08:00
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
2026-01-20 14:03:59 +08:00
|
|
|
|
</div>
|
2026-01-19 17:07:45 +08:00
|
|
|
|
|
2026-01-20 14:03:59 +08:00
|
|
|
|
{/* Row 2: Date Filters (Collapsible) */}
|
|
|
|
|
|
{showAdvancedFilter && (
|
|
|
|
|
|
<div className="grid grid-cols-1 md:grid-cols-12 gap-4 items-end animate-in fade-in slide-in-from-top-2 duration-200">
|
|
|
|
|
|
<div className="md:col-span-6 space-y-2">
|
|
|
|
|
|
<Label className="text-xs font-medium text-grey-1">快速時間區間</Label>
|
|
|
|
|
|
<div className="flex flex-wrap gap-2">
|
|
|
|
|
|
{[
|
|
|
|
|
|
{ label: "今日", value: "today" },
|
|
|
|
|
|
{ label: "昨日", value: "yesterday" },
|
|
|
|
|
|
{ label: "本週", value: "this_week" },
|
|
|
|
|
|
{ label: "本月", value: "this_month" },
|
|
|
|
|
|
{ label: "上月", value: "last_month" },
|
|
|
|
|
|
].map((opt) => (
|
|
|
|
|
|
<Button
|
|
|
|
|
|
key={opt.value}
|
|
|
|
|
|
size="sm"
|
|
|
|
|
|
onClick={() => handleDateRangeChange(opt.value)}
|
|
|
|
|
|
className={
|
|
|
|
|
|
dateRangeType === opt.value
|
|
|
|
|
|
? 'button-filled-primary h-9 px-4 shadow-sm'
|
|
|
|
|
|
: 'button-outlined-primary h-9 px-4 bg-white'
|
|
|
|
|
|
}
|
|
|
|
|
|
>
|
|
|
|
|
|
{opt.label}
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
2026-01-19 17:07:45 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-01-20 14:03:59 +08:00
|
|
|
|
<div className="md:col-span-6">
|
|
|
|
|
|
<div className="grid grid-cols-2 gap-4 items-end">
|
|
|
|
|
|
<div className="space-y-1">
|
|
|
|
|
|
<Label className="text-xs text-grey-2 font-medium">開始日期</Label>
|
|
|
|
|
|
<div className="relative">
|
|
|
|
|
|
<Calendar className="absolute left-2.5 top-1/2 transform -translate-y-1/2 h-4 w-4 text-gray-400 pointer-events-none" />
|
|
|
|
|
|
<Input
|
|
|
|
|
|
type="date"
|
|
|
|
|
|
value={dateStart}
|
|
|
|
|
|
onChange={(e) => {
|
|
|
|
|
|
setDateStart(e.target.value);
|
|
|
|
|
|
setDateRangeType('custom');
|
|
|
|
|
|
}}
|
|
|
|
|
|
className="pl-9 block w-full h-9 bg-white"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="space-y-1">
|
|
|
|
|
|
<Label className="text-xs text-grey-2 font-medium">結束日期</Label>
|
|
|
|
|
|
<div className="relative">
|
|
|
|
|
|
<Calendar className="absolute left-2.5 top-1/2 transform -translate-y-1/2 h-4 w-4 text-gray-400 pointer-events-none" />
|
|
|
|
|
|
<Input
|
|
|
|
|
|
type="date"
|
|
|
|
|
|
value={dateEnd}
|
|
|
|
|
|
onChange={(e) => {
|
|
|
|
|
|
setDateEnd(e.target.value);
|
|
|
|
|
|
setDateRangeType('custom');
|
|
|
|
|
|
}}
|
|
|
|
|
|
className="pl-9 block w-full h-9 bg-white text-left"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2026-01-19 17:07:45 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2026-01-20 14:03:59 +08:00
|
|
|
|
)}
|
2026-01-19 17:07:45 +08:00
|
|
|
|
|
2026-01-20 14:03:59 +08:00
|
|
|
|
<div className="flex items-center justify-end border-t border-grey-4 pt-5 gap-3 mt-4">
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="ghost"
|
|
|
|
|
|
size="sm"
|
|
|
|
|
|
onClick={() => setShowAdvancedFilter(!showAdvancedFilter)}
|
|
|
|
|
|
className="mr-auto text-gray-500 hover:text-gray-900 h-9"
|
|
|
|
|
|
>
|
|
|
|
|
|
{showAdvancedFilter ? (
|
|
|
|
|
|
<>
|
|
|
|
|
|
<ChevronUp className="h-4 w-4 mr-1" />
|
|
|
|
|
|
收合篩選
|
|
|
|
|
|
</>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<>
|
|
|
|
|
|
<ChevronDown className="h-4 w-4 mr-1" />
|
|
|
|
|
|
進階篩選
|
|
|
|
|
|
{(dateStart || dateEnd) && (
|
|
|
|
|
|
<span className="ml-2 w-2 h-2 rounded-full bg-primary-main" />
|
|
|
|
|
|
)}
|
|
|
|
|
|
</>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</Button>
|
2026-01-19 17:07:45 +08:00
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
onClick={handleReset}
|
2026-01-20 14:03:59 +08:00
|
|
|
|
className="flex items-center gap-2 button-outlined-primary h-9"
|
2026-01-19 17:07:45 +08:00
|
|
|
|
>
|
|
|
|
|
|
<RotateCcw className="h-4 w-4" />
|
2026-01-20 14:03:59 +08:00
|
|
|
|
重置
|
2026-01-19 17:07:45 +08:00
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
onClick={handleFilter}
|
2026-01-20 14:03:59 +08:00
|
|
|
|
className="flex items-center gap-2 button-filled-primary h-9 px-6"
|
2026-01-19 17:07:45 +08:00
|
|
|
|
>
|
|
|
|
|
|
<Search className="h-4 w-4" />
|
2026-01-20 14:03:59 +08:00
|
|
|
|
查詢
|
2026-01-19 17:07:45 +08:00
|
|
|
|
</Button>
|
|
|
|
|
|
</div>
|
2025-12-30 15:03:19 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<PurchaseOrderTable
|
|
|
|
|
|
orders={orders.data}
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
2026-01-13 17:09:52 +08:00
|
|
|
|
{/* 分頁元件 - 統一樣式 */}
|
2026-01-19 17:07:45 +08:00
|
|
|
|
<div className="mt-4 flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4">
|
2026-01-13 17:09:52 +08:00
|
|
|
|
<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" }
|
|
|
|
|
|
]}
|
2026-01-19 17:07:45 +08:00
|
|
|
|
className="w-[100px] h-8"
|
2026-01-13 17:09:52 +08:00
|
|
|
|
showSearch={false}
|
|
|
|
|
|
/>
|
|
|
|
|
|
<span>筆</span>
|
|
|
|
|
|
</div>
|
2026-01-19 17:07:45 +08:00
|
|
|
|
<div className="w-full sm:w-auto flex justify-center sm:justify-end">
|
|
|
|
|
|
<Pagination links={orders.links} />
|
|
|
|
|
|
</div>
|
2025-12-30 15:03:19 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</AuthenticatedLayout>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|