import { h } from '/static/admin2/vendor/preact.module.js';
import { useEffect, useState } from '/static/admin2/vendor/hooks.module.js';
import htm from '/static/admin2/vendor/htm.module.js';
import { api } from '/static/admin2/api.js';
import { Card } from '/static/admin2/ui/card.js';
import { Table } from '/static/admin2/ui/table.js';
const html = htm.bind(h);
const when = (ts) => new Date(ts * 1000).toLocaleString('ru-RU');
export function SystemPage() {
const [data, setData] = useState(null);
const [error, setError] = useState('');
useEffect(() => {
let alive = true;
const load = () => api('/api/admin/v2/system').then((d) => {
if (alive) { setData(d); setError(''); }
}).catch((e) => { if (alive) setError(e.message || 'Система не отвечает'); });
load();
const t = setInterval(load, 60000);
return () => { alive = false; clearInterval(t); };
}, []);
if (error) return html`
${error}
`;
if (!data) return html`Загрузка…
`;
const bad = data.channels.filter((c) => !c.ok);
return html`
<${Card} title="Каналы" value=${bad.length ? bad.length + ' сбоя' : 'все живы'}
note=${'проверено ' + when(data.generated_at)} />
<${Card} title="Настройки" value=${data.settings.length} note="только чтение" />
<${Card} title="События" value=${data.history.length} note="последние 50" />
Каналы
${data.channels.map((c) => html`
${c.name}${c.ok ? 'ок' : 'сбой'}
${c.detail || ''}
`)}
Настройки
<${Table} columns=${[
{key: 'key', label: 'Ключ'}, {key: 'value', label: 'Значение'},
]} rows=${data.settings.map((r) => ({...r, key: r.key}))} empty="Настроек нет" />
История
<${Table} columns=${[
{key: 'timestamp', label: 'Когда', render: (r) => when(r.timestamp)},
{key: 'label', label: 'Событие'}, {key: 'username', label: 'Кто'},
{key: 'detail', label: 'Было → стало'},
]} rows=${data.history.map((r, i) => ({...r, key: r.id || i}))} empty="Событий нет" />
`;
}