<?php
require_once 'auth_guard.php';
$pageTitle = 'لوحة التحكم';

// Summary stats
$total_places    = db()->query("SELECT COUNT(*) FROM places WHERE is_active=1")->fetchColumn();
$total_users     = db()->query("SELECT COUNT(*) FROM users")->fetchColumn();
$pending_prov    = db()->query("SELECT COUNT(*) FROM providers WHERE status='pending'")->fetchColumn();
$unread_msgs     = db()->query("SELECT COUNT(*) FROM messages WHERE is_read=0")->fetchColumn();
$total_providers = db()->query("SELECT COUNT(*) FROM providers WHERE status='approved'")->fetchColumn();

// Latest messages
$msgs = db()->query("SELECT * FROM messages ORDER BY created_at DESC LIMIT 5")->fetchAll();

// Latest providers pending
$pending = db()->query("SELECT p.*,u.username FROM providers p JOIN users u ON u.id=p.user_id WHERE p.status='pending' ORDER BY p.created_at DESC LIMIT 5")->fetchAll();

$act_icons = ['restaurant'=>'🍽️','hotel'=>'🏨','guide'=>'🗺️','craft'=>'🛍️','agency'=>'✈️','transport'=>'🚲'];

include 'header.php';
?>

<div class="page-header">
  <div class="page-title">مرحباً بك 👋</div>
  <div class="page-sub">لوحة تحكم منصة تحويسة — نظرة عامة على المحتوى</div>
</div>

<!-- Stats -->
<div class="stats-row">
  <div class="stat-card">
    <div class="stat-icon">🗺️</div>
    <div class="stat-num"><?= $total_places ?></div>
    <div class="stat-label">مكان سياحي</div>
  </div>
  <div class="stat-card">
    <div class="stat-icon">👥</div>
    <div class="stat-num"><?= $total_users ?></div>
    <div class="stat-label">مستخدم مسجل</div>
  </div>
  <div class="stat-card">
    <div class="stat-icon">🏪</div>
    <div class="stat-num"><?= $total_providers ?></div>
    <div class="stat-label">خدمة مفعّلة</div>
  </div>
  <div class="stat-card">
    <div class="stat-icon">✉️</div>
    <div class="stat-num"><?= $unread_msgs ?></div>
    <div class="stat-label">رسالة غير مقروءة</div>
  </div>
</div>

<div style="display:grid;gap:20px" class="dash-grid">

  <!-- Pending providers -->
  <?php if ($pending_prov > 0): ?>
  <div class="table-wrap">
    <div class="table-head">
      <span class="table-head-title">⏳ طلبات بانتظار الموافقة (<?= $pending_prov ?>)</span>
      <a href="activities.php?filter=pending" class="btn btn-gold btn-sm">عرض الكل</a>
    </div>
    <div class="tbl-scroll">
      <table class="tbl">
        <thead><tr>
          <th>النشاط</th><th>النوع</th><th>المستخدم</th><th>التاريخ</th><th>إجراء</th>
        </tr></thead>
        <tbody>
        <?php foreach ($pending as $p): ?>
          <tr>
            <td>
              <div style="display:flex;align-items:center;gap:8px">
                <?php if ($p['image']): ?>
                  <img src="../<?= htmlspecialchars($p['image']) ?>" class="thumb" style="border-radius:8px">
                <?php else: ?>
                  <div class="thumb"><?= $act_icons[$p['activity_type']] ?? '🏪' ?></div>
                <?php endif; ?>
                <div>
                  <div style="font-weight:700"><?= htmlspecialchars($p['business_name']) ?></div>
                  <div style="font-size:.75rem;color:var(--muted)"><?= htmlspecialchars($p['phone']) ?></div>
                </div>
              </div>
            </td>
            <td><?= $act_icons[$p['activity_type']] ?? '' ?> <?= htmlspecialchars($p['activity_type']) ?></td>
            <td><?= htmlspecialchars($p['username']) ?></td>
            <td style="font-size:.78rem;color:var(--muted)"><?= date('d/m/Y', strtotime($p['created_at'])) ?></td>
            <td>
              <div style="display:flex;gap:6px">
                <form method="POST" action="activities.php" style="display:inline">
                  <input type="hidden" name="id" value="<?= $p['id'] ?>">
                  <input type="hidden" name="action" value="approve">
                  <button type="submit" class="btn btn-primary btn-sm">✅ قبول</button>
                </form>
                <form method="POST" action="activities.php" style="display:inline">
                  <input type="hidden" name="id" value="<?= $p['id'] ?>">
                  <input type="hidden" name="action" value="reject">
                  <button type="submit" class="btn btn-danger btn-sm">❌ رفض</button>
                </form>
              </div>
            </td>
          </tr>
        <?php endforeach; ?>
        </tbody>
      </table>
    </div>
  </div>
  <?php endif; ?>

  <!-- Latest messages -->
  <div class="table-wrap">
    <div class="table-head">
      <span class="table-head-title">✉️ آخر الرسائل</span>
      <a href="messages.php" class="btn btn-ghost btn-sm">عرض الكل</a>
    </div>
    <div class="tbl-scroll">
      <table class="tbl">
        <thead><tr>
          <th>الاسم</th><th>الرسالة</th><th>التاريخ</th><th>الحالة</th>
        </tr></thead>
        <tbody>
        <?php foreach ($msgs as $m): ?>
          <tr>
            <td>
              <div style="font-weight:700"><?= htmlspecialchars($m['name']) ?></div>
              <div style="font-size:.75rem;color:var(--muted)"><?= htmlspecialchars($m['email']) ?></div>
            </td>
            <td style="max-width:200px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis">
              <?= htmlspecialchars(mb_substr($m['message'],0,60)) ?>…
            </td>
            <td style="font-size:.78rem;color:var(--muted)"><?= date('d/m/Y', strtotime($m['created_at'])) ?></td>
            <td>
              <?php if ($m['is_read']): ?>
                <span class="badge badge-green">مقروءة</span>
              <?php else: ?>
                <span class="badge badge-gold">جديدة</span>
              <?php endif; ?>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (!$msgs): ?>
          <tr><td colspan="4" style="text-align:center;color:var(--muted);padding:24px">لا توجد رسائل بعد.</td></tr>
        <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>

</div>

<?php include 'footer.php'; ?>
