<?php
require_once __DIR__ . '/../config.php';
requireAdmin();
$db = getDB();

$total_products  = $db->query("SELECT COUNT(*) FROM products")->fetchColumn();
$total_cats      = $db->query("SELECT COUNT(*) FROM categories")->fetchColumn();
$total_inquiries = $db->query("SELECT COUNT(*) FROM inquiries")->fetchColumn();
$new_inquiries   = $db->query("SELECT COUNT(*) FROM inquiries WHERE status='new'")->fetchColumn();
$recent_inquiries = $db->query("SELECT * FROM inquiries ORDER BY created_at DESC LIMIT 8")->fetchAll();
$recent_products  = $db->query("SELECT p.*,c.name AS cat FROM products p LEFT JOIN categories c ON p.category_id=c.id ORDER BY p.id DESC LIMIT 5")->fetchAll();

$page_title = 'Dashboard';
include __DIR__ . '/header.php';
?>
<div class="stats-grid">
  <div class="stat-card"><div class="stat-icon blue"><i class="fas fa-box"></i></div><div><h3><?= $total_products ?></h3><p>Total Products</p></div></div>
  <div class="stat-card" style="border-color:var(--success);"><div class="stat-icon green"><i class="fas fa-tags"></i></div><div><h3><?= $total_cats ?></h3><p>Categories</p></div></div>
  <div class="stat-card" style="border-color:var(--warning);"><div class="stat-icon gold"><i class="fas fa-envelope"></i></div><div><h3><?= $total_inquiries ?></h3><p>Total Inquiries</p></div></div>
  <div class="stat-card" style="border-color:var(--danger);"><div class="stat-icon red"><i class="fas fa-bell"></i></div><div><h3><?= $new_inquiries ?></h3><p>New Inquiries</p></div></div>
</div>

<div style="display:grid;grid-template-columns:1.4fr 1fr;gap:24px;">
  <!-- Recent Inquiries -->
  <div class="card">
    <div class="card-header">
      <h2>Recent Inquiries</h2>
      <a href="inquiries.php" style="font-size:.82rem;color:var(--blue-main);">View All →</a>
    </div>
    <div class="table-wrap">
      <table>
        <thead><tr><th>Name</th><th>Phone</th><th>Date</th><th>Status</th><th>Action</th></tr></thead>
        <tbody>
          <?php foreach ($recent_inquiries as $inq): ?>
          <tr>
            <td><strong><?= h($inq['name']) ?></strong><br><span style="font-size:.75rem;color:var(--text-light);"><?= h($inq['company']) ?></span></td>
            <td><?= h($inq['phone']) ?></td>
            <td><?= date('d M Y', strtotime($inq['created_at'])) ?></td>
            <td><span class="badge badge-<?= $inq['status'] ?>"><?= ucfirst($inq['status']) ?></span></td>
            <td><a href="inquiries.php?view=<?= $inq['id'] ?>" class="btn-action btn-view">View</a></td>
          </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    </div>
  </div>

  <!-- Quick Add & Recent Products -->
  <div>
    <div class="card" style="margin-bottom:20px;">
      <div class="card-header"><h2>Quick Actions</h2></div>
      <div class="card-body" style="display:flex;flex-direction:column;gap:10px;">
        <a href="products.php?action=add" class="btn-primary" style="text-align:center;padding:11px;">+ Add New Product</a>
        <a href="categories.php?action=add" class="btn-primary" style="text-align:center;padding:11px;background:var(--success);">+ Add Category</a>
        <a href="inquiries.php" class="btn-primary" style="text-align:center;padding:11px;background:var(--warning);">📥 View Inquiries</a>
      </div>
    </div>
    <div class="card">
      <div class="card-header"><h2>Recent Products</h2><a href="products.php" style="font-size:.82rem;color:var(--blue-main);">All →</a></div>
      <div style="padding:10px 0;">
        <?php foreach ($recent_products as $pr): ?>
        <div style="display:flex;align-items:center;gap:12px;padding:10px 18px;border-bottom:1px solid #f1f5f9;">
          <img src="<?= SITE_URL ?>/assets/images/<?= h($pr['image']) ?>" alt="" class="img-preview">
          <div style="flex:1;min-width:0;">
            <div style="font-weight:600;font-size:.88rem;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;"><?= h($pr['name']) ?></div>
            <div style="font-size:.75rem;color:var(--text-light);"><?= h($pr['cat']) ?> | ₹<?= number_format($pr['price']) ?></div>
          </div>
          <a href="products.php?action=edit&id=<?= $pr['id'] ?>" class="btn-action btn-edit">Edit</a>
        </div>
        <?php endforeach; ?>
      </div>
    </div>
  </div>
</div>

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