import { type ColumnDef } from "@tanstack/react-table"; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "@/components/ui/card"; import { FileText, Building2 } from "lucide-react"; import { FormDialog } from "@/components/dialogs/form-dialog"; import { VersionTable } from "@/components/tables/version-table"; import { RequestUpdateMenu } from "@/components/menu/request-update-menu"; import type { AxiosProgressEvent } from "axios"; import { useState } from "react"; import { SelectDialog } from "@/components/dialogs/select-dialog"; import { DeviceSearchDialog } from "@/components/bars/device-searchbar"; import { UploadVersionForm } from "@/components/forms/upload-file-form"; import type { Room } from "@/types/room"; import { mapRoomsToSelectItems } from "@/helpers/mapRoomToSelectItems"; import { fetchDevicesFromRoom } from "@/services/device.service"; interface AppManagerTemplateProps { title: string; description: string; data: TData[]; isLoading: boolean; columns: ColumnDef[]; onUpload: ( fd: FormData, config?: { onUploadProgress?: (e: AxiosProgressEvent) => void } ) => Promise; onUpdate?: (targetNames: string[]) => Promise | void; updateLoading?: boolean; onDownload?: (targetNames: string[]) => Promise | void; downloadLoading?: boolean; onTableInit?: (table: any) => void; rooms?: Room[]; devices?: string[]; } export function AppManagerTemplate({ title, description, data, isLoading, columns, onUpload, onUpdate, updateLoading, onDownload, downloadLoading, onTableInit, rooms = [], devices = [], }: AppManagerTemplateProps) { const [dialogOpen, setDialogOpen] = useState(false); const [dialogType, setDialogType] = useState<"room" | "device" | "download-room" | "download-device" | null>(null); const openRoomDialog = () => { if (rooms.length > 0 && onUpdate) { setDialogType("room"); setDialogOpen(true); } }; const openDeviceDialog = () => { if (onUpdate) { setDialogType("device"); setDialogOpen(true); } }; const openDownloadRoomDialog = () => { if (rooms.length > 0 && onDownload) { setDialogType("download-room"); setDialogOpen(true); } }; const openDownloadDeviceDialog = () => { if (onDownload) { setDialogType("download-device"); setDialogOpen(true); } }; const handleUpdateAll = async () => { if (!onUpdate) return; try { const roomIds = rooms.map((room) => typeof room === "string" ? room : room.name ); const allTargets = [...roomIds, ...devices]; await onUpdate(allTargets); } catch (e) { console.error("Update error:", e); } }; return (
{/* Header */}

{title}

{description}

{(closeDialog) => ( )}
Lịch sử phiên bản Tất cả các phiên bản đã tải lên {onUpdate && ( {onDownload && ( { if (!onDownload) return; const roomIds = rooms.map((room) => typeof room === "string" ? room : room.name ); const allTargets = [...roomIds, ...devices]; onDownload(allTargets); }} loading={downloadLoading} /> )} )} {/* Dialog chọn phòng */} {dialogType === "room" && ( { setDialogOpen(false); setDialogType(null); }} title="Chọn phòng" description="Chọn các phòng cần cập nhật" icon={} items={mapRoomsToSelectItems(rooms)} onConfirm={async (selectedItems) => { if (!onUpdate) return; try { await onUpdate(selectedItems); } catch (e) { console.error("Update error:", e); } finally { setDialogOpen(false); setDialogType(null); setTimeout(() => window.location.reload(), 500); } }} /> )} {/* Dialog tìm thiết bị */} {dialogType === "device" && ( { setDialogOpen(false); setDialogType(null); }} rooms={rooms} fetchDevices={fetchDevicesFromRoom} onSelect={async (deviceIds) => { if (!onUpdate) { setDialogOpen(false); setDialogType(null); return; } try { await onUpdate(deviceIds); } catch (e) { console.error("Update error:", e); } finally { setDialogOpen(false); setDialogType(null); setTimeout(() => window.location.reload(), 500); } }} /> )} {/* Dialog tải file - chọn phòng */} {dialogType === "download-room" && ( { setDialogOpen(false); setDialogType(null); }} title="Chọn phòng" description="Chọn các phòng để tải file xuống" icon={} items={mapRoomsToSelectItems(rooms)} onConfirm={async (selectedItems) => { if (!onDownload) return; try { await onDownload(selectedItems); } catch (e) { console.error("Download error:", e); } finally { setDialogOpen(false); setDialogType(null); setTimeout(() => window.location.reload(), 500); } }} /> )} {/* Dialog tải file - tìm thiết bị */} {dialogType === "download-device" && ( { setDialogOpen(false); setDialogType(null); }} rooms={rooms} fetchDevices={fetchDevicesFromRoom} onSelect={async (deviceIds) => { if (!onDownload) { setDialogOpen(false); setDialogType(null); return; } try { await onDownload(deviceIds); } catch (e) { console.error("Download error:", e); } finally { setDialogOpen(false); setDialogType(null); setTimeout(() => window.location.reload(), 500); } }} /> )}
); }