2025-11-19 14:55:14 +07:00
|
|
|
import { RequestUpdateMenu } from "@/components/menu/request-update-menu";
|
|
|
|
|
import { SelectDialog } from "@/components/dialogs/select-dialog";
|
|
|
|
|
import { DeviceSearchDialog } from "@/components/bars/device-searchbar";
|
2025-10-31 16:52:56 +07:00
|
|
|
import {
|
|
|
|
|
Card,
|
|
|
|
|
CardContent,
|
|
|
|
|
CardDescription,
|
|
|
|
|
CardFooter,
|
|
|
|
|
CardHeader,
|
|
|
|
|
CardTitle,
|
|
|
|
|
} from "@/components/ui/card";
|
2025-11-19 14:55:14 +07:00
|
|
|
import { FormDialog } from "@/components/dialogs/form-dialog";
|
|
|
|
|
import { VersionTable } from "@/components/tables/version-table";
|
2025-10-31 16:52:56 +07:00
|
|
|
import type { ColumnDef } from "@tanstack/react-table";
|
2025-11-19 14:55:14 +07:00
|
|
|
import { FileText, Building2 } from "lucide-react";
|
2025-10-31 16:52:56 +07:00
|
|
|
import { useState } from "react";
|
2025-11-19 14:55:14 +07:00
|
|
|
import { BlacklistForm } from "@/components/forms/black-list-form";
|
|
|
|
|
import type { BlacklistFormData } from "@/types/black-list";
|
|
|
|
|
import type { Room } from "@/types/room";
|
|
|
|
|
import { mapRoomsToSelectItems } from "@/helpers/mapRoomToSelectItems";
|
|
|
|
|
import { fetchDevicesFromRoom } from "@/services/device.service";
|
2025-10-31 16:52:56 +07:00
|
|
|
|
|
|
|
|
interface BlackListManagerTemplateProps<TData> {
|
|
|
|
|
title: string;
|
|
|
|
|
description: string;
|
|
|
|
|
data: TData[];
|
|
|
|
|
isLoading: boolean;
|
|
|
|
|
columns: ColumnDef<TData, any>[];
|
2025-11-19 14:55:14 +07:00
|
|
|
onAdd: (data: BlacklistFormData) => Promise<void>;
|
|
|
|
|
onDelete?: (id: number) => Promise<void>;
|
|
|
|
|
onUpdate?: (target: string | string[]) => void | Promise<void>;
|
2025-10-31 16:52:56 +07:00
|
|
|
updateLoading?: boolean;
|
|
|
|
|
onTableInit?: (table: any) => void;
|
2025-11-19 14:55:14 +07:00
|
|
|
rooms: Room[];
|
2025-10-31 16:52:56 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function BlackListManagerTemplate<TData>({
|
|
|
|
|
title,
|
|
|
|
|
description,
|
|
|
|
|
data,
|
|
|
|
|
isLoading,
|
|
|
|
|
columns,
|
2025-11-19 14:55:14 +07:00
|
|
|
onAdd,
|
2025-10-31 16:52:56 +07:00
|
|
|
onUpdate,
|
|
|
|
|
updateLoading,
|
|
|
|
|
onTableInit,
|
|
|
|
|
rooms = [],
|
|
|
|
|
}: BlackListManagerTemplateProps<TData>) {
|
|
|
|
|
const [dialogOpen, setDialogOpen] = useState(false);
|
|
|
|
|
const [dialogType, setDialogType] = useState<"room" | "device" | null>(null);
|
|
|
|
|
|
2025-11-19 14:55:14 +07:00
|
|
|
const handleUpdateAll = async () => {
|
|
|
|
|
if (onUpdate) await onUpdate("All");
|
2025-10-31 16:52:56 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const openRoomDialog = () => {
|
|
|
|
|
if (rooms.length > 0 && onUpdate) {
|
|
|
|
|
setDialogType("room");
|
|
|
|
|
setDialogOpen(true);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const openDeviceDialog = () => {
|
2025-11-19 14:55:14 +07:00
|
|
|
if (onUpdate) {
|
2025-10-31 16:52:56 +07:00
|
|
|
setDialogType("device");
|
|
|
|
|
setDialogOpen(true);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="w-full px-6 space-y-4">
|
|
|
|
|
{/* Header */}
|
|
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
<div>
|
|
|
|
|
<h1 className="text-3xl font-bold">{title}</h1>
|
|
|
|
|
<p className="text-muted-foreground mt-2">{description}</p>
|
|
|
|
|
</div>
|
2025-11-19 14:55:14 +07:00
|
|
|
<FormDialog
|
|
|
|
|
triggerLabel="Thêm phần mềm bị chặn"
|
|
|
|
|
title="Thêm phần mềm bị chặn"
|
|
|
|
|
>
|
|
|
|
|
{(closeDialog) => (
|
|
|
|
|
<BlacklistForm onSubmit={onAdd} closeDialog={closeDialog} />
|
|
|
|
|
)}
|
|
|
|
|
</FormDialog>
|
2025-10-31 16:52:56 +07:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Table */}
|
|
|
|
|
<Card className="w-full">
|
|
|
|
|
<CardHeader>
|
|
|
|
|
<CardTitle className="flex items-center gap-2">
|
|
|
|
|
<FileText className="h-5 w-5" /> Danh sách phần mềm bị chặn
|
|
|
|
|
</CardTitle>
|
|
|
|
|
<CardDescription>
|
|
|
|
|
Các phần mềm không được cho phép trong hệ thống
|
|
|
|
|
</CardDescription>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
|
|
|
|
|
<CardContent>
|
|
|
|
|
<VersionTable
|
|
|
|
|
data={data}
|
|
|
|
|
isLoading={isLoading}
|
|
|
|
|
columns={columns}
|
|
|
|
|
onTableInit={onTableInit}
|
|
|
|
|
/>
|
|
|
|
|
</CardContent>
|
|
|
|
|
|
|
|
|
|
{/* Footer */}
|
|
|
|
|
{onUpdate && (
|
|
|
|
|
<CardFooter className="flex flex-col sm:flex-row gap-2">
|
|
|
|
|
<RequestUpdateMenu
|
|
|
|
|
onUpdateDevice={openDeviceDialog}
|
|
|
|
|
onUpdateRoom={openRoomDialog}
|
|
|
|
|
onUpdateAll={handleUpdateAll}
|
|
|
|
|
loading={updateLoading}
|
|
|
|
|
/>
|
|
|
|
|
</CardFooter>
|
|
|
|
|
)}
|
|
|
|
|
</Card>
|
|
|
|
|
|
2025-11-19 14:55:14 +07:00
|
|
|
{/* Dialog chọn phòng */}
|
|
|
|
|
{dialogType === "room" && (
|
2025-10-31 16:52:56 +07:00
|
|
|
<SelectDialog
|
|
|
|
|
open={dialogOpen}
|
|
|
|
|
onClose={() => setDialogOpen(false)}
|
2025-11-19 14:55:14 +07:00
|
|
|
title="Chọn phòng"
|
|
|
|
|
description="Chọn các phòng cần cập nhật danh sách đen"
|
|
|
|
|
icon={<Building2 className="w-6 h-6 text-primary" />}
|
|
|
|
|
items={mapRoomsToSelectItems(rooms)}
|
|
|
|
|
onConfirm={async (selectedRooms) => {
|
2025-10-31 16:52:56 +07:00
|
|
|
if (!onUpdate) return;
|
2025-11-19 14:55:14 +07:00
|
|
|
await onUpdate(selectedRooms);
|
2025-10-31 16:52:56 +07:00
|
|
|
setDialogOpen(false);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2025-11-19 14:55:14 +07:00
|
|
|
|
|
|
|
|
{/* Dialog tìm thiết bị */}
|
|
|
|
|
{dialogType === "device" && (
|
|
|
|
|
<DeviceSearchDialog
|
|
|
|
|
open={dialogOpen && dialogType === "device"}
|
|
|
|
|
onClose={() => setDialogOpen(false)}
|
|
|
|
|
rooms={rooms}
|
|
|
|
|
fetchDevices={fetchDevicesFromRoom} // ⬅ thêm vào đây
|
|
|
|
|
onSelect={(deviceIds) => onUpdate && onUpdate(deviceIds)}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2025-10-31 16:52:56 +07:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|