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 chosen=records.filter(x=>selected.has(x.id));
function done(){const ids=chosen.map(x=>x.id);onSelectionChange(new Set());onArchived(ids);}
async function archive(){if(!chosen.length||!window.confirm(`${chosen.length} رکورد به بایگانی منتقل شود؟`))return;setBusy(true);try{for(const x of chosen)await api(`/api/v1/archive/${entityType}/${x.id}/archive`,{method:"POST",body:JSON.stringify({rowVersion:x.rowVersion})});done();}catch(e){window.alert(e instanceof Error?e.message:"بایگانی گروهی ناموفق بود.");}finally{setBusy(false);}}
async function remove(){if(!chosen.length)return;const phrase=`حذف دائمی ${chosen.length} رکورد`;const answer=window.prompt(`رکوردها واقعاً از دیتابیس حذف میشوند و قابل بازگشت نیستند.\nبرای ادامه دقیقاً بنویسید:\n${phrase}`);if(answer!==phrase){if(answer!==null)window.alert("عبارت تأیید صحیح نیست.");return;}if(!window.confirm("تأیید نهایی حذف دائمی؟"))return;setBusy(true);try{for(const x of chosen)await api(`/api/v1/delete/${entityType}/${x.id}`,{method:"DELETE",body:JSON.stringify({rowVersion:x.rowVersion})});done();}catch(e){window.alert(e instanceof Error?e.message:"حذف گروهی ناموفق بود.");}finally{setBusy(false);}}
const all=records.length>0&&records.every(x=>selected.has(x.id));return