<?php
/**
 * Shared sidebar for admin app (brand header + nav + folder config tabs).
 * Include after <body class="admin-app">. Styles live in admin_layout_styles.php.
 *
 * Optional vars from parent page:
 *   $admin_current_nav  — highlight nav item
 *   $admin_app_title    — sidebar title
 *   $admin_config_id    — current configuration id (configuration_detail.php)
 *   $admin_folder_id    — folder id for sibling config tabs
 */

if (!function_exists('h')) {
    function h($v) { return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); }
}

$admin_current_nav = $admin_current_nav ?? 'folders';
$admin_app_title = $admin_app_title ?? 'Door Configurator';
$admin_config_id = isset($admin_config_id) ? (int)$admin_config_id : 0;
$admin_folder_id = isset($admin_folder_id) ? (int)$admin_folder_id : 0;
$admin_folder_configs = [];
$admin_folder_name = '';

if ($admin_folder_id > 0 && isset($pdo)) {
    $stmt = $pdo->prepare("SELECT name FROM door_folders WHERE id = ? LIMIT 1");
    $stmt->execute([$admin_folder_id]);
    $admin_folder_name = trim((string)($stmt->fetchColumn() ?: ''));

    $stmt = $pdo->prepare("SELECT id, options_json FROM door_configurations WHERE folder_id = ? ORDER BY id ASC");
    $stmt->execute([$admin_folder_id]);
    $admin_folder_configs = $stmt->fetchAll(PDO::FETCH_ASSOC);
}

$admin_show_config_tabs = count($admin_folder_configs) > 1
    || ($admin_config_id === 0 && count($admin_folder_configs) > 0);

$admin_nav_items = [
    'folders' => [
        'label' => 'Projects',
        'url' => 'folders.php',
        'icon' => '<svg class="admin-sidebar-link-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 7.5V19a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V7.5"/><path d="M3 7.5 12 3l9 4.5"/><path d="M7 22V12h10v10"/></svg>',
    ],
];
?>
<aside
  class="admin-sidebar"
  id="admin-sidebar"
  aria-label="Admin navigation"
  data-has-config-tabs="<?= $admin_show_config_tabs ? '1' : '0' ?>"
>
  <div class="admin-sidebar-header">
    <span class="admin-sidebar-icon" aria-hidden="true">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round">
        <path d="M4 4h16v16H4z"/>
        <path d="M4 9h16"/>
        <path d="M9 4v16"/>
        <circle cx="15" cy="15" r="2"/>
      </svg>
    </span>
    <span class="admin-sidebar-title"><?= h($admin_app_title) ?></span>
    <button type="button" class="admin-sidebar-toggle" id="admin-sidebar-toggle" aria-label="Collapse sidebar" title="Collapse sidebar">
      <svg class="admin-sidebar-toggle-icon admin-sidebar-toggle-icon--expand" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M15 18l-6-6 6-6"/></svg>
      <svg class="admin-sidebar-toggle-icon admin-sidebar-toggle-icon--collapse" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M9 18l6-6-6-6"/></svg>
    </button>
  </div>

  <div class="admin-sidebar-body">
    <nav class="admin-sidebar-nav" aria-label="Main menu">
      <p class="admin-sidebar-section-label">Menu</p>
      <?php foreach ($admin_nav_items as $key => $item): ?>
        <a
          href="<?= h($item['url']) ?>"
          class="admin-sidebar-link<?= $admin_current_nav === $key ? ' admin-sidebar-link-active' : '' ?>"
          title="<?= h($item['label']) ?>"
        >
          <?= $item['icon'] ?>
          <span class="admin-sidebar-link-text"><?= h($item['label']) ?></span>
        </a>
      <?php endforeach; ?>
    </nav>

    <?php if ($admin_show_config_tabs): ?>
    <div class="admin-sidebar-folder-configs">
      <p class="admin-sidebar-section-label">
        <?php if ($admin_folder_name !== ''): ?>
          <span class="admin-sidebar-section-label__text"><?= h($admin_folder_name) ?></span>
        <?php else: ?>
          <span class="admin-sidebar-section-label__text">Configurations</span>
        <?php endif; ?>
        <span class="admin-sidebar-section-count"><?= count($admin_folder_configs) ?></span>
      </p>
      <div class="admin-sidebar-config-tabs" role="list">
        <?php foreach ($admin_folder_configs as $fc):
          $fc_id = (int)$fc['id'];
          $fc_opts = json_decode($fc['options_json'] ?? '', true) ?: [];
          $fc_product = trim((string)($fc_opts['_product_title'] ?? ''));
          $is_active = ($admin_config_id > 0 && $fc_id === $admin_config_id);
          $tab_title = $fc_product !== '' ? $fc_product : ('Configuration #' . $fc_id);
        ?>
          <?php if ($is_active): ?>
            <span
              class="admin-sidebar-config-tab admin-sidebar-config-tab--active"
              role="listitem"
              title="<?= h($tab_title) ?>"
            >
              <span class="admin-sidebar-config-tab__id">#<?= h($fc_id) ?></span>
              <span class="admin-sidebar-config-tab__title"><?= h($tab_title) ?></span>
            </span>
          <?php else: ?>
            <a
              class="admin-sidebar-config-tab"
              role="listitem"
              href="configuration_detail.php?id=<?= h($fc_id) ?>&folder_id=<?= h($admin_folder_id) ?>"
              title="<?= h($tab_title) ?>"
            >
              <span class="admin-sidebar-config-tab__id">#<?= h($fc_id) ?></span>
              <span class="admin-sidebar-config-tab__title"><?= h($tab_title) ?></span>
            </a>
          <?php endif; ?>
        <?php endforeach; ?>
      </div>
      <?php if ($admin_folder_id > 0): ?>
      <a class="admin-sidebar-back-link" href="folder_detail.php?id=<?= h($admin_folder_id) ?>" title="Back to project">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M15 18l-6-6 6-6"/></svg>
        <span>Back to project</span>
      </a>
      <?php endif; ?>
    </div>
    <?php endif; ?>
  </div>

  <div class="admin-sidebar-footer">
    <span class="admin-sidebar-brand">Vintage Doors</span>
  </div>
</aside>

<script>
(function() {
  var KEY = 'admin-sidebar-full';
  var sidebar = document.getElementById('admin-sidebar');
  var btn = document.getElementById('admin-sidebar-toggle');
  if (!sidebar || !btn) return;

  function isFull() {
    var stored = localStorage.getItem(KEY);
    if (stored === null) return true;
    return stored !== '0';
  }

  function setFull(full) {
    localStorage.setItem(KEY, full ? '1' : '0');
    sidebar.classList.toggle('admin-sidebar--collapsed', !full);
    btn.setAttribute('aria-label', full ? 'Collapse sidebar' : 'Expand sidebar');
    btn.setAttribute('title', full ? 'Collapse sidebar' : 'Expand sidebar');
  }

  setFull(isFull());
  btn.addEventListener('click', function() {
    setFull(sidebar.classList.contains('admin-sidebar--collapsed'));
  });
})();
</script>
