{% extends "base.html" %}
{% set active_page = "logs" %}
{% block page_title %}Kea-DHCP{% endblock %}
{% block page_sub %}<div class="topbar-sub">Log-Ausgabe des kea-dhcp4-server-Dienstes</div>{% endblock %}

{% block content %}

<div class="section-head">
  <div>
    <h2 style="font-size:16px;">Kea-DHCP-Log</h2>
    <div class="hint">Eigene Log-Datei des DHCP-Dienstes (siehe Systemeinstellungen → Logs für Rotation/Aufbewahrung).</div>
  </div>
  <button id="refresh-btn" class="btn btn-secondary">
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12a9 9 0 11-3.2-6.9M21 4v5h-5"/></svg>
    Aktualisieren
  </button>
</div>

<div class="log-shell">
  <div class="log-toolbar">
    <div class="log-dots"><span></span><span></span><span></span></div>
    <span class="text-faint mono" style="font-size:11.5px;">kea-dhcp4.log</span>
  </div>
  <div id="log-box">{{ log_content or "Keine Logdatei gefunden." }}</div>
</div>
{% endblock %}

{% block scripts %}
<script>
function colorizeLine(line) {
    let cls = "";
    if (/\s(FATAL|ERROR)\s/.test(line)) cls = "offline";
    else if (/\sWARN\s/.test(line)) cls = "restart";
    const span = document.createElement("span");
    span.className = "log-line" + (cls ? " " + cls : "");
    span.textContent = line;
    return span;
}

document.addEventListener("DOMContentLoaded", () => {
    function renderLog(text) {
        const box = document.getElementById("log-box");
        box.innerHTML = "";
        const lines = text.split("\n");
        lines.forEach((line, i) => {
            box.appendChild(colorizeLine(line));
            if (i < lines.length - 1) box.appendChild(document.createElement("br"));
        });
        box.scrollTop = box.scrollHeight;
    }

    function fetchLog() {
        fetch("{{ url_for('get_kea_log') }}")
            .then(r => r.text().then(renderLog))
            .catch(err => console.error(err));
    }

    document.getElementById("refresh-btn").addEventListener("click", fetchLog);
    fetchLog();
});
</script>
{% endblock %}
