<?php
$page_title = 'Dashboard';
require_once __DIR__ . '/header.php';

// Fetch quick stats
$total_orders = $db->query("SELECT COUNT(*) FROM orders WHERE customer_id={$dealer['id']}")->fetchColumn();
$recent_orders = $db->query("SELECT * FROM orders WHERE customer_id={$dealer['id']} ORDER BY created_at DESC LIMIT 5")->fetchAll();
?>

<!-- Top Stats Grid -->
<div style="display:grid;grid-template-columns:repeat(auto-fit, minmax(200px, 1fr));gap:24px;margin-bottom:24px;">
  <!-- Total Outstanding -->
  <div class="card" style="margin:0;display:flex;flex-direction:column;gap:8px;">
    <span style="font-size:1.5rem;color:#2557d6;"><i class="fas fa-wallet"></i></span>
    <h3 style="margin:0;font-size:1.5rem;color:#0f172a;">₹<?= number_format($dealer['outstanding_balance']) ?></h3>
    <span style="font-size:.85rem;color:#64748b;">Total Outstanding</span>
  </div>
  
  <!-- Available Credit -->
  <div class="card" style="margin:0;display:flex;flex-direction:column;gap:8px;">
    <span style="font-size:1.5rem;color:#16a34a;"><i class="fas fa-credit-card"></i></span>
    <h3 style="margin:0;font-size:1.5rem;color:#0f172a;">₹<?= number_format(max(0, $dealer['credit_limit'] - $dealer['outstanding_balance'])) ?></h3>
    <span style="font-size:.85rem;color:#64748b;">Available Credit</span>
  </div>

  <!-- Active Orders -->
  <div class="card" style="margin:0;display:flex;flex-direction:column;gap:8px;">
    <span style="font-size:1.5rem;color:#8b5cf6;"><i class="fas fa-box"></i></span>
    <h3 style="margin:0;font-size:1.5rem;color:#0f172a;">12</h3>
    <span style="font-size:.85rem;color:#64748b;">Active Orders</span>
  </div>

  <!-- Loyalty Points -->
  <div class="card" style="margin:0;display:flex;flex-direction:column;gap:8px;">
    <span style="font-size:1.5rem;color:#f59e0b;"><i class="fas fa-star"></i></span>
    <h3 style="margin:0;font-size:1.5rem;color:#0f172a;">580</h3>
    <span style="font-size:.85rem;color:#64748b;">Loyalty Points</span>
  </div>
</div>

<!-- Announcements -->
<div class="card" style="display:flex;justify-content:space-between;align-items:center;background:#fffbeb;border:1px solid #fde68a;">
  <div style="display:flex;align-items:center;gap:16px;">
    <div style="width:40px;height:40px;background:#fef3c7;color:#d97706;border-radius:50%;display:flex;align-items:center;justify-content:center;font-size:1.2rem;"><i class="fas fa-bell"></i></div>
    <div>
      <h4 style="margin:0;color:#92400e;font-size:1rem;">Announcement</h4>
      <p style="margin:2px 0 0;color:#b45309;font-size:.85rem;">New offers available on Premium Sinks. Check Now!</p>
    </div>
  </div>
  <a href="#" class="btn-primary" style="padding:6px 12px;font-size:.8rem;background:#d97706;">View All</a>
</div>

<div style="display:grid;grid-template-columns:2fr 1fr;gap:24px;">
  <!-- Recent Orders -->
  <div class="card" style="margin:0;">
    <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px;">
      <h3 style="margin:0;">Recent Orders</h3>
    </div>
    <div class="table-wrap" style="margin-top:0;">
      <table>
        <thead>
          <tr>
            <th>Order ID</th>
            <th>Date</th>
            <th>Amount</th>
            <th>Status</th>
          </tr>
        </thead>
        <tbody>
          <?php foreach($recent_orders as $o): ?>
          <tr>
            <td><strong>#<?= str_pad($o['id'], 5, '0', STR_PAD_LEFT) ?></strong></td>
            <td><?= date('d M Y', strtotime($o['created_at'])) ?></td>
            <td><strong>₹<?= number_format($o['total']) ?></strong></td>
            <td>
              <?php 
                $c = 'info';
                if($o['order_status']==='delivered') $c='success';
                if($o['order_status']==='cancelled') $c='danger';
                if($o['order_status']==='pending') $c='warning';
              ?>
              <span class="badge badge-<?= $c ?>"><?= ucfirst($o['order_status']) ?></span>
            </td>
          </tr>
          <?php endforeach; ?>
          <?php if(!$recent_orders): ?>
          <tr><td colspan="4" style="text-align:center;color:#64748b;padding:20px;">No recent orders.</td></tr>
          <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>

  <!-- Today's Offers -->
  <div class="card" style="margin:0;background:linear-gradient(135deg, #1e3a8a, #3b82f6);color:#fff;display:flex;flex-direction:column;justify-content:center;align-items:center;text-align:center;padding:40px 24px;">
    <h3 style="margin:0 0 8px;font-family:'Outfit';font-size:1.8rem;">Today's Offers</h3>
    <div style="font-size:2.5rem;font-weight:900;color:#fde047;margin-bottom:16px;">UPTO 15% OFF</div>
    <p style="margin:0 0 24px;opacity:0.9;">On Premium Kitchen Sinks. Minimum order 50 pieces.</p>
    <img src="<?= SITE_URL ?>/assets/images/sink1.jpg" alt="Sink" style="width:100%;max-width:200px;border-radius:12px;box-shadow:0 10px 25px rgba(0,0,0,0.2);">
  </div>
</div>

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