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

// Stats
$stats = [
    ['📋', 'New RFQs', Database::fetchOne("SELECT COUNT(*) as c FROM quotations WHERE status='pending'")['c'] ?? 0, '#ff6b00'],
    ['📄', 'Estimates Sent', Database::fetchOne("SELECT COUNT(*) as c FROM estimates WHERE status='sent'")['c'] ?? 0, '#6366f1'],
    ['💰', 'Pending Invoices', Database::fetchOne("SELECT COUNT(*) as c FROM invoices WHERE status IN ('sent','partial','overdue')")['c'] ?? 0, '#f59e0b'],
    ['🛠️', 'Repair Requests', Database::fetchOne("SELECT COUNT(*) as c FROM repair_requests WHERE status='received'")['c'] ?? 0, '#10b981'],
];

$recentQuotes  = Database::fetchAll("SELECT q.*, c.contact_name, c.company_name FROM quotations q LEFT JOIN customers c ON q.customer_id=c.id ORDER BY q.created_at DESC LIMIT 8");
$recentInvoices = Database::fetchAll("SELECT i.*, c.contact_name, c.company_name FROM invoices i LEFT JOIN customers c ON i.customer_id=c.id ORDER BY i.created_at DESC LIMIT 8");
$totalRevenue = Database::fetchOne("SELECT COALESCE(SUM(amount_paid),0) as r FROM invoices WHERE status IN ('paid','partial')")['r'] ?? 0;
$overdueAmount = Database::fetchOne("SELECT COALESCE(SUM(total-amount_paid),0) as r FROM invoices WHERE status='overdue'")['r'] ?? 0;
$totalProducts = Database::fetchOne("SELECT COUNT(*) as c FROM products WHERE is_active=1")['c'] ?? 0;
$totalCustomers = Database::fetchOne("SELECT COUNT(*) as c FROM customers")['c'] ?? 0;
?>

<!-- Revenue strip -->
<div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6">
  <?php foreach ($stats as [$icon, $label, $val, $color]): ?>
  <div class="card-dark p-5">
    <div class="flex items-center justify-between mb-3">
      <span class="text-2xl"><?= $icon ?></span>
      <span class="text-2xl font-bold" style="color:<?= $color ?>"><?= $val ?></span>
    </div>
    <div class="text-xs text-gray-400"><?= $label ?></div>
  </div>
  <?php endforeach; ?>
</div>

<div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-8">
  <div class="card-dark p-5">
    <div class="text-xs text-gray-400 mb-1">Total Revenue (PKR)</div>
    <div class="text-xl font-bold text-green-400"><?= number_format($totalRevenue, 0) ?></div>
  </div>
  <div class="card-dark p-5">
    <div class="text-xs text-gray-400 mb-1">Overdue Amount (PKR)</div>
    <div class="text-xl font-bold text-red-400"><?= number_format($overdueAmount, 0) ?></div>
  </div>
  <div class="card-dark p-5 flex gap-6">
    <div><div class="text-xs text-gray-400 mb-1">Products</div><div class="text-xl font-bold"><?= $totalProducts ?></div></div>
    <div><div class="text-xs text-gray-400 mb-1">Customers</div><div class="text-xl font-bold"><?= $totalCustomers ?></div></div>
  </div>
</div>

<!-- Quick Actions -->
<div class="grid grid-cols-2 md:grid-cols-5 gap-3 mb-8">
  <?php foreach ([
    ['/admin/products.php?action=add','➕ New Product'],
    ['/admin/quotes.php','📋 View RFQs'],
    ['/admin/invoices.php?action=create','💰 New Invoice'],
    ['/admin/customers.php','👥 Customers'],
    ['/admin/updater.php','🔄 Site Updater'],
    ['/admin/api.php','🔌 API Console'],
  ] as [$href,$label]): ?>
  <a href="<?= $href ?>" class="card-dark p-3 text-center text-sm font-medium hover:border-orange-600 transition-colors"><?= $label ?></a>
  <?php endforeach; ?>
</div>

<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
  <!-- Recent RFQs -->
  <div class="card-dark overflow-hidden">
    <div class="p-4 border-b border-navy-500 flex justify-between items-center">
      <h3 class="font-semibold text-sm">Recent RFQ Requests</h3>
      <a href="/admin/quotes.php" class="text-xs text-orange-500">View All</a>
    </div>
    <table class="data-table">
      <thead><tr><th>Quote #</th><th>Customer</th><th>Status</th><th>Date</th><th></th></tr></thead>
      <tbody>
      <?php foreach ($recentQuotes as $q): ?>
      <tr>
        <td class="font-mono text-xs text-orange-400"><?= e($q['quote_number']) ?></td>
        <td class="text-sm"><?= e($q['company_name'] ?: $q['contact_name']) ?></td>
        <td><?= statusBadge($q['status']) ?></td>
        <td class="text-xs text-gray-400"><?= formatDate($q['created_at']) ?></td>
        <td><a href="/admin/quotes.php?view=<?= $q['id'] ?>" class="text-xs text-orange-500 hover:underline">Open</a></td>
      </tr>
      <?php endforeach; ?>
      </tbody>
    </table>
  </div>

  <!-- Recent Invoices -->
  <div class="card-dark overflow-hidden">
    <div class="p-4 border-b border-navy-500 flex justify-between items-center">
      <h3 class="font-semibold text-sm">Recent Invoices</h3>
      <a href="/admin/invoices.php" class="text-xs text-orange-500">View All</a>
    </div>
    <table class="data-table">
      <thead><tr><th>Invoice #</th><th>Customer</th><th>Amount</th><th>Status</th></tr></thead>
      <tbody>
      <?php foreach ($recentInvoices as $inv): ?>
      <tr>
        <td class="font-mono text-xs text-orange-400"><?= e($inv['invoice_number']) ?></td>
        <td class="text-sm"><?= e($inv['company_name'] ?: $inv['contact_name']) ?></td>
        <td class="text-sm font-semibold"><?= formatPrice($inv['total']) ?></td>
        <td><?= statusBadge($inv['status']) ?></td>
      </tr>
      <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>

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