<?php include '_head.php';
$p=mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(*) c FROM products"));
$o=mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(*) c FROM orders"));
$u=mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(*) c FROM users"));
$sales=mysqli_fetch_assoc(mysqli_query($conn,"SELECT COALESCE(SUM(total),0) s FROM orders WHERE status<>'cancelled'"));
$pending=mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(*) c FROM orders WHERE status='pending'"));
$stock=mysqli_fetch_assoc(mysqli_query($conn,"SELECT COALESCE(SUM(stock),0) s FROM products"));
$latest=mysqli_query($conn,"SELECT * FROM orders ORDER BY id DESC LIMIT 8");
?>
<h1>لوحة التحكم</h1>
<div class="admin-stats-grid">
  <div class="admin-stat"><i class="fa-solid fa-box"></i><span>المنتجات</span><strong><?php echo (int)$p['c']; ?></strong></div>
  <div class="admin-stat"><i class="fa-solid fa-receipt"></i><span>الطلبات</span><strong><?php echo (int)$o['c']; ?></strong></div>
  <div class="admin-stat"><i class="fa-solid fa-clock"></i><span>طلبات قيد الانتظار</span><strong><?php echo (int)$pending['c']; ?></strong></div>
  <div class="admin-stat"><i class="fa-solid fa-sack-dollar"></i><span>إجمالي المبيعات</span><strong><?php echo money_iqd($sales['s']); ?></strong></div>
  <div class="admin-stat"><i class="fa-solid fa-warehouse"></i><span>إجمالي المخزن</span><strong><?php echo (int)$stock['s']; ?></strong></div>
  <div class="admin-stat"><i class="fa-solid fa-users"></i><span>الزبائن</span><strong><?php echo (int)$u['c']; ?></strong></div>
</div>
<div class="admin-card">
  <h2>آخر الطلبات</h2>
  <table class="table"><tr><th>#</th><th>الزبون</th><th>الهاتف</th><th>المجموع</th><th>الحالة</th><th>التاريخ</th></tr>
  <?php if($latest): while($o=mysqli_fetch_assoc($latest)): ?><tr><td><?php echo (int)$o['id']; ?></td><td><?php echo e($o['customer_name']); ?></td><td><?php echo e($o['phone']); ?></td><td><?php echo money_iqd($o['total']); ?></td><td><?php echo e($o['status']); ?></td><td><?php echo e($o['created_at']); ?></td></tr><?php endwhile; endif; ?>
  </table>
</div>
<?php include '_foot.php'; ?>
