<?php
// superadmin/projects.php
session_start();
require_once '../includes/config.php';
require_once '../includes/sa_auth.php';
require_once '../includes/db.php';
requireSuperAdmin();

$pdo      = getDB();
$projects = $pdo->query("SELECT p.*, 
  (SELECT COUNT(*) FROM invitees iv JOIN invitations i ON iv.invitation_id=i.id WHERE i.project_id=p.id) as total,
  (SELECT COUNT(*) FROM invitees iv JOIN invitations i ON iv.invitation_id=i.id WHERE i.project_id=p.id AND iv.response_status='Accept') as accepted,
  (SELECT COUNT(*) FROM invitees iv JOIN invitations i ON iv.invitation_id=i.id WHERE i.project_id=p.id AND iv.response_status='Decline') as declined
  FROM projects p ORDER BY created_at DESC")->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>All Projects</title>
  <link rel="stylesheet" href="sa.css"/>
</head>
<body>
<div class="sa-wrapper">
  <?php include 'sidebar.php'; ?>
  <div class="sa-main">
    <?php include 'topbar.php'; ?>
    <div class="sa-content">

      <div class="card">
        <div class="card-header">
          <span class="card-title">All Projects (<?= count($projects) ?>)</span>
          <a href="create-project.php" class="btn btn-primary btn-sm">+ New Project</a>
        </div>
        <table class="sa-table">
          <thead>
            <tr>
              <th>Project</th>
              <th>Wedding Date</th>
              <th>Invitees</th>
              <th>Accepted</th>
              <th>Declined</th>
              <th>Pending</th>
              <th>Actions</th>
            </tr>
          </thead>
          <tbody>
            <?php if (empty($projects)): ?>
              <tr><td colspan="7" style="text-align:center;color:var(--text-muted);padding:40px;">No projects yet.</td></tr>
            <?php else: foreach ($projects as $p):
              $pending = $p['total'] - $p['accepted'] - $p['declined'];
            ?>
              <tr>
                <td>
                  <strong style="color:var(--gold)"><?= htmlspecialchars($p['groom_name']) ?> &amp; <?= htmlspecialchars($p['bride_name']) ?></strong><br/>
                  <span style="font-size:12px;color:var(--text-muted);"><?= htmlspecialchars($p['directory_name']) ?></span>
                </td>
                <td><?= date('M j, Y', strtotime($p['wedding_datetime'])) ?></td>
                <td><?= $p['total'] ?></td>
                <td><span class="badge-status badge-accept"><?= $p['accepted'] ?></span></td>
                <td><span class="badge-status badge-decline"><?= $p['declined'] ?></span></td>
                <td><span class="badge-status badge-pending"><?= $pending ?></span></td>
                <td>
                  <div style="display:flex;gap:6px;flex-wrap:wrap;">
                    <a href="edit-project.php?id=<?= $p['id'] ?>" class="btn btn-outline btn-sm">Edit</a>
                    <a href="manage-invitees.php?id=<?= $p['id'] ?>" class="btn btn-outline btn-sm">Invitees</a>
                    <a href="rsvp-report.php?id=<?= $p['id'] ?>" class="btn btn-outline btn-sm">RSVP</a>
                    <a href="<?= PROJECTS_URL ?>/<?= urlencode($p['directory_name']) ?>/" class="btn btn-outline btn-sm" target="_blank">↗</a>
                  </div>
                </td>
              </tr>
            <?php endforeach; endif; ?>
          </tbody>
        </table>
      </div>

    </div>
  </div>
</div>
</body>
</html>
