<?php
/**
 * Admin: list all folders (with config count). Link to folder detail.
 */
require __DIR__ . '/config.php';
if (is_file(__DIR__ . '/hubspot-config.php')) {
    require_once __DIR__ . '/hubspot-config.php';
}
require_once __DIR__ . '/admin_config_helpers.php';

 header_remove('X-Frame-Options');
header('Content-Security-Policy: frame-ancestors https://admin.shopify.com https://*.myshopify.com');


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

$stmt = $pdo->query("
  SELECT f.id, f.name, f.customer_email, f.created_at,
         COUNT(c.id) AS config_count
  FROM door_folders f
  LEFT JOIN door_configurations c ON c.folder_id = f.id
  GROUP BY f.id
  ORDER BY f.created_at DESC
");
$folders = $stmt->fetchAll(PDO::FETCH_ASSOC);

$contact_saved = isset($_GET['contact_saved']) && $_GET['contact_saved'] === '1';
$contact_error = isset($_GET['contact_error']) ? trim((string) $_GET['contact_error']) : '';
$contact_open_id = isset($_GET['contact_open']) ? (int) $_GET['contact_open'] : 0;

$admin_current_nav = 'folders';
$admin_app_title = 'Door projects';
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Folders · Door Configurator</title>
  <?php include __DIR__ . '/includes/admin_layout_styles.php'; ?>
  <?php include __DIR__ . '/includes/folder_contact_styles.php'; ?>
</head>
<body class="admin-app">
  <?php include __DIR__ . '/includes/admin_sidebar.php'; ?>
  <main class="admin-main">
  <div class="admin-wrap">
    <header class="admin-page-header admin-page-header--folders">
      <div class="admin-page-header__content">
        <span class="admin-page-eyebrow">Door Configurator</span>
        <h1 class="admin-page-title">Projects</h1>
        <p class="admin-page-meta">Browse and manage all door configurator projects and customer contacts.</p>
      </div>
    </header>

    <?php if ($contact_saved): ?>
    <div class="admin-alert admin-alert--success">
      <div><strong>Contact saved.</strong> Shopify customer updated and HubSpot contact synced.</div>
    </div>
    <?php endif; ?>
    <?php if ($contact_error !== ''): ?>
    <div class="admin-alert admin-alert--error">
      <div><strong>Contact not saved.</strong> <?= h($contact_error) ?></div>
    </div>
    <?php endif; ?>

    <div class="admin-card admin-card--table">
      <div class="admin-card-header">
        <p class="admin-card-title">All projects</p>
      </div>
      <?php if (!empty($folders)): ?>
      <div class="admin-table-wrap">
        <table class="admin-table admin-table--hover">
          <thead>
            <tr>
              <th>Name</th>
              <th>Customer email</th>
              <th class="admin-col-center">Configs</th>
              <th>Created/Updated</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($folders as $f): ?>
              <tr>
                <td class="admin-folder-name-cell">
                  <a href="../admin/folder_detail.php?id=<?= h($f['id']) ?>"><?= h($f['name']) ?></a>
                  <?php
                  $folder = admin_load_folder_contact_for_display($f);
                  $return_to = 'folders';
                  include __DIR__ . '/includes/folder_contact_panel.php';
                  ?>
                </td>
                <td class="admin-email-cell"><?= h($f['customer_email'] ?? '—') ?></td>
                <td class="admin-col-center"><span class="admin-pill admin-pill--finalized"><?= h($f['config_count']) ?></span></td>
                <td class="admin-date"><?= h($f['created_at']) ?></td>
              </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>
      <?php else: ?>
        <div class="admin-empty admin-empty--icon">
          <div class="admin-empty-icon" aria-hidden="true"></div>
          <p>No projects yet.</p>
          <p class="admin-empty-hint">Projects appear here when customers save configurations.</p>
        </div>
      <?php endif; ?>
    </div>
  </div>
  </main>
</body>
</html>
