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

$totalCouples = $db->query("SELECT COUNT(*) FROM couples WHERE status='approved'")->fetchColumn();
$pendingCouples = $db->query("SELECT COUNT(*) FROM couples WHERE status='pending'")->fetchColumn();
$rejectedCouples = $db->query("SELECT COUNT(*) FROM couples WHERE status='rejected'")->fetchColumn();
$totalVideos = $db->query("SELECT COUNT(*) FROM videos")->fetchColumn();

$recent = $db->query("SELECT * FROM couples ORDER BY created_at DESC LIMIT 10")->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">
  <!-- Sidebar -->
  <aside class="admin-sidebar">
    <div class="logo">💕 তজিয়া গ্রুপ</div>
    <nav>
      <a href="dashboard.php" class="active"><i class="fas fa-tachometer-alt"></i> ড্যাশবোর্ড</a>
      <a href="couples.php"><i class="fas fa-users"></i> সব কাপল</a>
      <a href="pending.php"><i class="fas fa-clock"></i> অপেক্ষমাণ <span class="badge badge-pending" style="margin-left:auto;"><?= $pendingCouples ?></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">
    <!-- Topbar -->
    <div class="admin-topbar">
      <div>
        <button class="admin-sidebar-toggle" id="adminSidebarToggle"><i class="fas fa-bars"></i></button>
        <span style="font-weight:600;">ড্যাশবোর্ড</span>
      </div>
      <div style="color:var(--text-muted);">👋 স্বাগতম, <?= $currentAdmin ?></div>
    </div>

    <div class="admin-content">
      <!-- Stats Cards -->
      <div class="admin-cards">
        <div class="admin-card">
          <p>💑 মোট কাপল</p>
          <h3><?= bnNum($totalCouples) ?></h3>
        </div>
        <div class="admin-card">
          <p>⏳ অপেক্ষমাণ</p>
          <h3><?= bnNum($pendingCouples) ?></h3>
        </div>
        <div class="admin-card">
          <p>❌ বাতিলকৃত</p>
          <h3><?= bnNum($rejectedCouples) ?></h3>
        </div>
        <div class="admin-card">
          <p>🎬 মোট ভিডিও</p>
          <h3><?= bnNum($totalVideos) ?></h3>
        </div>
      </div>

      <!-- Recent Submissions -->
      <div class="glass">
        <h3 style="margin-bottom:16px;">📋 সাম্প্রতিক জমা</h3>
        <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 ($recent as $r): ?>
              <tr>
                <td>
                  <?php if ($r['photo']): ?>
                  <img src="<?= UPLOAD_URL . e($r['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($r['boy_name']) ?></strong> ❤ <strong><?= e($r['girl_name']) ?></strong></td>
                <td><?= e($r['district'] ?? '—') ?></td>
                <td>
                  <span class="badge badge-<?= $r['status'] ?>"><?= $r['status'] === 'approved' ? 'অনুমোদিত' : ($r['status'] === 'pending' ? 'অপেক্ষমাণ' : 'বাতিল') ?></span>
                </td>
                <td style="color:var(--text-muted);font-size:.85rem;"><?= bnTimeAgo($r['created_at']) ?></td>
                <td>
                  <a href="couples.php?edit=<?= $r['id'] ?>" class="btn btn-sm btn-outline"><i class="fas fa-edit"></i></a>
                </td>
              </tr>
              <?php endforeach; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</div>
<div class="toast-container"></div>
<script src="<?= SITE_URL ?>/assets/js/admin.js"></script>
</body>
</html>
