first commit
This commit is contained in:
@@ -0,0 +1,185 @@
|
||||
/**
|
||||
* 新增供貨商品對話框
|
||||
*/
|
||||
|
||||
import { useState, useMemo } from "react";
|
||||
import { Button } from "../ui/button";
|
||||
import { Input } from "../ui/input";
|
||||
import { Label } from "../ui/label";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
} from "../ui/dialog";
|
||||
import {
|
||||
Command,
|
||||
CommandEmpty,
|
||||
CommandGroup,
|
||||
CommandInput,
|
||||
CommandItem,
|
||||
CommandList,
|
||||
} from "../ui/command";
|
||||
import {
|
||||
Popover,
|
||||
PopoverContent,
|
||||
PopoverTrigger,
|
||||
} from "../ui/popover";
|
||||
import { Check, ChevronsUpDown } from "lucide-react";
|
||||
import { cn } from "../ui/utils";
|
||||
import type { Product } from "../../types/product";
|
||||
import type { SupplyProduct } from "../../types/vendor";
|
||||
|
||||
interface AddSupplyProductDialogProps {
|
||||
open: boolean;
|
||||
products: Product[];
|
||||
existingSupplyProducts: SupplyProduct[];
|
||||
onClose: () => void;
|
||||
onAdd: (productId: string, lastPrice?: number) => void;
|
||||
}
|
||||
|
||||
export default function AddSupplyProductDialog({
|
||||
open,
|
||||
products,
|
||||
existingSupplyProducts,
|
||||
onClose,
|
||||
onAdd,
|
||||
}: AddSupplyProductDialogProps) {
|
||||
const [selectedProductId, setSelectedProductId] = useState<string>("");
|
||||
const [lastPrice, setLastPrice] = useState<string>("");
|
||||
const [openCombobox, setOpenCombobox] = useState(false);
|
||||
|
||||
// 過濾掉已經在供貨列表中的商品
|
||||
const availableProducts = useMemo(() => {
|
||||
const existingIds = new Set(existingSupplyProducts.map(sp => sp.productId));
|
||||
return products.filter(p => !existingIds.has(p.id));
|
||||
}, [products, existingSupplyProducts]);
|
||||
|
||||
const selectedProduct = availableProducts.find(p => p.id === selectedProductId);
|
||||
|
||||
const handleAdd = () => {
|
||||
if (!selectedProductId) return;
|
||||
|
||||
const price = lastPrice ? parseFloat(lastPrice) : undefined;
|
||||
onAdd(selectedProductId, price);
|
||||
|
||||
// 重置表單
|
||||
setSelectedProductId("");
|
||||
setLastPrice("");
|
||||
};
|
||||
|
||||
const handleCancel = () => {
|
||||
setSelectedProductId("");
|
||||
setLastPrice("");
|
||||
onClose();
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onClose}>
|
||||
<DialogContent className="max-w-xl">
|
||||
<DialogHeader>
|
||||
<DialogTitle>新增供貨商品</DialogTitle>
|
||||
<DialogDescription>選擇該廠商可供應的商品並設定採購價格。</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-4">
|
||||
{/* 商品選擇 */}
|
||||
<div>
|
||||
<Label className="text-muted-foreground text-xs">商品名稱</Label>
|
||||
<Popover open={openCombobox} onOpenChange={setOpenCombobox}>
|
||||
<PopoverTrigger asChild>
|
||||
<Button
|
||||
variant="outline"
|
||||
role="combobox"
|
||||
aria-expanded={openCombobox}
|
||||
className="w-full justify-between mt-1"
|
||||
>
|
||||
{selectedProduct ? selectedProduct.name : "選擇商品..."}
|
||||
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="w-[500px] p-0">
|
||||
<Command>
|
||||
<CommandInput placeholder="搜尋商品..." />
|
||||
<CommandList>
|
||||
<CommandEmpty>找不到商品</CommandEmpty>
|
||||
<CommandGroup>
|
||||
{availableProducts.map((product) => (
|
||||
<CommandItem
|
||||
key={product.id}
|
||||
value={product.name}
|
||||
onSelect={() => {
|
||||
setSelectedProductId(product.id);
|
||||
setOpenCombobox(false);
|
||||
}}
|
||||
>
|
||||
<Check
|
||||
className={cn(
|
||||
"mr-2 h-4 w-4",
|
||||
selectedProductId === product.id ? "opacity-100" : "opacity-0"
|
||||
)}
|
||||
/>
|
||||
<div className="flex items-center justify-between flex-1">
|
||||
<span>{product.name}</span>
|
||||
<span className="text-xs text-muted-foreground ml-2">
|
||||
({product.unit})
|
||||
</span>
|
||||
</div>
|
||||
</CommandItem>
|
||||
))}
|
||||
</CommandGroup>
|
||||
</CommandList>
|
||||
</Command>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
</div>
|
||||
|
||||
{/* 單位(自動帶入) */}
|
||||
{selectedProduct && (
|
||||
<div>
|
||||
<Label className="text-muted-foreground text-xs">單位</Label>
|
||||
<Input
|
||||
value={selectedProduct.unit}
|
||||
disabled
|
||||
className="mt-1 bg-muted"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 上次採購價格 */}
|
||||
<div>
|
||||
<Label className="text-muted-foreground text-xs">上次採購單價(選填)</Label>
|
||||
<Input
|
||||
type="number"
|
||||
placeholder="輸入價格"
|
||||
value={lastPrice}
|
||||
onChange={(e) => setLastPrice(e.target.value)}
|
||||
className="mt-1"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={handleCancel}
|
||||
className="gap-2 button-outlined-primary"
|
||||
>
|
||||
取消
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={handleAdd}
|
||||
disabled={!selectedProductId}
|
||||
className="gap-2 button-filled-primary"
|
||||
>
|
||||
新增
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
* 編輯供貨商品對話框
|
||||
*/
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { Button } from "../ui/button";
|
||||
import { Input } from "../ui/input";
|
||||
import { Label } from "../ui/label";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
} from "../ui/dialog";
|
||||
import type { SupplyProduct } from "../../types/vendor";
|
||||
|
||||
interface EditSupplyProductDialogProps {
|
||||
open: boolean;
|
||||
product: SupplyProduct | null;
|
||||
onClose: () => void;
|
||||
onSave: (productId: string, lastPrice?: number) => void;
|
||||
}
|
||||
|
||||
export default function EditSupplyProductDialog({
|
||||
open,
|
||||
product,
|
||||
onClose,
|
||||
onSave,
|
||||
}: EditSupplyProductDialogProps) {
|
||||
const [lastPrice, setLastPrice] = useState<string>("");
|
||||
|
||||
useEffect(() => {
|
||||
if (product) {
|
||||
setLastPrice(product.lastPrice?.toString() || "");
|
||||
}
|
||||
}, [product, open]);
|
||||
|
||||
const handleSave = () => {
|
||||
if (!product) return;
|
||||
|
||||
const price = lastPrice ? parseFloat(lastPrice) : undefined;
|
||||
onSave(product.productId, price);
|
||||
setLastPrice("");
|
||||
};
|
||||
|
||||
const handleCancel = () => {
|
||||
setLastPrice("");
|
||||
onClose();
|
||||
};
|
||||
|
||||
if (!product) return null;
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onClose}>
|
||||
<DialogContent className="max-w-xl">
|
||||
<DialogHeader>
|
||||
<DialogTitle>編輯供貨商品</DialogTitle>
|
||||
<DialogDescription>修改商品的採購價格資訊。</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-4">
|
||||
{/* 商品名稱(不可編輯) */}
|
||||
<div>
|
||||
<Label className="text-muted-foreground text-xs">商品名稱</Label>
|
||||
<Input
|
||||
value={product.productName}
|
||||
disabled
|
||||
className="mt-1 bg-muted"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 單位(不可編輯) */}
|
||||
<div>
|
||||
<Label className="text-muted-foreground text-xs">單位</Label>
|
||||
<Input
|
||||
value={product.unit}
|
||||
disabled
|
||||
className="mt-1 bg-muted"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 上次採購價格 */}
|
||||
<div>
|
||||
<Label className="text-muted-foreground text-xs">上次採購單價(選填)</Label>
|
||||
<Input
|
||||
type="number"
|
||||
placeholder="輸入價格"
|
||||
value={lastPrice}
|
||||
onChange={(e) => setLastPrice(e.target.value)}
|
||||
className="mt-1"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={handleCancel}
|
||||
className="gap-2 button-outlined-primary"
|
||||
>
|
||||
取消
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={handleSave}
|
||||
className="gap-2 button-filled-primary"
|
||||
>
|
||||
儲存
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* 供貨商品列表元件
|
||||
*/
|
||||
|
||||
import { Pencil, XCircle } from "lucide-react";
|
||||
import { Button } from "../ui/button";
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from "../ui/table";
|
||||
import type { SupplyProduct } from "../../types/vendor";
|
||||
|
||||
interface SupplyProductListProps {
|
||||
products: SupplyProduct[];
|
||||
onEdit: (product: SupplyProduct) => void;
|
||||
onRemove: (product: SupplyProduct) => void;
|
||||
}
|
||||
|
||||
export default function SupplyProductList({
|
||||
products,
|
||||
onEdit,
|
||||
onRemove,
|
||||
}: SupplyProductListProps) {
|
||||
return (
|
||||
<div className="border border-border rounded-lg overflow-hidden">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>商品名稱</TableHead>
|
||||
<TableHead>單位</TableHead>
|
||||
<TableHead className="text-right">上次採購單價</TableHead>
|
||||
<TableHead className="text-right w-[150px]">操作</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{products.length === 0 ? (
|
||||
<TableRow>
|
||||
<TableCell colSpan={4} className="text-center text-muted-foreground py-8">
|
||||
尚無供貨商品,請點擊上方按鈕新增
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
) : (
|
||||
products.map((product) => (
|
||||
<TableRow key={product.id}>
|
||||
<TableCell>{product.productName}</TableCell>
|
||||
<TableCell>{product.unit}</TableCell>
|
||||
<TableCell className="text-right">
|
||||
{product.lastPrice ? `$${product.lastPrice.toLocaleString()}` : "-"}
|
||||
</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<div className="flex items-center justify-end gap-2">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => onEdit(product)}
|
||||
className="gap-1 button-outlined-primary"
|
||||
>
|
||||
<Pencil className="h-3 w-3" />
|
||||
編輯
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => onRemove(product)}
|
||||
className="gap-1 button-filled-error"
|
||||
>
|
||||
<XCircle className="h-3 w-3" />
|
||||
取消供貨
|
||||
</Button>
|
||||
</div>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))
|
||||
)}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* 廠商刪除確認對話框
|
||||
*/
|
||||
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from "../ui/alert-dialog";
|
||||
import type { Supplier } from "../../types/vendor";
|
||||
|
||||
interface VendorDeleteDialogProps {
|
||||
open: boolean;
|
||||
supplier: Supplier | null;
|
||||
onConfirm: () => void;
|
||||
onCancel: () => void;
|
||||
}
|
||||
|
||||
export default function VendorDeleteDialog({
|
||||
open,
|
||||
supplier,
|
||||
onConfirm,
|
||||
onCancel,
|
||||
}: VendorDeleteDialogProps) {
|
||||
return (
|
||||
<AlertDialog open={open} onOpenChange={onCancel}>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>確認刪除廠商</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
確定要刪除 {supplier?.name} 廠商嗎?此操作無法撤銷。
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel
|
||||
size="sm"
|
||||
className="gap-2 button-outlined-primary"
|
||||
>
|
||||
取消
|
||||
</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
size="sm"
|
||||
className="gap-2 button-filled-error"
|
||||
onClick={onConfirm}
|
||||
>
|
||||
刪除
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
);
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,191 @@
|
||||
/**
|
||||
* 廠商詳細資訊頁面
|
||||
*/
|
||||
|
||||
import { useState } from "react";
|
||||
import { Phone, Mail, Plus, ArrowLeft } from "lucide-react";
|
||||
import { Label } from "../ui/label";
|
||||
import { Button } from "../ui/button";
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from "../ui/alert-dialog";
|
||||
import SupplyProductList from "./SupplyProductList";
|
||||
import AddSupplyProductDialog from "./AddSupplyProductDialog";
|
||||
import EditSupplyProductDialog from "./EditSupplyProductDialog";
|
||||
import type { Supplier, SupplyProduct } from "../../types/vendor";
|
||||
import type { Product } from "../../types/product";
|
||||
|
||||
interface VendorDetailProps {
|
||||
supplier: Supplier;
|
||||
products: Product[];
|
||||
onBack: () => void;
|
||||
onAddSupplyProduct: (supplierId: string, productId: string, lastPrice?: number) => void;
|
||||
onUpdateSupplyProduct: (supplierId: string, productId: string, lastPrice?: number) => void;
|
||||
onRemoveSupplyProduct: (supplierId: string, productId: string) => void;
|
||||
}
|
||||
|
||||
export default function VendorDetail({
|
||||
supplier,
|
||||
products,
|
||||
onBack,
|
||||
onAddSupplyProduct,
|
||||
onUpdateSupplyProduct,
|
||||
onRemoveSupplyProduct,
|
||||
}: VendorDetailProps) {
|
||||
const [showAddDialog, setShowAddDialog] = useState(false);
|
||||
const [showEditDialog, setShowEditDialog] = useState(false);
|
||||
const [showRemoveDialog, setShowRemoveDialog] = useState(false);
|
||||
const [selectedProduct, setSelectedProduct] = useState<SupplyProduct | null>(null);
|
||||
|
||||
const handleAddProduct = (productId: string, lastPrice?: number) => {
|
||||
onAddSupplyProduct(supplier.id, productId, lastPrice);
|
||||
setShowAddDialog(false);
|
||||
};
|
||||
|
||||
const handleEditProduct = (product: SupplyProduct) => {
|
||||
setSelectedProduct(product);
|
||||
setShowEditDialog(true);
|
||||
};
|
||||
|
||||
const handleUpdateProduct = (productId: string, lastPrice?: number) => {
|
||||
onUpdateSupplyProduct(supplier.id, productId, lastPrice);
|
||||
setShowEditDialog(false);
|
||||
setSelectedProduct(null);
|
||||
};
|
||||
|
||||
const handleRemoveProduct = (product: SupplyProduct) => {
|
||||
setSelectedProduct(product);
|
||||
setShowRemoveDialog(true);
|
||||
};
|
||||
|
||||
const handleConfirmRemove = () => {
|
||||
if (selectedProduct) {
|
||||
onRemoveSupplyProduct(supplier.id, selectedProduct.productId);
|
||||
}
|
||||
setShowRemoveDialog(false);
|
||||
setSelectedProduct(null);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="p-6">
|
||||
{/* 返回按鈕 */}
|
||||
<div className="mb-6">
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={onBack}
|
||||
className="gap-2 button-outlined-primary mb-6"
|
||||
>
|
||||
<ArrowLeft className="h-4 w-4" />
|
||||
返回廠商列表
|
||||
</Button>
|
||||
<h1>廠商詳細資訊</h1>
|
||||
<p className="body-sm text-muted-foreground mt-2">
|
||||
查看並管理供應商的詳細資料與供貨商品
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* 基本資料 */}
|
||||
<div className="bg-card rounded-lg border border-border p-6 mb-6">
|
||||
<h3 className="mb-4">基本資料</h3>
|
||||
<div className="grid grid-cols-2 gap-6">
|
||||
<div>
|
||||
<Label className="text-muted-foreground text-xs">廠商名稱</Label>
|
||||
<p className="mt-1">{supplier.name}</p>
|
||||
</div>
|
||||
<div>
|
||||
<Label className="text-muted-foreground text-xs">聯絡人</Label>
|
||||
<p className="mt-1">{supplier.contact || "-"}</p>
|
||||
</div>
|
||||
<div>
|
||||
<Label className="text-muted-foreground text-xs">聯絡電話</Label>
|
||||
<p className="mt-1 flex items-center gap-2">
|
||||
<Phone className="h-3 w-3 text-muted-foreground" />
|
||||
{supplier.phone || "-"}
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
<Label className="text-muted-foreground text-xs">Email</Label>
|
||||
<p className="mt-1 flex items-center gap-2">
|
||||
<Mail className="h-3 w-3 text-muted-foreground" />
|
||||
{supplier.email || "-"}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 供貨商品列表 */}
|
||||
<div className="bg-card rounded-lg border border-border p-6">
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<h3>供貨商品</h3>
|
||||
<Button
|
||||
onClick={() => setShowAddDialog(true)}
|
||||
className="gap-2 button-filled-primary"
|
||||
size="sm"
|
||||
>
|
||||
<Plus className="h-4 w-4" />
|
||||
新增供貨商品
|
||||
</Button>
|
||||
</div>
|
||||
<SupplyProductList
|
||||
products={supplier.supplyProducts}
|
||||
onEdit={handleEditProduct}
|
||||
onRemove={handleRemoveProduct}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 新增供貨商品對話框 */}
|
||||
<AddSupplyProductDialog
|
||||
open={showAddDialog}
|
||||
products={products}
|
||||
existingSupplyProducts={supplier.supplyProducts}
|
||||
onClose={() => setShowAddDialog(false)}
|
||||
onAdd={handleAddProduct}
|
||||
/>
|
||||
|
||||
{/* 編輯供貨商品對話框 */}
|
||||
<EditSupplyProductDialog
|
||||
open={showEditDialog}
|
||||
product={selectedProduct}
|
||||
onClose={() => {
|
||||
setShowEditDialog(false);
|
||||
setSelectedProduct(null);
|
||||
}}
|
||||
onSave={handleUpdateProduct}
|
||||
/>
|
||||
|
||||
{/* 取消供貨確認對話框 */}
|
||||
<AlertDialog open={showRemoveDialog} onOpenChange={setShowRemoveDialog}>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>確認取消供貨</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
確定要將「{selectedProduct?.productName}」從供貨列表中移除嗎?此操作無法撤銷。
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel
|
||||
size="sm"
|
||||
className="gap-2 button-outlined-primary"
|
||||
>
|
||||
取消
|
||||
</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
size="sm"
|
||||
className="gap-2 button-filled-error"
|
||||
onClick={handleConfirmRemove}
|
||||
>
|
||||
確認移除
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,140 @@
|
||||
/**
|
||||
* 廠商新增/編輯表單對話框
|
||||
*/
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { Button } from "../ui/button";
|
||||
import { Input } from "../ui/input";
|
||||
import { Label } from "../ui/label";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
} from "../ui/dialog";
|
||||
import type { Supplier, SupplierFormData } from "../../types/vendor";
|
||||
|
||||
interface VendorFormDialogProps {
|
||||
open: boolean;
|
||||
supplier: Supplier | null;
|
||||
onClose: () => void;
|
||||
onSave: (data: SupplierFormData) => void;
|
||||
}
|
||||
|
||||
const initialFormData: SupplierFormData = {
|
||||
name: "",
|
||||
contact: "",
|
||||
phone: "",
|
||||
email: "",
|
||||
};
|
||||
|
||||
export default function VendorFormDialog({
|
||||
open,
|
||||
supplier,
|
||||
onClose,
|
||||
onSave,
|
||||
}: VendorFormDialogProps) {
|
||||
const [formData, setFormData] = useState<SupplierFormData>(initialFormData);
|
||||
const isEditing = !!supplier;
|
||||
|
||||
useEffect(() => {
|
||||
if (supplier) {
|
||||
setFormData({
|
||||
name: supplier.name,
|
||||
contact: supplier.contact || "",
|
||||
phone: supplier.phone || "",
|
||||
email: supplier.email || "",
|
||||
});
|
||||
} else {
|
||||
setFormData(initialFormData);
|
||||
}
|
||||
}, [supplier, open]);
|
||||
|
||||
const handleSave = () => {
|
||||
onSave(formData);
|
||||
setFormData(initialFormData);
|
||||
};
|
||||
|
||||
const handleCancel = () => {
|
||||
onClose();
|
||||
setFormData(initialFormData);
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onClose}>
|
||||
<DialogContent className="max-w-2xl">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{isEditing ? "編輯廠商" : "新增廠商"}</DialogTitle>
|
||||
<DialogDescription>
|
||||
{isEditing ? "編輯供應商的詳細資料。" : "新增供應商的詳細資料。"}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-6">
|
||||
<div>
|
||||
<h3 className="mb-4">基本資料</h3>
|
||||
<div className="space-y-4">
|
||||
<div>
|
||||
<Label className="text-muted-foreground text-xs">廠商名稱</Label>
|
||||
<Input
|
||||
placeholder="輸入廠商名稱"
|
||||
value={formData.name}
|
||||
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
|
||||
className="mt-1"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Label className="text-muted-foreground text-xs">聯絡人</Label>
|
||||
<Input
|
||||
placeholder="輸入聯絡人姓名"
|
||||
value={formData.contact}
|
||||
onChange={(e) => setFormData({ ...formData, contact: e.target.value })}
|
||||
className="mt-1"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Label className="text-muted-foreground text-xs">聯絡電話</Label>
|
||||
<Input
|
||||
placeholder="例:02-1234-5678"
|
||||
value={formData.phone}
|
||||
onChange={(e) => setFormData({ ...formData, phone: e.target.value })}
|
||||
className="mt-1"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Label className="text-muted-foreground text-xs">Email</Label>
|
||||
<Input
|
||||
type="email"
|
||||
placeholder="例:vendor@example.com"
|
||||
value={formData.email}
|
||||
onChange={(e) => setFormData({ ...formData, email: e.target.value })}
|
||||
className="mt-1"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={handleCancel}
|
||||
className="gap-2 button-outlined-primary"
|
||||
>
|
||||
取消
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={handleSave}
|
||||
className="gap-2 button-filled-primary"
|
||||
>
|
||||
儲存
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,130 @@
|
||||
/**
|
||||
* 廠商列表顯示元件
|
||||
*/
|
||||
|
||||
import { Phone, Mail, Package, Pencil, Trash2, Calendar, Eye } from "lucide-react";
|
||||
import { Button } from "../ui/button";
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from "../ui/table";
|
||||
import type { Supplier } from "../../types/vendor";
|
||||
|
||||
interface VendorListProps {
|
||||
suppliers: Supplier[];
|
||||
searchQuery: string;
|
||||
onViewDetails: (supplier: Supplier) => void;
|
||||
onEdit: (supplier: Supplier) => void;
|
||||
onDelete: (supplier: Supplier) => void;
|
||||
}
|
||||
|
||||
export default function VendorList({
|
||||
suppliers,
|
||||
searchQuery,
|
||||
onViewDetails,
|
||||
onEdit,
|
||||
onDelete,
|
||||
}: VendorListProps) {
|
||||
const isEmpty = suppliers.length === 0;
|
||||
const emptyMessage = searchQuery ? "未找到符合條件的廠商" : "尚無廠商資料";
|
||||
|
||||
const formatDate = (dateString?: string) => {
|
||||
if (!dateString) return "-";
|
||||
const date = new Date(dateString);
|
||||
return date.toLocaleDateString("zh-TW", {
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="bg-card rounded-lg border border-border overflow-hidden">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead className="w-[200px]">廠商名稱</TableHead>
|
||||
<TableHead className="w-[120px]">聯絡人</TableHead>
|
||||
<TableHead className="w-[140px]">聯絡電話</TableHead>
|
||||
<TableHead className="w-[180px]">Email</TableHead>
|
||||
<TableHead className="w-[130px]">上次採購日</TableHead>
|
||||
<TableHead className="w-[220px] text-right">操作</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{isEmpty ? (
|
||||
<TableRow>
|
||||
<TableCell colSpan={6} className="text-center text-muted-foreground py-8">
|
||||
{emptyMessage}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
) : (
|
||||
suppliers.map((supplier) => (
|
||||
<TableRow key={supplier.id}>
|
||||
<TableCell>
|
||||
<div className="flex items-center gap-2">
|
||||
<Package className="h-4 w-4 text-muted-foreground" />
|
||||
<span>{supplier.name}</span>
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell>{supplier.contact || "-"}</TableCell>
|
||||
<TableCell>
|
||||
<div className="flex items-center gap-2 text-sm">
|
||||
<Phone className="h-3 w-3 text-muted-foreground" />
|
||||
{supplier.phone || "-"}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<div className="flex items-center gap-2 text-sm">
|
||||
<Mail className="h-3 w-3 text-muted-foreground" />
|
||||
{supplier.email || "-"}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<div className="flex items-center gap-2 text-sm">
|
||||
<Calendar className="h-3 w-3 text-muted-foreground" />
|
||||
{formatDate(supplier.lastPurchaseDate)}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<div className="flex items-center justify-end gap-2">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => onViewDetails(supplier)}
|
||||
className="gap-1 button-outlined-primary"
|
||||
>
|
||||
<Eye className="h-3 w-3" />
|
||||
查看
|
||||
</Button>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => onEdit(supplier)}
|
||||
className="gap-1 button-outlined-primary"
|
||||
>
|
||||
<Pencil className="h-3 w-3" />
|
||||
編輯
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => onDelete(supplier)}
|
||||
className="gap-1 button-filled-error"
|
||||
>
|
||||
<Trash2 className="h-3 w-3" />
|
||||
刪除
|
||||
</Button>
|
||||
</div>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))
|
||||
)}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,134 @@
|
||||
/**
|
||||
* 廠商管理主元件
|
||||
* 整合所有子元件,處理狀態管理和事件協調
|
||||
*/
|
||||
|
||||
import { useState, useMemo } from "react";
|
||||
import VendorSearchBar from "./VendorSearchBar";
|
||||
import VendorList from "./VendorList";
|
||||
import VendorFormDialog from "./VendorFormDialog";
|
||||
import VendorDeleteDialog from "./VendorDeleteDialog";
|
||||
import { filterSuppliers } from "../../utils/search";
|
||||
import type { Supplier, SupplierFormData } from "../../types/vendor";
|
||||
import type { Product } from "../../types/product";
|
||||
|
||||
interface VendorManagementProps {
|
||||
suppliers: Supplier[];
|
||||
products: Product[];
|
||||
onAddSupplier: (supplier: Omit<Supplier, "id">) => void;
|
||||
onUpdateSupplier: (id: string, supplier: Omit<Supplier, "id">) => void;
|
||||
onDeleteSupplier: (id: string) => void;
|
||||
onViewDetail: (supplierId: string) => void;
|
||||
}
|
||||
|
||||
export default function VendorManagement({
|
||||
suppliers,
|
||||
products,
|
||||
onAddSupplier,
|
||||
onUpdateSupplier,
|
||||
onDeleteSupplier,
|
||||
onViewDetail,
|
||||
}: VendorManagementProps) {
|
||||
const [searchQuery, setSearchQuery] = useState("");
|
||||
const [editingSupplier, setEditingSupplier] = useState<Supplier | null>(null);
|
||||
const [deletingSupplier, setDeletingSupplier] = useState<Supplier | null>(null);
|
||||
|
||||
// 對話框顯示狀態
|
||||
const [showFormDialog, setShowFormDialog] = useState(false);
|
||||
const [showDeleteDialog, setShowDeleteDialog] = useState(false);
|
||||
|
||||
// 過濾廠商列表
|
||||
const filteredSuppliers = useMemo(
|
||||
() => filterSuppliers(suppliers, searchQuery),
|
||||
[suppliers, searchQuery]
|
||||
);
|
||||
|
||||
// 處理新增廠商
|
||||
const handleAddClick = () => {
|
||||
setEditingSupplier(null);
|
||||
setShowFormDialog(true);
|
||||
};
|
||||
|
||||
// 處理查看詳情
|
||||
const handleViewDetails = (supplier: Supplier) => {
|
||||
onViewDetail(supplier.id);
|
||||
};
|
||||
|
||||
// 處理編輯廠商
|
||||
const handleEdit = (supplier: Supplier) => {
|
||||
setEditingSupplier(supplier);
|
||||
setShowFormDialog(true);
|
||||
};
|
||||
|
||||
// 處理刪除廠商
|
||||
const handleDelete = (supplier: Supplier) => {
|
||||
setDeletingSupplier(supplier);
|
||||
setShowDeleteDialog(true);
|
||||
};
|
||||
|
||||
// 處理表單儲存
|
||||
const handleFormSave = (data: SupplierFormData) => {
|
||||
if (editingSupplier) {
|
||||
onUpdateSupplier(editingSupplier.id, data);
|
||||
} else {
|
||||
onAddSupplier(data);
|
||||
}
|
||||
setShowFormDialog(false);
|
||||
setEditingSupplier(null);
|
||||
};
|
||||
|
||||
// 處理確認刪除
|
||||
const handleConfirmDelete = () => {
|
||||
if (deletingSupplier) {
|
||||
onDeleteSupplier(deletingSupplier.id);
|
||||
}
|
||||
setShowDeleteDialog(false);
|
||||
setDeletingSupplier(null);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="p-6">
|
||||
{/* 頁面標題 */}
|
||||
<div className="mb-6">
|
||||
<h1>廠商管理</h1>
|
||||
<p className="body-sm text-muted-foreground mt-2">
|
||||
供應商資料總覽與管理
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* 搜尋列與新增按鈕 */}
|
||||
<div className="mb-6">
|
||||
<VendorSearchBar
|
||||
searchQuery={searchQuery}
|
||||
onSearchChange={setSearchQuery}
|
||||
onAddClick={handleAddClick}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 廠商列表 */}
|
||||
<VendorList
|
||||
suppliers={filteredSuppliers}
|
||||
searchQuery={searchQuery}
|
||||
onViewDetails={handleViewDetails}
|
||||
onEdit={handleEdit}
|
||||
onDelete={handleDelete}
|
||||
/>
|
||||
|
||||
{/* 新增/編輯對話框 */}
|
||||
<VendorFormDialog
|
||||
open={showFormDialog}
|
||||
supplier={editingSupplier}
|
||||
onClose={() => setShowFormDialog(false)}
|
||||
onSave={handleFormSave}
|
||||
/>
|
||||
|
||||
{/* 刪除確認對話框 */}
|
||||
<VendorDeleteDialog
|
||||
open={showDeleteDialog}
|
||||
supplier={deletingSupplier}
|
||||
onConfirm={handleConfirmDelete}
|
||||
onCancel={() => setShowDeleteDialog(false)}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* 廠商搜尋列元件
|
||||
*/
|
||||
|
||||
import { Search, Plus } from "lucide-react";
|
||||
import { Input } from "../ui/input";
|
||||
import { Button } from "../ui/button";
|
||||
|
||||
interface VendorSearchBarProps {
|
||||
searchQuery: string;
|
||||
onSearchChange: (query: string) => void;
|
||||
onAddClick: () => void;
|
||||
}
|
||||
|
||||
export default function VendorSearchBar({
|
||||
searchQuery,
|
||||
onSearchChange,
|
||||
onAddClick,
|
||||
}: VendorSearchBarProps) {
|
||||
return (
|
||||
<div className="flex justify-between items-center">
|
||||
<div className="relative flex-1 max-w-md">
|
||||
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 text-muted-foreground h-4 w-4" />
|
||||
<Input
|
||||
placeholder="搜尋廠商名稱 / 聯絡人 / Email"
|
||||
value={searchQuery}
|
||||
onChange={(e) => onSearchChange(e.target.value)}
|
||||
className="pl-9 input-outlined-primary"
|
||||
/>
|
||||
</div>
|
||||
<Button onClick={onAddClick} className="gap-2 button-filled-primary">
|
||||
<Plus className="h-4 w-4" />
|
||||
新增廠商
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user