<?php
// app/views/pages/settings.php

$statusText = $t('common.loading', 'Loading…');
$statusI18n = 'common.loading';
$statusInToolbar = true;
$centerTitle = '';
$centerI18n = null;
$showQueueSummary = true;
$toolbarLeftHtml = '';
$toolbarRightHtml = '';
?>
<!doctype html>
<html lang="ru">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <title data-i18n="settings.page_title"><?php echo htmlspecialchars($t('settings.page_title', 'scmedia / Settings'), ENT_QUOTES); ?></title>
  <link rel="stylesheet" href="/assets/css/common.css">
  <link rel="stylesheet" href="/assets/css/settings.css">
  <link rel="stylesheet" href="/assets/css/admin.css">
</head>
<body>
  <?php
  require __DIR__ . '/../partials/header.php';
  ?>

  <main class="layout">
    <nav class="tabs">
      <button class="tab active" data-tab="server" data-i18n="settings.tabs.server"><?php echo htmlspecialchars($t('settings.tabs.server', 'Server'), ENT_QUOTES); ?></button>
      <button class="tab" data-tab="plugins" data-i18n="settings.tabs.plugins"><?php echo htmlspecialchars($t('settings.tabs.plugins', 'Plugins'), ENT_QUOTES); ?></button>
      <button class="tab" data-tab="sources" data-i18n="settings.tabs.sources"><?php echo htmlspecialchars($t('settings.tabs.sources', 'Sources'), ENT_QUOTES); ?></button>
      <button class="tab" data-tab="rules" data-i18n="settings.tabs.rules"><?php echo htmlspecialchars($t('settings.tabs.rules', 'Rules'), ENT_QUOTES); ?></button>
      <button class="tab" data-tab="templates" data-i18n="settings.tabs.templates"><?php echo htmlspecialchars($t('settings.tabs.templates', 'Templates'), ENT_QUOTES); ?></button>
      <button class="tab" data-tab="tasks" data-i18n="settings.tabs.tasks"><?php echo htmlspecialchars($t('settings.tabs.tasks', 'Tasks'), ENT_QUOTES); ?></button>
      <button class="tab" data-tab="tools" data-i18n="settings.tabs.tools"><?php echo htmlspecialchars($t('settings.tabs.tools', 'Programs'), ENT_QUOTES); ?></button>
      <button class="tab" data-tab="logs" data-i18n="settings.tabs.logs"><?php echo htmlspecialchars($t('settings.tabs.logs', 'Logs'), ENT_QUOTES); ?></button>
      <button class="tab" data-tab="admin" data-i18n="admin.title"><?php echo htmlspecialchars($t('admin.title', 'Admin'), ENT_QUOTES); ?></button>
      <button class="tab" data-tab="debug" id="tabDebug"<?php echo empty($debugToolsEnabled) ? ' style="display:none;"' : ''; ?> data-i18n="settings.tabs.debug"><?php echo htmlspecialchars($t('settings.tabs.debug', 'Debug'), ENT_QUOTES); ?></button>
      <?php
      $statusHidden = false;
      $statusClass = $statusHidden ? 'status hidden' : 'status';
      $statusAttr = $statusI18n ? ' data-i18n="' . htmlspecialchars($statusI18n, ENT_QUOTES) . '"' : '';
      ?>
      <div class="tabs-footer">
        <span id="status" class="<?php echo htmlspecialchars($statusClass, ENT_QUOTES); ?>"<?php echo $statusAttr; ?>>
          <?php echo htmlspecialchars($statusText, ENT_QUOTES); ?>
        </span>
        <button id="btnSave" class="btn primary" type="button" disabled data-i18n="common.save"><?php echo htmlspecialchars($t('common.save', 'Save'), ENT_QUOTES); ?></button>
      </div>
    </nav>

    <section class="panel">
      <!-- ===================== SOURCES TAB ===================== -->
      <div class="tabpane" id="pane-sources">
        <div class="pane-header">
          <h2 data-i18n="settings.sources.title"><?php echo htmlspecialchars($t('settings.sources.title', 'Sources'), ENT_QUOTES); ?></h2>
          <button id="btnAddRoot" class="btn primary" data-i18n="settings.sources.add_root"><?php echo htmlspecialchars($t('settings.sources.add_root', 'Add source'), ENT_QUOTES); ?></button>
        </div>

        <div class="card">
          <?php
          $id = 'rootsTable';
          $tableClass = 'data-table';
          $headers = [
            ['label' => $t('settings.sources.th_type', 'Type'), 'i18n' => 'settings.sources.th_type', 'sort' => 'type', 'key' => 'type', 'filter' => ['type' => 'text', 'ops' => ['like','eq']]],
            ['label' => $t('settings.sources.th_path', 'Path'), 'i18n' => 'settings.sources.th_path', 'sort' => 'path', 'key' => 'path', 'filter' => ['type' => 'text', 'ops' => ['like','eq','empty']]],
            ['label' => $t('settings.sources.th_status', 'Status'), 'i18n' => 'settings.sources.th_status', 'sort' => 'status', 'key' => 'status', 'filter' => ['type' => 'text', 'ops' => ['like','eq']]],
            ['label' => '', 'i18n' => '', 'key' => '', 'filter' => null],
          ];
          $attrs = ['data-table' => 'roots'];
          require __DIR__ . '/../partials/table.php';
          ?>
          <div class="hint" id="rootsHint"></div>
        </div>

      </div>

      <!-- ===================== PLUGINS TAB ===================== -->
      <div class="tabpane" id="pane-plugins">
        <div class="pane-header">
          <h2 data-i18n="settings.plugins.title"><?php echo htmlspecialchars($t('settings.plugins.title', 'Plugins'), ENT_QUOTES); ?></h2>
          <div class="row">
            <button id="btnAddPlugin" class="btn primary" data-i18n="settings.plugins.add"><?php echo htmlspecialchars($t('settings.plugins.add', 'Add plugin'), ENT_QUOTES); ?></button>
            <input id="pluginFileInput" type="file" style="display:none;">
          </div>
        </div>

        <div class="card">
          <?php
          $id = 'pluginsTable';
          $tableClass = 'data-table';
          $headers = [
            ['label' => $t('settings.plugins.th_name', 'Plugin'), 'i18n' => 'settings.plugins.th_name', 'sort' => 'name', 'key' => 'name', 'filter' => ['type' => 'text', 'ops' => ['like','eq','empty']]],
            ['label' => $t('settings.plugins.th_type', 'Type'), 'i18n' => 'settings.plugins.th_type', 'key' => 'type', 'filter' => ['type' => 'text', 'ops' => ['like','eq']]],
            ['label' => $t('settings.plugins.th_status', 'Status'), 'i18n' => 'settings.plugins.th_status', 'key' => 'status', 'filter' => ['type' => 'text', 'ops' => ['like','eq']]],
            ['label' => '', 'i18n' => '', 'key' => '', 'filter' => null],
          ];
          $attrs = ['data-table' => 'plugins'];
          require __DIR__ . '/../partials/table.php';
          ?>
          <div class="hint" id="pluginsHint"></div>
        </div>
      </div>

      <!-- ===================== TASKS TAB ===================== -->
      <div class="tabpane" id="pane-tasks">
        <div class="pane-header">
          <h2 data-i18n="settings.tasks.title"><?php echo htmlspecialchars($t('settings.tasks.title', 'Tasks'), ENT_QUOTES); ?></h2>
          <button id="btnAddTask" class="btn primary" data-i18n="settings.tasks.add"><?php echo htmlspecialchars($t('settings.tasks.add', 'Add task'), ENT_QUOTES); ?></button>
        </div>
        <div class="card">
          <?php
          $id = 'tasksTable';
          $tableClass = 'data-table';
          $headers = [
            ['label' => $t('settings.tasks.th_name', 'Task'), 'i18n' => 'settings.tasks.th_name', 'sort' => 'name', 'key' => 'name', 'filter' => ['type' => 'text', 'ops' => ['like','eq','empty']]],
            ['label' => $t('settings.tasks.th_sources', 'Sources'), 'i18n' => 'settings.tasks.th_sources', 'key' => 'sources', 'filter' => ['type' => 'text', 'ops' => ['like','eq','empty']]],
            ['label' => $t('settings.tasks.th_rules', 'Rules'), 'i18n' => 'settings.tasks.th_rules', 'key' => 'rules', 'filter' => ['type' => 'text', 'ops' => ['like','eq','empty']]],
            ['label' => $t('settings.tasks.th_template', 'Template'), 'i18n' => 'settings.tasks.th_template', 'key' => 'template', 'filter' => ['type' => 'text', 'ops' => ['like','eq','empty']]],
            ['label' => $t('settings.tasks.th_status', 'Status'), 'i18n' => 'settings.tasks.th_status', 'key' => 'status', 'filter' => ['type' => 'text', 'ops' => ['like','eq']]],
            ['label' => '', 'i18n' => '', 'key' => '', 'filter' => null],
          ];
          $attrs = ['data-table' => 'tasks'];
          require __DIR__ . '/../partials/table.php';
          ?>
          <div class="hint" id="tasksHint"></div>
        </div>
      </div>

      <!-- ===================== RULES TAB ===================== -->
      <div class="tabpane" id="pane-rules">
        <div class="pane-header">
          <h2 data-i18n="settings.rules.title"><?php echo htmlspecialchars($t('settings.rules.title', 'Rules'), ENT_QUOTES); ?></h2>
          <button class="btn primary" id="btnAddRule" data-i18n="settings.rules.add_rule"><?php echo htmlspecialchars($t('settings.rules.add_rule', 'Add rule'), ENT_QUOTES); ?></button>
        </div>

        <div class="card">
          <?php
          $id = 'rulesTable';
          $tableClass = 'data-table';
          $headers = [
            ['label' => $t('rules.th.name', 'Name'), 'i18n' => 'rules.th.name', 'sort' => 'name', 'key' => 'name', 'filter' => ['type' => 'text', 'ops' => ['like','eq','empty']]],
            ['label' => $t('rules.th.tags', 'Tags'), 'i18n' => 'rules.th.tags', 'key' => 'tags', 'filter' => ['type' => 'text', 'ops' => ['like','eq','empty']]],
            ['label' => $t('rules.th.conditions', 'Conditions'), 'i18n' => 'rules.th.conditions', 'key' => 'conditions', 'filter' => ['type' => 'text', 'ops' => ['like','eq','empty']]],
            ['label' => $t('rules.th.status', 'Status'), 'i18n' => 'rules.th.status', 'key' => 'status', 'filter' => ['type' => 'text', 'ops' => ['like','eq']]],
            ['label' => '', 'i18n' => '', 'key' => '', 'filter' => null],
          ];
          $attrs = ['data-table' => 'rules'];
          require __DIR__ . '/../partials/table.php';
          ?>
        </div>
      </div>

      <!-- ===================== TEMPLATES TAB ===================== -->
      <div class="tabpane" id="pane-templates">
        <div class="pane-header">
          <h2 data-i18n="settings.templates.title"><?php echo htmlspecialchars($t('settings.templates.title', 'Templates'), ENT_QUOTES); ?></h2>
          <button id="btnAddTemplate" class="btn primary" data-i18n="settings.templates.add"><?php echo htmlspecialchars($t('settings.templates.add', 'Add template'), ENT_QUOTES); ?></button>
        </div>
        <div class="card">
          <?php
          $id = 'templatesTable';
          $tableClass = 'data-table';
          $headers = [
            ['label' => $t('settings.templates.th_name', 'Template'), 'i18n' => 'settings.templates.th_name', 'sort' => 'name', 'key' => 'name', 'filter' => ['type' => 'text', 'ops' => ['like','eq','empty']]],
            ['label' => $t('settings.templates.th_action', 'Action'), 'i18n' => 'settings.templates.th_action', 'key' => 'action', 'filter' => ['type' => 'text', 'ops' => ['like','eq','empty']]],
            ['label' => $t('settings.templates.th_status', 'Status'), 'i18n' => 'settings.templates.th_status', 'key' => 'status', 'filter' => ['type' => 'text', 'ops' => ['like','eq']]],
            ['label' => '', 'i18n' => '', 'key' => '', 'filter' => null],
          ];
          $attrs = ['data-table' => 'templates'];
          require __DIR__ . '/../partials/table.php';
          ?>
          <div class="hint" id="templatesHint"></div>
        </div>
      </div>

      <!-- ===================== SERVER TAB ===================== -->
      <div class="tabpane active" id="pane-server">
        <div class="pane-header">
          <h2 data-i18n="settings.server.title"><?php echo htmlspecialchars($t('settings.server.title', 'Server'), ENT_QUOTES); ?></h2>
        </div>

        <div class="card">
          <h3 data-i18n="settings.server.system"><?php echo htmlspecialchars($t('settings.server.system', 'System'), ENT_QUOTES); ?></h3>
          <div class="grid">
            <label>
              <div class="lbl" data-i18n="settings.server.env"><?php echo htmlspecialchars($t('settings.server.env', 'Environment'), ENT_QUOTES); ?></div>
              <input id="serverEnv" type="text" readonly data-local="true">
            </label>
            <label>
              <div class="lbl" data-i18n="settings.server.app_id"><?php echo htmlspecialchars($t('settings.server.app_id', 'App ID'), ENT_QUOTES); ?></div>
              <input id="serverAppId" type="text" readonly data-local="true">
            </label>
            <label>
              <div class="lbl" data-i18n="settings.server.debug"><?php echo htmlspecialchars($t('settings.server.debug', 'Debug'), ENT_QUOTES); ?></div>
              <input id="serverDebug" type="text" readonly data-local="true">
            </label>
            <label>
              <div class="lbl" data-i18n="settings.server.backend_version"><?php echo htmlspecialchars($t('settings.server.backend_version', 'Backend version'), ENT_QUOTES); ?></div>
              <input id="serverBackendVersion" type="text" readonly data-local="true">
            </label>
            <label>
              <div class="lbl" data-i18n="settings.server.db_version"><?php echo htmlspecialchars($t('settings.server.db_version', 'DB version'), ENT_QUOTES); ?></div>
              <input id="serverDbVersion" type="text" readonly data-local="true">
            </label>
          </div>
        </div>

        <div class="card">
          <h3 data-i18n="settings.server.background"><?php echo htmlspecialchars($t('settings.server.background', 'Background'), ENT_QUOTES); ?></h3>
          <div class="grid">
            <label>
              <div class="lbl" data-i18n="settings.background.mode"><?php echo htmlspecialchars($t('settings.background.mode', 'Mode'), ENT_QUOTES); ?></div>
              <select id="bgMode">
                <option value="light" data-i18n="settings.background.mode_light"><?php echo htmlspecialchars($t('settings.background.mode_light', 'Light'), ENT_QUOTES); ?></option>
                <option value="normal" data-i18n="settings.background.mode_normal"><?php echo htmlspecialchars($t('settings.background.mode_normal', 'Normal'), ENT_QUOTES); ?></option>
                <option value="aggressive" data-i18n="settings.background.mode_aggressive"><?php echo htmlspecialchars($t('settings.background.mode_aggressive', 'Aggressive'), ENT_QUOTES); ?></option>
              </select>
            </label>
            <label>
              <div class="lbl" data-i18n="settings.background.max_parallel"><?php echo htmlspecialchars($t('settings.background.max_parallel', 'Max parallel jobs'), ENT_QUOTES); ?></div>
              <input id="bgMaxParallel" type="number" min="1" max="10">
            </label>
            <label>
              <div class="lbl" data-i18n="settings.background.max_network"><?php echo htmlspecialchars($t('settings.background.max_network', 'Max network jobs'), ENT_QUOTES); ?></div>
              <input id="bgMaxNetwork" type="number" min="1" max="10">
            </label>
            <label>
              <div class="lbl" data-i18n="settings.background.max_io"><?php echo htmlspecialchars($t('settings.background.max_io', 'Max IO jobs'), ENT_QUOTES); ?></div>
              <input id="bgMaxIo" type="number" min="1" max="10">
            </label>
            <label>
              <div class="lbl" data-i18n="settings.background.batch_sleep"><?php echo htmlspecialchars($t('settings.background.batch_sleep', 'Sleep between batches (ms)'), ENT_QUOTES); ?></div>
              <input id="bgBatchSleep" type="number" min="0" max="60000">
            </label>
            <label>
              <div class="lbl" data-i18n="settings.background.watchdog"><?php echo htmlspecialchars($t('settings.background.watchdog', 'Stalled watchdog (min)'), ENT_QUOTES); ?></div>
              <input id="bgWatchdog" type="number" min="1" max="120">
            </label>
            <label>
              <div class="lbl" data-i18n="settings.background.sse_ttl"><?php echo htmlspecialchars($t('settings.background.sse_ttl', 'SSE session TTL (sec)'), ENT_QUOTES); ?></div>
              <input id="bgSseTtl" type="number" min="1" max="600">
            </label>
          </div>
        </div>

        <div class="card">
          <h3 data-i18n="settings.server.sse"><?php echo htmlspecialchars($t('settings.server.sse', 'SSE'), ENT_QUOTES); ?></h3>
          <div class="grid">
            <label>
              <div class="lbl" data-i18n="settings.ui.sse_tick"><?php echo htmlspecialchars($t('settings.ui.sse_tick', 'SSE tick interval (sec)'), ENT_QUOTES); ?></div>
              <input id="uiSseTickSeconds" type="number" min="1" max="60">
            </label>
          </div>
        </div>

        <div class="card">
          <h3 data-i18n="settings.server.safety"><?php echo htmlspecialchars($t('settings.server.safety', 'Safety'), ENT_QUOTES); ?></h3>
          <div class="grid">
            <label>
              <div class="lbl" data-i18n="settings.server.safety.max_depth"><?php echo htmlspecialchars($t('settings.server.safety.max_depth', 'Max depth'), ENT_QUOTES); ?></div>
              <input id="safetyMaxDepth" type="number" min="1" max="50">
            </label>
            <label>
              <div class="lbl" data-i18n="settings.server.safety.max_files"><?php echo htmlspecialchars($t('settings.server.safety.max_files', 'Max files per item'), ENT_QUOTES); ?></div>
              <input id="safetyMaxFiles" type="number" min="100" max="2000000">
            </label>
            <label>
              <div class="lbl" data-i18n="settings.server.safety.max_items"><?php echo htmlspecialchars($t('settings.server.safety.max_items', 'Max items per scan'), ENT_QUOTES); ?></div>
              <input id="safetyMaxItems" type="number" min="0" max="2000000">
            </label>
          </div>
        </div>

        <div class="card">
          <h3 data-i18n="settings.server.diagnostics"><?php echo htmlspecialchars($t('settings.server.diagnostics', 'Diagnostics'), ENT_QUOTES); ?></h3>
          <div class="grid">
            <label>
              <div class="lbl" data-i18n="settings.server.php_version"><?php echo htmlspecialchars($t('settings.server.php_version', 'PHP version'), ENT_QUOTES); ?></div>
              <input id="serverPhpVersion" type="text" readonly data-local="true">
            </label>
            <label>
              <div class="lbl" data-i18n="settings.server.disk"><?php echo htmlspecialchars($t('settings.server.disk', 'Disk free'), ENT_QUOTES); ?></div>
              <input id="serverDisk" type="text" readonly data-local="true">
            </label>
            <label>
              <div class="lbl" data-i18n="settings.server.binaries"><?php echo htmlspecialchars($t('settings.server.binaries', 'Binaries'), ENT_QUOTES); ?></div>
              <input id="serverBinaries" type="text" readonly data-local="true">
            </label>
            <label>
              <div class="lbl" data-i18n="settings.server.jobs"><?php echo htmlspecialchars($t('settings.server.jobs', 'Jobs status'), ENT_QUOTES); ?></div>
              <input id="serverJobs" type="text" readonly data-local="true">
            </label>
          </div>
        </div>

        <div class="card">
          <h3 data-i18n="settings.server.snapshots"><?php echo htmlspecialchars($t('settings.server.snapshots', 'Snapshots'), ENT_QUOTES); ?></h3>
          <div class="row">
            <input id="snapshotLabel" type="text" placeholder="Snapshot label" data-i18n-placeholder="settings.server.snapshot_label" data-local="true">
            <button id="btnCreateSnapshot" class="btn" type="button" data-i18n="settings.server.snapshot_create"><?php echo htmlspecialchars($t('settings.server.snapshot_create', 'Create snapshot'), ENT_QUOTES); ?></button>
          </div>
          <div class="table-wrap">
            <table class="data-table" id="snapshotsTable">
              <thead>
                <tr>
                  <th data-i18n="settings.server.snapshot_id"><?php echo htmlspecialchars($t('settings.server.snapshot_id', 'ID'), ENT_QUOTES); ?></th>
                  <th data-i18n="settings.server.snapshot_label"><?php echo htmlspecialchars($t('settings.server.snapshot_label', 'Label'), ENT_QUOTES); ?></th>
                  <th data-i18n="settings.server.snapshot_date"><?php echo htmlspecialchars($t('settings.server.snapshot_date', 'Created'), ENT_QUOTES); ?></th>
                  <th></th>
                </tr>
              </thead>
              <tbody></tbody>
            </table>
          </div>
          <div class="hint" id="snapshotsHint"></div>
        </div>

        <div class="card">
          <h3 data-i18n="settings.server.danger"><?php echo htmlspecialchars($t('settings.server.danger', 'Danger zone'), ENT_QUOTES); ?></h3>
        </div>

        <div class="card">
          <h3 data-i18n="settings.about.title"><?php echo htmlspecialchars($t('settings.about.title', 'About'), ENT_QUOTES); ?></h3>
          <div class="about-card" data-version>
            <div class="about-row">
              <img class="about-logo" src="/assets/icons/scmedia.png" alt="scMedia">
              <div class="about-body">
                <div class="about-title">
                  <a class="about-link" href="https://safe-cap.com/software/scMedia">scMedia</a>
                  <span class="version">v0.0.0</span>
                </div>
                <div class="about-meta">
                  <span class="about-company">SAFE-CAP</span>
                  <a class="about-link muted" href="https://safe-cap.com/">safe-cap.com</a>
                </div>
              </div>
            </div>
          </div>
        </div>

        <div class="card">
          <?php
          $id = 'aboutPluginsTable';
          $tableClass = 'table';
          $headers = [
            ['label' => $t('settings.about.table_name', 'Name'), 'i18n' => 'settings.about.table_name'],
            ['label' => $t('settings.about.table_type', 'Type'), 'i18n' => 'settings.about.table_type'],
            ['label' => $t('settings.about.table_author', 'Author'), 'i18n' => 'settings.about.table_author'],
            ['label' => $t('settings.about.table_version', 'Version'), 'i18n' => 'settings.about.table_version'],
            ['label' => $t('settings.about.table_update', 'Update'), 'i18n' => 'settings.about.table_update'],
          ];
          $footer = false;
          require __DIR__ . '/../partials/table.php';
          ?>
        </div>
      </div>

      <!-- ===================== TOOLS TAB ===================== -->
      <div class="tabpane" id="pane-tools">
        <div class="pane-header">
          <h2 data-i18n="settings.tools.title"><?php echo htmlspecialchars($t('settings.tools.title', 'Programs'), ENT_QUOTES); ?></h2>
          <div class="row">
            <div class="dropdown">
              <button id="btnAddTool" class="btn primary" data-i18n="settings.tools.add"><?php echo htmlspecialchars($t('settings.tools.add', 'Add tool'), ENT_QUOTES); ?></button>
              <div class="dropdown-menu" id="toolTypeMenu">
                <button type="button" data-tool-type="mkvmerge" data-i18n="settings.tools.mkvmerge"><?php echo htmlspecialchars($t('settings.tools.mkvmerge', 'mkvmerge'), ENT_QUOTES); ?></button>
                <button type="button" data-tool-type="mkvpropedit" data-i18n="settings.tools.mkvpropedit"><?php echo htmlspecialchars($t('settings.tools.mkvpropedit', 'mkvpropedit'), ENT_QUOTES); ?></button>
                <button type="button" data-tool-type="ffmpeg" data-i18n="settings.tools.ffmpeg"><?php echo htmlspecialchars($t('settings.tools.ffmpeg', 'ffmpeg'), ENT_QUOTES); ?></button>
              </div>
            </div>
            <button id="btnDetectTools" class="btn" data-i18n="settings.tools.detect"><?php echo htmlspecialchars($t('settings.tools.detect', 'Detect'), ENT_QUOTES); ?></button>
          </div>
        </div>
        <div class="card">
          <?php
          $id = 'toolsTable';
          $tableClass = 'data-table';
          $headers = [
            ['label' => $t('settings.tools.th_name', 'Tool'), 'i18n' => 'settings.tools.th_name', 'sort' => 'name', 'key' => 'name', 'filter' => ['type' => 'text', 'ops' => ['like','eq','empty']]],
            ['label' => $t('settings.tools.th_path', 'Path'), 'i18n' => 'settings.tools.th_path', 'key' => 'path', 'filter' => ['type' => 'text', 'ops' => ['like','eq','empty']]],
            ['label' => '', 'i18n' => '', 'key' => '', 'filter' => null],
          ];
          $attrs = ['data-table' => 'tools'];
          require __DIR__ . '/../partials/table.php';
          ?>
          <div class="hint" id="toolsHint"></div>
        </div>
      </div>

      <!-- ===================== LOGS TAB ===================== -->
      <div class="tabpane" id="pane-logs">
        <div class="pane-header">
          <h2 data-i18n="settings.logs.title"><?php echo htmlspecialchars($t('settings.logs.title', 'Logs'), ENT_QUOTES); ?></h2>
        </div>
        <div class="card">
          <div class="tabs logs-tabs">
            <button class="tab active" data-logs-tab="view" data-i18n="settings.logs.tab_view"><?php echo htmlspecialchars($t('settings.logs.tab_view', 'View'), ENT_QUOTES); ?></button>
            <button class="tab" data-logs-tab="settings" data-i18n="settings.logs.tab_settings"><?php echo htmlspecialchars($t('settings.logs.tab_settings', 'Settings'), ENT_QUOTES); ?></button>
          </div>

          <div class="logs-pane active" id="logs-pane-view">
            <div class="row logs-row logs-filters">
              <label>
                <div class="lbl" data-i18n="settings.logs.type"><?php echo htmlspecialchars($t('settings.logs.type', 'Type'), ENT_QUOTES); ?></div>
                <select id="logsType" data-local="true">
                  <option value="system" data-i18n="settings.logs.type_system"><?php echo htmlspecialchars($t('settings.logs.type_system', 'System'), ENT_QUOTES); ?></option>
                  <option value="audit" data-i18n="settings.logs.type_audit"><?php echo htmlspecialchars($t('settings.logs.type_audit', 'Audit'), ENT_QUOTES); ?></option>
                </select>
              </label>
              <div class="row logs-row" id="logsControlsShared">
                <label>
                <div class="lbl" data-i18n="settings.logs.date_from"><?php echo htmlspecialchars($t('settings.logs.date_from', 'From'), ENT_QUOTES); ?></div>
                <input id="logsDateFrom" type="date" data-local="true">
                </label>
                <label>
                <div class="lbl" data-i18n="settings.logs.date_to"><?php echo htmlspecialchars($t('settings.logs.date_to', 'To'), ENT_QUOTES); ?></div>
                <input id="logsDateTo" type="date" data-local="true">
                </label>
              </div>
              <div class="row logs-row" id="logsControlsLevel">
                <label>
                <div class="lbl" data-i18n="settings.logs.filter_level"><?php echo htmlspecialchars($t('settings.logs.filter_level', 'Level'), ENT_QUOTES); ?></div>
                <select id="logsFilterLevel" data-local="true">
                  <option value="all" data-i18n="filters.all"><?php echo htmlspecialchars($t('filters.all', 'All'), ENT_QUOTES); ?></option>
                  <option value="debug">debug</option>
                  <option value="info">info</option>
                  <option value="warn">warn</option>
                  <option value="error">error</option>
                </select>
                </label>
              </div>
              <div class="row logs-row is-hidden" id="logsControlsEvent">
                <label>
                  <div class="lbl" data-i18n="settings.logs.filter_event"><?php echo htmlspecialchars($t('settings.logs.filter_event', 'Event'), ENT_QUOTES); ?></div>
                  <select id="logsFilterEvent" data-local="true">
                    <option value="all" data-i18n="filters.all"><?php echo htmlspecialchars($t('filters.all', 'All'), ENT_QUOTES); ?></option>
                  </select>
                </label>
              </div>
              <div class="row logs-row" id="logsControlsSystemActions">
                <span class="hint" id="logsHint"></span>
                <span class="hint" id="logsSelectionCount"></span>
              </div>
            </div>
            <div class="row logs-row logs-actions" id="logsControlsActions">
              <button id="btnLoadLogs" class="btn" data-i18n="settings.logs.load"><?php echo htmlspecialchars($t('settings.logs.load', 'Load'), ENT_QUOTES); ?></button>
              <button id="btnResetLogsDate" class="btn" data-i18n="settings.logs.reset"><?php echo htmlspecialchars($t('settings.logs.reset', 'Reset'), ENT_QUOTES); ?></button>
              <button id="btnLogsSelectAll" class="btn" type="button" data-i18n="settings.logs.select_all"><?php echo htmlspecialchars($t('settings.logs.select_all', 'Select all'), ENT_QUOTES); ?></button>
              <button id="btnLogsCopy" class="btn" type="button" data-i18n="settings.logs.copy_selected"><?php echo htmlspecialchars($t('settings.logs.copy_selected', 'Copy selected'), ENT_QUOTES); ?></button>
            </div>
            <div id="logsOutputWrap">
              <?php
              $id = 'logsTable';
              $tableClass = 'table';
              $headers = [
                ['label' => '', 'key' => '', 'filter' => null],
                ['label' => $t('settings.logs.when', 'When'), 'i18n' => 'settings.logs.when', 'key' => 'ts', 'sort' => 'ts', 'filter' => ['type' => 'date', 'ops' => ['eq','between']]],
                ['label' => $t('settings.logs.level', 'Level'), 'i18n' => 'settings.logs.level', 'key' => 'level', 'sort' => 'level', 'filter' => ['type' => 'select', 'options' => ['debug','info','warn','error']]],
                ['label' => $t('settings.logs.message', 'Message'), 'i18n' => 'settings.logs.message', 'key' => 'message', 'sort' => 'message', 'filter' => ['type' => 'text', 'ops' => ['like','eq','empty']]],
                ['label' => $t('settings.logs.context', 'Context'), 'i18n' => 'settings.logs.context', 'key' => 'context_json', 'sort' => 'context_json', 'filter' => ['type' => 'text', 'ops' => ['like','eq','empty']]],
              ];
              $attrs = ['data-table-id' => 'system-logs'];
              $tbodyId = 'logsTableBody';
              require __DIR__ . '/../partials/table.php';
              ?>
            </div>
            <div id="logsAuditWrap" class="admin-card is-hidden">
              <div class="admin-table-wrap">
                <?php
                $id = 'adminAuditTable';
                $tableClass = 'grid admin-table';
                $headers = [
                  ['label' => '', 'key' => '', 'filter' => null],
                  ['label' => 'When', 'key' => 'created_at', 'filter' => ['type' => 'date', 'ops' => ['eq','between']]],
                  ['label' => 'Actor', 'key' => 'actor', 'filter' => ['type' => 'text', 'ops' => ['like','eq','empty']]],
                  ['label' => 'Action', 'key' => 'action', 'filter' => ['type' => 'text', 'ops' => ['like','eq']]],
                  ['label' => 'Target', 'key' => 'target', 'filter' => ['type' => 'text', 'ops' => ['like','eq','empty']]],
                  ['label' => 'Meta', 'key' => 'meta_json', 'filter' => ['type' => 'text', 'ops' => ['like','eq','empty']]],
                ];
                $attrs = ['data-table-id' => 'admin-audit'];
                $footer = false;
                $tbodyId = 'adminAudit';
                require __DIR__ . '/../partials/table.php';
                ?>
              </div>
            </div>
          </div>

          <div class="logs-pane" id="logs-pane-settings">
            <div class="row logs-row">
              <label>
                <div class="lbl" data-i18n="settings.logs.retention"><?php echo htmlspecialchars($t('settings.logs.retention', 'Retention'), ENT_QUOTES); ?></div>
                <select id="logsRetention">
                  <option value="1">1</option>
                  <option value="7">7</option>
                  <option value="14">14</option>
                  <option value="90">90</option>
                  <option value="0" data-i18n="settings.logs.forever"><?php echo htmlspecialchars($t('settings.logs.forever', 'Forever'), ENT_QUOTES); ?></option>
                </select>
              </label>
              <label>
                <div class="lbl" data-i18n="settings.logs.level"><?php echo htmlspecialchars($t('settings.logs.level', 'Log level'), ENT_QUOTES); ?></div>
                <select id="logsLevel">
                  <option value="debug">debug</option>
                  <option value="info">info</option>
                  <option value="warn">warn</option>
                  <option value="error">error</option>
                </select>
              </label>
              <button id="btnCleanupLogs" class="btn" data-i18n="settings.logs.cleanup"><?php echo htmlspecialchars($t('settings.logs.cleanup', 'Cleanup'), ENT_QUOTES); ?></button>
              <span class="hint" id="logsSettingsHint"></span>
            </div>
          </div>
        </div>
      </div>

      <div class="tabpane" id="pane-admin">
        <div class="admin-layout">
          <section class="admin-card">
            <div class="card-head">
              <h2 data-i18n="admin.users"><?php echo htmlspecialchars($t('admin.users', 'Users'), ENT_QUOTES); ?></h2>
              <button id="btnReloadUsers" class="btn" type="button" data-i18n="common.reload"><?php echo htmlspecialchars($t('common.reload', 'Reload'), ENT_QUOTES); ?></button>
            </div>
            <div class="admin-table-wrap">
              <?php
              $id = 'adminUsersTable';
              $tableClass = 'grid admin-table';
              $headers = [
                ['label' => 'Email', 'key' => 'email', 'filter' => ['type' => 'text', 'ops' => ['like','eq','empty']]],
                ['label' => 'Roles', 'key' => 'roles', 'filter' => ['type' => 'text', 'ops' => ['like','eq','empty']]],
                ['label' => 'Status', 'key' => 'status', 'filter' => ['type' => 'select', 'options' => ['active','disabled']]],
                ['label' => 'Last login', 'key' => 'last_login_at', 'filter' => ['type' => 'date', 'ops' => ['eq','between']]],
                ['label' => 'Actions', 'key' => '', 'filter' => null],
              ];
              $attrs = ['data-table-id' => 'admin-users'];
              $footer = false;
              $tbodyId = 'adminUsers';
              require __DIR__ . '/../partials/table.php';
              ?>
            </div>
          </section>
        </div>
      </div>

      <!-- ===================== DEBUG TAB ===================== -->
      <div class="tabpane" id="pane-debug">
        <div class="pane-header">
          <h2 data-i18n="settings.debug.title"><?php echo htmlspecialchars($t('settings.debug.title', 'Debug'), ENT_QUOTES); ?></h2>
        </div>

        <div class="card warn">
          <h3 data-i18n="settings.debug.content.title"><?php echo htmlspecialchars($t('settings.debug.content.title', 'Content data'), ENT_QUOTES); ?></h3>
          <div class="grid">
            <div>
              <div class="lbl" data-i18n="settings.debug.content.files"><?php echo htmlspecialchars($t('settings.debug.content.files', 'Media files'), ENT_QUOTES); ?></div>
              <div id="dbgContentFiles">-</div>
            </div>
            <div>
              <div class="lbl" data-i18n="settings.debug.content.meta"><?php echo htmlspecialchars($t('settings.debug.content.meta', 'Metadata'), ENT_QUOTES); ?></div>
              <div id="dbgContentMeta">-</div>
            </div>
            <div>
              <div class="lbl" data-i18n="settings.debug.content.items"><?php echo htmlspecialchars($t('settings.debug.content.items', 'Indexed items'), ENT_QUOTES); ?></div>
              <div id="dbgContentItems">-</div>
            </div>
          </div>
          <input id="dbgContentConfirm" type="text" placeholder="<?php echo htmlspecialchars($t('settings.debug.content.ph', 'Type CLEAR CONTENT'), ENT_QUOTES); ?>">
          <button id="btnClearContent" class="btn danger" data-i18n="settings.debug.content.clear_btn"><?php echo htmlspecialchars($t('settings.debug.content.clear_btn', 'Clear content'), ENT_QUOTES); ?></button>
          <pre id="dbgContentOut" class="pre"></pre>
        </div>

        <div class="card danger">
          <h3 data-i18n="settings.debug.db.title"><?php echo htmlspecialchars($t('settings.debug.db.title', 'Database'), ENT_QUOTES); ?></h3>
          <div class="grid">
            <div>
              <div class="lbl" data-i18n="settings.debug.db.tables"><?php echo htmlspecialchars($t('settings.debug.db.tables', 'Tables'), ENT_QUOTES); ?></div>
              <div id="dbgDbTables">-</div>
            </div>
            <div>
              <div class="lbl" data-i18n="settings.debug.db.size"><?php echo htmlspecialchars($t('settings.debug.db.size', 'Size'), ENT_QUOTES); ?></div>
              <div id="dbgDbSize">-</div>
            </div>
            <div>
              <div class="lbl">DB</div>
              <div id="dbgDbName">-</div>
            </div>
            <div>
              <div class="lbl">User</div>
              <div id="dbgDbUser">-</div>
            </div>
          </div>
          <div class="card">
            <?php
            $id = 'dbgDbTablesList';
            $tableClass = 'table';
            $headers = [
              ['label' => 'Table'],
              ['label' => 'Rows'],
            ];
            $footer = false;
            require __DIR__ . '/../partials/table.php';
            ?>
            <div id="dbgDbTablesStatus" class="hint"></div>
            <pre id="dbgDbTablePreview" class="pre"></pre>
          </div>
          <input id="dbgResetConfirm" type="text" placeholder="<?php echo htmlspecialchars($t('settings.debug.db.ph', 'Type RESET DATABASE'), ENT_QUOTES); ?>">
          <button id="btnResetDb" class="btn danger" data-i18n="settings.debug.db.reset_btn"<?php echo empty($allowDbReset) ? ' disabled' : ''; ?>><?php echo htmlspecialchars($t('settings.debug.db.reset_btn', 'Reset DB'), ENT_QUOTES); ?></button>
          <pre id="dbgResetOut" class="pre"></pre>
        </div>

        <div class="card">
          <h3 data-i18n="settings.debug.dump.title"><?php echo htmlspecialchars($t('settings.debug.dump.title', 'Database dump'), ENT_QUOTES); ?></h3>
          <div class="row">
            <button id="btnDbDump" class="btn" data-i18n="settings.debug.dump.download"><?php echo htmlspecialchars($t('settings.debug.dump.download', 'Download dump'), ENT_QUOTES); ?></button>
            <input id="dbgRestoreFile" type="file" accept=".sql">
            <button id="btnDbRestore" class="btn danger" data-i18n="settings.debug.dump.restore"<?php echo empty($allowDbReset) ? ' disabled' : ''; ?>><?php echo htmlspecialchars($t('settings.debug.dump.restore', 'Restore dump'), ENT_QUOTES); ?></button>
          </div>
          <pre id="dbgDumpOut" class="pre"></pre>
        </div>
      </div>

    </section>
  </main>

  <div id="ruleModal" class="modal" style="display:none;">
    <div class="modal-card">
      <div class="modal-head">
        <div class="modal-title" id="ruleModalTitle">Rule</div>
        <button id="ruleModalClose" class="btn" data-i18n="common.close"><?php echo htmlspecialchars($t('common.close', 'Close'), ENT_QUOTES); ?></button>
      </div>
      <div class="modal-body" id="ruleModalBody"></div>
      <div class="modal-foot">
        <button id="ruleModalCancel" class="btn" data-i18n="common.close"><?php echo htmlspecialchars($t('common.close', 'Close'), ENT_QUOTES); ?></button>
        <button id="ruleModalSave" class="btn primary" data-i18n="common.save"><?php echo htmlspecialchars($t('common.save', 'Save'), ENT_QUOTES); ?></button>
      </div>
    </div>
  </div>

  <script>
    window.I18N = <?php echo json_encode($dict, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); ?>;
    window.APP_LANG = <?php echo json_encode($lang, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); ?>;
    window.DEBUG_TOOLS_ENABLED = <?php echo !empty($debugToolsEnabled) ? 'true' : 'false'; ?>;
  </script>
  <script src="/assets/js/app-config.js"></script>
  <script src="/assets/js/table.js"></script>
  <script src="/assets/js/ui.js"></script>
  <script src="/assets/js/api.js"></script>
  <script src="/assets/js/prefs.js"></script>
  <script src="/assets/js/auth.js"></script>
  <script src="/assets/js/sse.js"></script>
  <script src="/assets/js/http.js"></script>
  <script src="/assets/js/settings.js"></script>
  <script src="/assets/js/admin.js"></script>
</body>
</html>
