import { Archive, Trash2 } from "lucide-react"; import { useState } from "react"; import { api } from "../lib/api"; export type BulkArchiveRecord = { id: string; rowVersion: string }; export function SelectionCheckbox({ checked, onChange, label }: { checked: boolean; onChange: (checked: boolean) => void; label: string; }) { return onChange(e.target.checked)} aria-label={label}/>; } export function BulkArchiveActions({ entityType, records, selected, onSelectionChange, onArchived }: { entityType: string; records: BulkArchiveRecord[]; selected: Set; onSelectionChange: (next: Set) => void; onArchived: (ids: string[]) => void; }) { const [busy, setBusy] = useState(false); const [result, setResult] = useState<{ success: number; failures: string[] } | null>(null); const chosen = records.filter(x => selected.has(x.id)); async function run(action: "archive" | "delete") { if (!chosen.length) return; if (action === "archive") { if (!window.confirm(`${chosen.length} رکورد به بایگانی منتقل شود؟`)) return; } else { const phrase = `حذف دائمی ${chosen.length} رکورد`; if (window.prompt(`رکوردها واقعاً از دیتابیس حذف میشوند و قابل بازگشت نیستند.\nبرای ادامه دقیقاً بنویسید:\n${phrase}`) !== phrase) return; if (!window.confirm("تأیید نهایی حذف دائمی؟")) return; } setBusy(true); setResult(null); const succeeded: string[] = []; const failures: string[] = []; try { for (const record of chosen) { try { const path = action === "archive" ? `/api/v1/archive/${entityType}/${record.id}/archive` : `/api/v1/delete/${entityType}/${record.id}`; await api(path, { method: action === "archive" ? "POST" : "DELETE", body: JSON.stringify({ rowVersion: record.rowVersion }) }); succeeded.push(record.id); } catch (reason) { failures.push(`${record.id}: ${reason instanceof Error ? reason.message : "عملیات ناموفق بود."}`); } } } finally { if (succeeded.length) { onSelectionChange(new Set([...selected].filter(id => !succeeded.includes(id)))); onArchived(succeeded); } setBusy(false); } setResult({ success: succeeded.length, failures }); } const all = records.length > 0 && records.every(x => selected.has(x.id)); return {result && {result.success} مورد انجام شد؛ {result.failures.length} مورد ناموفق بود. {result.failures.length > 0 && {result.failures.map((failure, index) => {failure})}} } onSelectionChange(on ? new Set(records.map(x => x.id)) : new Set())} label="انتخاب همه رکوردهای صفحه"/>انتخاب همه صفحه {chosen.length} رکورد انتخاب شده void run("archive")}>بایگانی گروهی void run("delete")}>حذف دائمی گروهی ; }