import { useState } from "react"; import { Pencil, Eye, Trash2 } from "lucide-react"; import { Button } from "@/Components/ui/button"; import { Link, useForm, usePage } from "@inertiajs/react"; import type { PurchaseOrder } from "@/types/purchase-order"; import { toast } from "sonner"; import { Can } from "@/Components/Permission/Can"; import { PageProps } from "@/types/global"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/Components/ui/alert-dialog"; export function PurchaseOrderActions({ order, }: { order: PurchaseOrder }) { const [showDeleteDialog, setShowDeleteDialog] = useState(false); const { delete: destroy, processing } = useForm({}); const { auth } = usePage().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); const handleConfirmDelete = () => { // @ts-ignore destroy(route('purchase-orders.destroy', order.id), { onSuccess: () => { toast.success("採購單已成功刪除"); setShowDeleteDialog(false); }, onError: (errors: any) => toast.error(errors.error || "刪除過程中發生錯誤"), }); }; return (
{showEditButton && ( )} {order.status === 'draft' && ( )} 確認刪除採購單 確定要刪除採購單 「{order.poNumber}」 嗎?此操作無法撤銷。 取消 確認刪除
); }