2026-01-19 15:32:41 +08:00
|
|
|
|
import { useState } from "react";
|
2025-12-31 17:48:36 +08:00
|
|
|
|
import { Pencil, Eye, Trash2 } from "lucide-react";
|
2025-12-30 15:03:19 +08:00
|
|
|
|
import { Button } from "@/Components/ui/button";
|
2026-02-06 15:38:47 +08:00
|
|
|
|
import { Link, useForm, usePage } from "@inertiajs/react";
|
2025-12-30 15:03:19 +08:00
|
|
|
|
import type { PurchaseOrder } from "@/types/purchase-order";
|
2025-12-30 17:05:19 +08:00
|
|
|
|
import { toast } from "sonner";
|
2026-01-13 17:00:58 +08:00
|
|
|
|
import { Can } from "@/Components/Permission/Can";
|
2026-02-06 15:38:47 +08:00
|
|
|
|
import { PageProps } from "@/types/global";
|
2026-01-19 15:32:41 +08:00
|
|
|
|
import {
|
|
|
|
|
|
AlertDialog,
|
|
|
|
|
|
AlertDialogAction,
|
|
|
|
|
|
AlertDialogCancel,
|
|
|
|
|
|
AlertDialogContent,
|
|
|
|
|
|
AlertDialogDescription,
|
|
|
|
|
|
AlertDialogFooter,
|
|
|
|
|
|
AlertDialogHeader,
|
|
|
|
|
|
AlertDialogTitle,
|
|
|
|
|
|
} from "@/Components/ui/alert-dialog";
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
|
|
|
|
|
export function PurchaseOrderActions({
|
|
|
|
|
|
order,
|
|
|
|
|
|
}: { order: PurchaseOrder }) {
|
2026-01-19 15:32:41 +08:00
|
|
|
|
const [showDeleteDialog, setShowDeleteDialog] = useState(false);
|
2025-12-30 17:05:19 +08:00
|
|
|
|
const { delete: destroy, processing } = useForm({});
|
2026-02-06 15:38:47 +08:00
|
|
|
|
const { auth } = usePage<PageProps>().props;
|
|
|
|
|
|
const permissions = auth.user?.permissions || [];
|
|
|
|
|
|
const isSuperAdmin = auth.user?.roles?.some((r: any) => r.name === 'super-admin');
|
|
|
|
|
|
|
|
|
|
|
|
const canApprove = isSuperAdmin || permissions.includes('purchase_orders.approve');
|
|
|
|
|
|
const canEdit = isSuperAdmin || permissions.includes('purchase_orders.edit');
|
|
|
|
|
|
|
|
|
|
|
|
// 編輯按鈕顯示邏輯:
|
|
|
|
|
|
// 1. 草稿狀態 + 編輯權限
|
|
|
|
|
|
// 2. 待核准狀態 + 核准權限 (讓主管能直接改)
|
|
|
|
|
|
const showEditButton = (order.status === 'draft' && canEdit) ||
|
|
|
|
|
|
(order.status === 'pending' && canApprove);
|
2025-12-30 17:05:19 +08:00
|
|
|
|
|
2026-01-19 15:32:41 +08:00
|
|
|
|
const handleConfirmDelete = () => {
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
destroy(route('purchase-orders.destroy', order.id), {
|
|
|
|
|
|
onSuccess: () => {
|
|
|
|
|
|
toast.success("採購單已成功刪除");
|
|
|
|
|
|
setShowDeleteDialog(false);
|
|
|
|
|
|
},
|
|
|
|
|
|
onError: (errors: any) => toast.error(errors.error || "刪除過程中發生錯誤"),
|
|
|
|
|
|
});
|
2025-12-30 17:05:19 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-12-30 15:03:19 +08:00
|
|
|
|
return (
|
2026-01-13 13:30:51 +08:00
|
|
|
|
<div className="flex justify-center gap-2">
|
2025-12-30 15:03:19 +08:00
|
|
|
|
<Link href={`/purchase-orders/${order.id}`}>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
size="sm"
|
2026-01-13 14:23:45 +08:00
|
|
|
|
className="button-outlined-primary"
|
|
|
|
|
|
title="查看詳情"
|
2025-12-30 15:03:19 +08:00
|
|
|
|
>
|
|
|
|
|
|
<Eye className="h-4 w-4" />
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</Link>
|
2026-02-06 15:38:47 +08:00
|
|
|
|
{showEditButton && (
|
|
|
|
|
|
<Link href={`/purchase-orders/${order.id}/edit`}>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
size="sm"
|
|
|
|
|
|
className="button-outlined-primary"
|
|
|
|
|
|
title="編輯"
|
|
|
|
|
|
>
|
|
|
|
|
|
<Pencil className="h-4 w-4" />
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</Link>
|
|
|
|
|
|
)}
|
2026-02-06 15:32:12 +08:00
|
|
|
|
<Can permission="purchase_orders.delete">
|
|
|
|
|
|
{order.status === 'draft' && (
|
2026-01-13 17:00:58 +08:00
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
size="sm"
|
2026-02-06 15:32:12 +08:00
|
|
|
|
className="button-outlined-error"
|
|
|
|
|
|
title="刪除"
|
|
|
|
|
|
onClick={() => setShowDeleteDialog(true)}
|
|
|
|
|
|
disabled={processing}
|
2026-01-13 17:00:58 +08:00
|
|
|
|
>
|
2026-02-06 15:32:12 +08:00
|
|
|
|
<Trash2 className="h-4 w-4" />
|
2026-01-13 17:00:58 +08:00
|
|
|
|
</Button>
|
2026-02-06 15:32:12 +08:00
|
|
|
|
)}
|
2026-01-19 15:32:41 +08:00
|
|
|
|
|
|
|
|
|
|
<AlertDialog open={showDeleteDialog} onOpenChange={setShowDeleteDialog}>
|
|
|
|
|
|
<AlertDialogContent>
|
|
|
|
|
|
<AlertDialogHeader>
|
|
|
|
|
|
<AlertDialogTitle>確認刪除採購單</AlertDialogTitle>
|
|
|
|
|
|
<AlertDialogDescription>
|
|
|
|
|
|
確定要刪除採購單 「{order.poNumber}」 嗎?此操作無法撤銷。
|
|
|
|
|
|
</AlertDialogDescription>
|
|
|
|
|
|
</AlertDialogHeader>
|
|
|
|
|
|
<AlertDialogFooter>
|
|
|
|
|
|
<AlertDialogCancel className="button-outlined-primary">取消</AlertDialogCancel>
|
|
|
|
|
|
<AlertDialogAction
|
|
|
|
|
|
onClick={handleConfirmDelete}
|
|
|
|
|
|
className="button-filled-error"
|
|
|
|
|
|
>
|
|
|
|
|
|
確認刪除
|
|
|
|
|
|
</AlertDialogAction>
|
|
|
|
|
|
</AlertDialogFooter>
|
|
|
|
|
|
</AlertDialogContent>
|
|
|
|
|
|
</AlertDialog>
|
2026-01-13 17:00:58 +08:00
|
|
|
|
</Can>
|
2025-12-30 15:03:19 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|