<?php
require_once 'includes/auth.php';
$db = getDB();
$csrf = generateCSRFToken();

$pendingCount = $db->query("SELECT COUNT(*) FROM couples WHERE status='pending'")->fetchColumn();
$pending = $db->query("SELECT * FROM couples WHERE status='pending' ORDER BY created_at DESC")->fetchAll();
$currentAdmin = e($_SESSION['admin_name'] ?? 'Admin');
?>
<!DOCTYPE html>
<html lang="bn" data-theme="dark">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>অপেক্ষমাণ — তজিয়া গ্রুপ</title>
  <link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@400;600;700&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
  <link rel="stylesheet" href="<?= SITE_URL ?>/assets/css/style.css">
</head>
<body>
<div class="admin-wrap">
  <aside class="admin-sidebar">
    <div class="logo">💕 তজিয়া গ্রুপ</div>
    <nav>
      <a href="dashboard.php"><i class="fas fa-tachometer-alt"></i> ড্যাশবোর্ড</a>
      <a href="couples.php"><i class="fas fa-users"></i> সব কাপল</a>
      <a href="pending.php" class="active"><i class="fas fa-clock"></i> অপেক্ষমাণ <span class="badge badge-pending" style="margin-left:auto;"><?= $pendingCount ?></span></a>
      <a href="settings.php"><i class="fas fa-cog"></i> সেটিংস</a>
      <a href="<?= SITE_URL ?>" target="_blank"><i class="fas fa-external-link-alt"></i> সাইট দেখুন</a>
      <a href="logout.php"><i class="fas fa-sign-out-alt"></i> লগআউট</a>
    </nav>
  </aside>

  <div class="admin-main">
    <div class="admin-topbar">
      <div><button class="admin-sidebar-toggle" id="adminSidebarToggle"><i class="fas fa-bars"></i></button> <span style="font-weight:600;">অপেক্ষমাণ কাপল (<?= bnNum($pendingCount) ?>)</span></div>
      <div style="color:var(--text-muted);">👋 <?= $currentAdmin ?></div>
    </div>

    <div class="admin-content">
      <?php if ($pending): ?>
      <div class="glass">
        <div style="overflow-x:auto;">
          <table class="admin-table">
            <thead>
              <tr><th>ছবি</th><th>কাপল</th><th>জেলা</th><th>গল্প</th><th>জমার তারিখ</th><th>অ্যাকশন</th></tr>
            </thead>
            <tbody>
              <?php foreach ($pending as $p): ?>
              <tr>
                <td>
                  <?php if ($p['photo']): ?><img src="<?= UPLOAD_URL . e($p['photo']) ?>" alt="">
                  <?php else: ?><div style="width:50px;height:50px;background:var(--gradient);border-radius:8px;display:flex;align-items:center;justify-content:center;">💑</div><?php endif; ?>
                </td>
                <td><strong><?= e($p['boy_name']) ?></strong> ❤ <strong><?= e($p['girl_name']) ?></strong></td>
                <td><?= e($p['district'] ?? '—') ?></td>
                <td style="max-width:200px;color:var(--text-muted);font-size:.85rem;"><?= e(truncate($p['short_story'], 60)) ?></td>
                <td style="color:var(--text-muted);font-size:.85rem;"><?= bnTimeAgo($p['created_at']) ?></td>
                <td style="white-space:nowrap;">
                  <button class="btn btn-sm btn-success quick-action" data-action="approve" data-id="<?= $p['id'] ?>" data-csrf="<?= $csrf ?>"><i class="fas fa-check"></i> অনুমোদন</button>
                  <button class="btn btn-sm btn-danger quick-action" data-action="reject" data-id="<?= $p['id'] ?>" data-csrf="<?= $csrf ?>"><i class="fas fa-times"></i> বাতিল</button>
                  <a href="couples.php?edit=<?= $p['id'] ?>" class="btn btn-sm btn-outline"><i class="fas fa-eye"></i></a>
                </td>
              </tr>
              <?php endforeach; ?>
            </tbody>
          </table>
        </div>
      </div>
      <?php else: ?>
      <div class="glass" style="text-align:center;padding:48px;">
        <p style="font-size:3rem;">🎉</p>
        <h3>সব পরিষ্কার!</h3>
        <p style="color:var(--text-muted);">কোনো অপেক্ষমাণ কাপল নেই।</p>
      </div>
      <?php endif; ?>
    </div>
  </div>
</div>
<div class="toast-container"></div>
<script src="<?= SITE_URL ?>/assets/js/admin.js"></script>
</body>
</html>
