import { useEffect, useState } from 'react';
import { useSearchParams } from 'react-router-dom';
import { Navigate } from 'react-router-dom';
import { Pencil, Save, Plus, Trash2, Power, Copy, Download } from 'lucide-react';
import { useUi } from '../../context/ui-context.jsx';
import { useAuth } from '../../context/auth-context.jsx';
import { useAsync, useAction } from '../../hooks/useAsync.js';
import {
listDevices, getDevice, updateDevice, getDevicesDashboard, listSoftware, listSoftwareDevices,
getDevicesAccess, listDeviceViewers, addDeviceViewer, removeDeviceViewer,
listInventoryTokens, createInventoryToken, setInventoryTokenActive, deleteInventoryToken, getInventoryTokenSecret,
} from '../../services/devices.js';
import { listUsers } from '../../services/users.js';
import { listDepartments } from '../../services/departments.js';
import { Button } from '../../components/Button.jsx';
import { Field } from '../../components/Field.jsx';
import { ComboBox } from '../../components/ComboBox.jsx';
import { DetailPopup } from '../../components/DetailPopup.jsx';
import { DraftNotice } from '../../components/DraftNotice.jsx';
import { useDraftForm, useUserDrafts } from '../../hooks/useDraftForm.js';
import { SearchInput } from '../../components/SearchInput.jsx';
import { ListView } from '../../components/ListView.jsx';
import { Tabs } from '../../components/Tabs.jsx';
import { Badge } from '../../components/Badge.jsx';
import { KpiCard } from '../../components/KpiCard.jsx';
import { buildInstallerBat, downloadTextFile, GLPI_AGENT_VERSION } from './agentInstaller.js';
const KINDS = ['desktop', 'laptop', 'server', 'virtual', 'printer', 'network', 'other'];
const fmtDate = (v) => (v ? String(v).slice(0, 16).replace('T', ' ') : '—');
const gb = (mb) => (mb ? `${(mb / 1024).toFixed(mb >= 10240 ? 0 : 1)} GB` : '—');
const dash = (v) => (v === null || v === undefined || v === '' ? '—' : v);
const daysAgo = (v) => (v ? (Date.now() - new Date(String(v).replace(' ', 'T')).getTime()) / 86400000 : Infinity);
function Row({ label, children }) {
return (
{label}
{children}
);
}
function SimpleTable({ columns, rows, empty }) {
if (!rows?.length) return {empty}
;
return (
{columns.map((c) => | {c.label} | )}
{rows.map((r, i) => (
{columns.map((c) => | {c.render ? c.render(r) : dash(r[c.key])} | )}
))}
);
}
function DeviceDetail({ id, onClose, onChanged, canEdit, tab = 'general', onTabChange }) {
const { t, pushToast } = useUi();
const runAction = useAction();
const { data: device, reload } = useAsync(() => getDevice(id), [id]);
const { data: users } = useAsync(listUsers, []);
const { data: departments } = useAsync(listDepartments, []);
const setTab = onTabChange; // la pestaña vive en la URL (?deviceTab=)
const [searches, setSearches] = useState({});
const [showComponents, setShowComponents] = useState(false);
const [editing, setEditing] = useState(false);
// Borrador de los datos editables del equipo (estado, asignado, departamento, notas).
const draft = useDraftForm({
formId: 'device-edit', entityId: id,
initial: device ? { status: device.status, assignedUserId: device.assigned_user_id || '', departmentId: device.department_id || '', notes: device.notes || '', editVersion: device.edit_version } : {},
label: `${t('eq_detail')}: ${device?.hostname || id}`, enabled: !!device && canEdit && tab === 'general', onDiscard: () => setEditing(false),
});
const form = draft.values;
const setForm = draft.setValues;
useEffect(() => { if (draft.restored) setEditing(true); }, [draft.restored]);
const flat = (list) => (list || []).flatMap((d) => [d, ...(d.children ? flat(d.children) : [])]);
const userOptions = (users || []).map((u) => ({ value: u.id, label: u.name }));
const deptOptions = flat(departments).map((d) => ({ value: d.id, label: d.name }));
function startEdit() {
setEditing(true);
}
function save() {
if (!draft.isDirty) { draft.discard(); setEditing(false); return; }
draft.guardSave(() => runAction(async () => {
await updateDevice(id, {
status: form.status,
assignedUserId: form.assignedUserId ? Number(form.assignedUserId) : null,
departmentId: form.departmentId ? Number(form.departmentId) : null,
notes: form.notes,
editVersion: form.editVersion,
});
draft.clear();
setEditing(false);
await reload();
onChanged();
pushToast({ tone: 'success', message: t('common_saved') });
}), { fetchCurrent: async () => { const d = await getDevice(id); return { status: d.status, assignedUserId: d.assigned_user_id || '', departmentId: d.department_id || '', notes: d.notes || '' }; } });
}
if (!device) return {t('common_loading')}
;
const tabs = [
{ key: 'general', label: t('eq_tab_general'), dirty: editing && draft.isDirty },
{ key: 'users', label: t('eq_tab_users'), count: device.accounts.filter((a) => a.kind === 'local_user').length },
{ key: 'hardware', label: t('eq_tab_hardware') },
{ key: 'network', label: t('eq_tab_network'), count: device.networks.length },
{ key: 'software', label: t('eq_tab_software'), count: device.software.filter((r) => showComponents || !r.is_component).length },
{ key: 'history', label: t('eq_tab_history'), count: device.changes.length },
];
// Un buscador por pestaña (su texto se conserva al cambiar de pestaña y volver).
const q = (searches[tab] || '').trim().toLowerCase();
const hit = (...values) => !q || values.join(' ').toLowerCase().includes(q);
const emptyText = q ? t('common_search_no_results') : t('common_empty');
const only = (rows, fn) => rows.filter((r) => hit(...fn(r)));
const showRows = (list) => list.filter(([label, , text]) => hit(label, text)).map(([label, node]) => {node}
);
const val = (v) => dash(v);
const infoRows = [
[t('eq_hostname'), val(device.hostname), device.hostname],
[t('eq_kind'), t(`eq_kind_${device.kind}`), t(`eq_kind_${device.kind}`)],
[t('eq_manufacturer_model'), val([device.manufacturer, device.model].filter(Boolean).join(' ')), `${device.manufacturer || ''} ${device.model || ''}`],
[t('eq_serial'), val(device.serial), device.serial],
[t('eq_os'), val([device.os_name, device.os_version].filter(Boolean).join(' ')), `${device.os_name || ''} ${device.os_version || ''}`],
[t('eq_domain'), val(device.domain), device.domain],
[t('eq_last_user'), val(device.last_user), device.last_user],
[t('eq_ip'), val(device.ip_main), device.ip_main],
[t('eq_agent'), val(device.agent_version), device.agent_version],
];
const manualRows = [
[t('common_status'), {t(`eq_status_${device.status}`)}, t(`eq_status_${device.status}`)],
[t('eq_assigned_to'), val(device.assigned_user_name), device.assigned_user_name],
[t('common_department'), val(device.department_name), device.department_name],
[t('eq_first_seen'), fmtDate(device.first_seen), fmtDate(device.first_seen)],
[t('eq_last_seen'), fmtDate(device.last_seen), fmtDate(device.last_seen)],
[t('eq_notes'), {val(device.notes)}, device.notes],
];
const cpuText = device.cpus.map((c) => `${c.name || ''}${c.cores ? ` · ${c.cores}c/${c.threads || '?'}t` : ''}`).join(' | ');
const hwRows = [
[t('eq_cpu'), val(cpuText), cpuText],
[t('eq_ram'), gb(device.ram_mb), gb(device.ram_mb)],
[t('eq_disk_total'), <>{gb(device.disk_total_mb)} {device.max_disk_used_pct != null && · {t('eq_disk_max_used')} {device.max_disk_used_pct}%}>, `${gb(device.disk_total_mb)} ${device.max_disk_used_pct ?? ''}`],
];
const memories = only(device.memories, (r) => [r.slot, gb(r.capacity_mb), r.type, r.speed, r.manufacturer]);
const disks = only(device.disks, (r) => [r.model, r.type, gb(r.size_mb), r.serial]);
const volumes = only(device.volumes, (r) => [r.mount, r.filesystem, gb(r.total_mb), gb(r.free_mb)]);
const monitors = only(device.monitors, (r) => [r.manufacturer, r.model, r.serial]);
const hwSection = (title, rows, columns) => (!q || rows.length > 0) && (
<>
{title}
>
);
const sessionNames = device.accounts.filter((a) => a.kind === 'session').map((a) => (a.domain ? `${a.domain}\\${a.name}` : a.name));
const allLocalUsers = device.accounts.filter((a) => a.kind === 'local_user');
const allLocalGroups = device.accounts.filter((a) => a.kind === 'local_group');
const userRows = [
[t('eq_users_session'), val(sessionNames.join(', ')), sessionNames.join(' ')],
[t('eq_users_local_count'), String(allLocalUsers.length), String(allLocalUsers.length)],
[t('eq_groups_local_count'), String(allLocalGroups.length), String(allLocalGroups.length)],
];
const localUsers = only(allLocalUsers, (r) => [r.name, r.sid]);
const localGroups = only(allLocalGroups, (r) => [r.name, r.sid]);
const networks = only(device.networks, (r) => [r.description, r.ip, r.mask, r.gateway, r.mac, r.status]);
const software = only(device.software.filter((r) => showComponents || !r.is_component), (r) => [r.name, r.version, r.publisher]);
const changes = only(device.changes, (r) => [fmtDate(r.created_at), t(`eq_change_${r.change_type}`), r.field, r.old_value, r.new_value]);
return (
<>
{tab === 'software' ? (
) :
}
setSearches({ ...searches, [tab]: v })} />
{tab === 'general' && (
<>
{!editing ? (
<>
{showRows(manualRows)}
{q && showRows(infoRows).length + showRows(manualRows).length === 0 &&
{t('common_search_no_results')}
}
{canEdit &&
}
>
) : (
<>
setForm({ ...form, status: v })}
/>
setForm({ ...form, assignedUserId: v })} />
setForm({ ...form, departmentId: v })} />
{fmtDate(device.first_seen)}
{fmtDate(device.last_seen)}
>
)}
{showRows(infoRows)}
>
)}
{tab === 'users' && (
<>
{showRows(userRows)}
{hwSection(t('eq_users_local'), localUsers, [
{ key: 'name', label: t('common_name') }, { key: 'sid', label: 'SID' },
])}
{hwSection(t('eq_groups_local'), localGroups, [
{ key: 'name', label: t('common_name') }, { key: 'sid', label: 'SID' },
])}
{q && showRows(userRows).length + localUsers.length + localGroups.length === 0 &&
{t('common_search_no_results')}
}
{t('eq_users_hint')}
>
)}
{tab === 'hardware' && (
<>
{showRows(hwRows)}
{hwSection(t('eq_memories'), memories, [
{ key: 'slot', label: t('eq_slot') }, { key: 'capacity_mb', label: t('eq_capacity'), render: (r) => gb(r.capacity_mb) },
{ key: 'type', label: t('eq_type') }, { key: 'speed', label: t('eq_speed') }, { key: 'manufacturer', label: t('eq_manufacturer') },
])}
{hwSection(t('eq_disks'), disks, [
{ key: 'model', label: t('eq_model') }, { key: 'type', label: t('eq_type') },
{ key: 'size_mb', label: t('eq_capacity'), render: (r) => gb(r.size_mb) }, { key: 'serial', label: t('eq_serial') },
])}
{hwSection(t('eq_volumes'), volumes, [
{ key: 'mount', label: t('eq_mount') }, { key: 'filesystem', label: t('eq_fs') },
{ key: 'total_mb', label: t('eq_capacity'), render: (r) => gb(r.total_mb) }, { key: 'free_mb', label: t('eq_free'), render: (r) => gb(r.free_mb) },
])}
{hwSection(t('eq_monitors'), monitors, [
{ key: 'manufacturer', label: t('eq_manufacturer') }, { key: 'model', label: t('eq_model') }, { key: 'serial', label: t('eq_serial') },
])}
{q && showRows(hwRows).length + memories.length + disks.length + volumes.length + monitors.length === 0 &&
{t('common_search_no_results')}
}
>
)}
{tab === 'network' && (
)}
{tab === 'software' && (
)}
{tab === 'history' && (
fmtDate(r.created_at) },
{ key: 'change_type', label: t('eq_change'), render: (r) => t(`eq_change_${r.change_type}`) },
{ key: 'field', label: t('eq_field') },
{ key: 'old_value', label: t('eq_before') }, { key: 'new_value', label: t('eq_after') },
]} />
)}
>
);
}
function SummaryTab() {
const { t } = useUi();
const { data: kpis } = useAsync(getDevicesDashboard, []);
if (!kpis) return {t('common_loading')}
;
return (
<>
{[['eq_by_os', kpis.byOs, (l) => l], ['eq_by_kind', kpis.byKind, (l) => t(`eq_kind_${l}`)], ['eq_top_software', kpis.topSoftware, (l) => l]].map(([title, rows, fmt]) => (
{t(title)}
{rows.length === 0 &&
{t('common_empty')}
}
{rows.map((r) => (
{fmt(r.label)}{r.value}
))}
))}
>
);
}
function DevicesTab({ canEdit }) {
const { t } = useUi();
const [search, setSearch] = useState('');
const [kind, setKind] = useState('');
const [status, setStatus] = useState('active');
// Equipo abierto y su pestaña en la URL (?device=&deviceTab=): F5, retorno tras el login y
// aviso de cambios sin guardar al cerrar el popup.
const [searchParams, setSearchParams] = useSearchParams();
const deviceId = searchParams.get('device') ? Number(searchParams.get('device')) : null;
const deviceTab = searchParams.get('deviceTab') || 'general';
const setDeviceParams = (id, tab) => setSearchParams((prev) => {
const next = new URLSearchParams(prev);
if (id) { next.set('device', id); if (tab && tab !== 'general') next.set('deviceTab', tab); else next.delete('deviceTab'); }
else { next.delete('device'); next.delete('deviceTab'); }
return next;
}, { replace: true });
const selected = deviceId ? { id: deviceId, view: 'info' } : null;
const setSelected = (v) => setDeviceParams(v?.id || null);
const { data: devices, reload } = useAsync(() => listDevices({ kind, status }), [kind, status]);
// Al volver con un borrador de un equipo (que el usuario sigue viendo), se reabre su popup.
const drafts = useUserDrafts();
useEffect(() => {
if (!devices || deviceId) return;
const d = drafts.find((x) => x.formId === 'device-edit' && devices.some((dv) => String(dv.id) === String(x.entityId)));
if (d) setDeviceParams(Number(d.entityId));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [devices]);
const kindOptions = [{ value: '', label: t('common_all') }, ...KINDS.map((k) => ({ value: k, label: t(`eq_kind_${k}`) }))];
const statusOptions = [
{ value: 'active', label: t('eq_status_active') }, { value: 'retired', label: t('eq_status_retired') }, { value: '', label: t('common_all') },
];
const columns = [
{ key: 'hostname', label: t('eq_hostname'), render: (r) => {dash(r.hostname)} },
{ key: 'kind', label: t('eq_kind'), render: (r) => t(`eq_kind_${r.kind}`) },
{ key: 'model', label: t('eq_manufacturer_model'), sortValue: (r) => r.model || '', render: (r) => dash([r.manufacturer, r.model].filter(Boolean).join(' ')) },
{ key: 'os_name', label: t('eq_os'), render: (r) => dash(r.os_name) },
{ key: 'ip_main', label: t('eq_ip'), render: (r) => dash(r.ip_main) },
];
if (!devices) return {t('common_loading')}
;
return (
<>
{devices.length === 0 && !search ? (
{t('eq_empty')}
) : (
r.id}
getSearchHaystack={(r) => [r.hostname, r.serial, r.model, r.manufacturer, r.ip_main, r.mac_main, r.last_user, r.assigned_user_name, r.os_name].join(' ')}
onRowClick={(r) => setSelected({ id: r.id, view: 'info' })} initialSort={{ key: 'hostname', dir: 1 }}
/>
)}
setSelected(null)} width={860} height={680}>
{selected?.view === 'info' && (
setSelected(null)} onChanged={reload} canEdit={canEdit}
tab={deviceTab} onTabChange={(k) => setDeviceParams(selected.id, k)}
/>
)}
>
);
}
function SoftwareTab() {
const { t } = useUi();
const [search, setSearch] = useState('');
const [selected, setSelected] = useState(null);
const [showComponents, setShowComponents] = useState(false);
const { data: rows } = useAsync(() => listSoftware(showComponents), [showComponents]);
const { data: installs } = useAsync(() => (selected ? listSoftwareDevices(selected) : Promise.resolve([])), [selected]);
if (!rows) return {t('common_loading')}
;
const columns = [
{ key: 'name', label: t('common_name'), render: (r) => {r.name} },
{ key: 'publisher', label: t('eq_publisher'), render: (r) => dash(r.publisher) },
{ key: 'version_count', label: t('eq_versions') },
{ key: 'device_count', label: t('eq_installed_on') },
];
return (
<>
r.name}
getSearchHaystack={(r) => `${r.name} ${r.publisher || ''}`} onRowClick={(r) => setSelected(r.name)}
initialSort={{ key: 'device_count', dir: -1 }}
/>
setSelected(null)} width={620} height={520}>
>
);
}
function ShareTab() {
const { t, openDialog } = useUi();
const runAction = useAction();
const { data: viewers, reload } = useAsync(listDeviceViewers, []);
const { data: users } = useAsync(listUsers, []);
const [userId, setUserId] = useState('');
if (!viewers || !users) return {t('common_loading')}
;
const taken = new Set(viewers.map((v) => v.user_id));
// Los roles de TI ya ven Equipos completo; solo se comparte con el resto de usuarios activos.
const options = users
.filter((u) => u.is_active && !taken.has(u.id) && !['superadmin', 'admin', 'it_manager'].includes(u.role_code))
.map((u) => ({ value: u.id, label: `${u.name} · ${u.email}` }));
function add() {
runAction(async () => { await addDeviceViewer(Number(userId)); setUserId(''); await reload(); });
}
function confirmRemove(v) {
openDialog({
tone: 'warning', title: t('eq_share_remove_title'), message: `${v.name}: ${t('eq_share_remove_msg')}`,
actions: [
{ label: t('common_cancel'), variant: 'standard' },
{ label: t('eq_share_remove'), variant: 'danger', onClick: () => runAction(async () => { await removeDeviceViewer(v.user_id); await reload(); }) },
],
});
}
return (
<>
{t('eq_share_title')}
{t('eq_share_hint')}
{viewers.length === 0 ? {t('eq_share_empty')}
: (
{r.name} },
{ key: 'email', label: 'Email' },
{ key: 'role_name', label: t('eq_share_role') },
{ key: 'created_at', label: t('eq_share_since'), render: (r) => fmtDate(r.created_at) },
{ key: 'actions', label: '', sortable: false, render: (r) => },
]}
rows={viewers} getRowKey={(r) => r.user_id}
/>
)}
>
);
}
function AgentsTab() {
const { t, pushToast, openDialog } = useUi();
const runAction = useAction();
const { data: tokens, reload } = useAsync(listInventoryTokens, []);
const [label, setLabel] = useState('');
const [created, setCreated] = useState(null); // { label, token } mostrado UNA vez
const serverUrl = `${window.location.origin}/api/inventory`;
const [installToken, setInstallToken] = useState('');
const [installTag, setInstallTag] = useState('');
function downloadFromToken(tk) {
runAction(async () => {
const { token } = await getInventoryTokenSecret(tk.id);
downloadTextFile('instalar-glpi-agent.bat', buildInstallerBat({ serverUrl, token, tag: installTag }));
});
}
function downloadInstaller() {
downloadTextFile('instalar-glpi-agent.bat', buildInstallerBat({ serverUrl, token: installToken.trim(), tag: installTag }));
}
function create() {
runAction(async () => {
const res = await createInventoryToken(label.trim());
setLabel('');
setCreated(res);
setInstallToken(res.token);
await reload();
});
}
function copy(text) {
navigator.clipboard?.writeText(text).then(() => pushToast({ tone: 'success', message: t('eq_copied') })).catch(() => {});
}
function confirmDelete(tk) {
openDialog({
tone: 'warning', title: t('common_confirm_delete_title'), message: t('eq_token_delete_msg'),
actions: [
{ label: t('common_cancel'), variant: 'standard' },
{ label: t('common_delete'), variant: 'danger', onClick: () => runAction(async () => { await deleteInventoryToken(tk.id); await reload(); }) },
],
});
}
if (!tokens) return {t('common_loading')}
;
return (
<>
{t('eq_agent_setup')}
{t('eq_agent_setup_hint')}
server = {serverUrl}
{t('eq_agent_setup_auth')}
{created && (
{t('eq_token_created')} {created.label}
{created.token}
{t('eq_token_once')}
)}
{r.label} },
{ key: 'is_active', label: t('common_status'), render: (r) => {r.is_active ? t('common_active') : t('common_inactive')} },
{ key: 'last_used_at', label: t('eq_token_last_used'), render: (r) => fmtDate(r.last_used_at) },
{ key: 'created_by_name', label: t('eq_token_created_by'), render: (r) => dash(r.created_by_name) },
{
key: 'actions', label: '', sortable: false,
render: (r) => (
e.stopPropagation()}>
),
},
]}
rows={tokens} getRowKey={(r) => r.id}
/>
>
);
}
export default function EquiposPage() {
const { t } = useUi();
const { data: access } = useAsync(getDevicesAccess, []);
const [searchParams, setSearchParams] = useSearchParams();
const tab = searchParams.get('tab') || 'summary';
const setTab = (key) => setSearchParams({ tab: key }, { replace: true });
const drafts = useUserDrafts();
const devicesDirty = drafts.some((d) => d.formId === 'device-edit');
if (!access) return {t('common_loading')}
;
if (!access.canView) return ;
const tabs = [
{ key: 'summary', label: t('eq_tab_summary') },
{ key: 'devices', label: t('eq_tab_devices'), dirty: devicesDirty },
{ key: 'software', label: t('eq_tab_software_all') },
...(access.canManage ? [{ key: 'agents', label: t('eq_tab_agents') }, { key: 'share', label: t('eq_tab_share') }] : []),
];
return (
{t('nav_devices')}
{tab === 'summary' &&
}
{tab === 'devices' &&
}
{tab === 'software' &&
}
{tab === 'agents' && access.canManage &&
}
{tab === 'share' && access.canManage &&
}
);
}