<?php

include "auth.php";
include "db.php";



$totalProducts = $conn->query("
SELECT COUNT(*) AS total
FROM products
")->fetch_assoc()['total'];

$totalCategories = $conn->query("
SELECT COUNT(DISTINCT category) AS total
FROM products
")->fetch_assoc()['total'];

$activeOffers = $conn->query("
SELECT COUNT(*) AS total
FROM offers
WHERE CURDATE()
BETWEEN start_date AND end_date
")->fetch_assoc()['total'];

$expiredOffers = $conn->query("
SELECT COUNT(*) AS total
FROM offers
WHERE end_date < CURDATE()
")->fetch_assoc()['total'];



$recentProducts = $conn->query("
SELECT *
FROM products
ORDER BY id DESC
LIMIT 5
");



$expiringOffers = $conn->query("
SELECT offers.*,
products.name
FROM offers
JOIN products
ON offers.product_id = products.id
WHERE end_date >= CURDATE()
AND end_date <= DATE_ADD(CURDATE(), INTERVAL 7 DAY)
ORDER BY end_date ASC
");

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  
<link rel="stylesheet" href="admin.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css">
<link rel="preconnect" href="https://fonts.googleapis.com">


   <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>

  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600&family=Playfair+Display:wght@500;600;700&display=swap" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600&family=Playfair+Display:wght@500;600;700&display=swap" rel="stylesheet">
<title>Admin Dashboard</title>
</head>
<body>
<div class="admin-container">

<button class="menu-toggle" id="menu-toggle">
    <i class="fa-solid fa-bars"></i>
</button>

  <aside class="sidebar" id="sidebar">

    <h2 class="logo">Maria Admin</h2>

    <nav>
      <a href="dashboard.php" class="active">Dashboard</a>
      <a href="addproducts.php">Add Product</a>
      <a href="add_offer.php">Add Offer</a>
      <a href="offers.php">Offers</a>
      <a href="products.php">Products</a>
      <a href="categoriesadmin.php">Categories</a>
      <a href="settings.php">Settings</a>
      <a href="logout.php" class="logout">Logout</a>
    </nav>

  </aside>

  
  <main class="main-content">

    <h1>Dashboard</h1>

<div class="dashboard-cards" style=" display:grid;
    grid-template-columns:repeat(auto-fit,minmax(220px,1fr));
    gap:20px;
    margin-bottom:25px;">

  <div class="dashboard-card" style=" background:#fff;
    padding:25px;
    border-radius:12px;
    box-shadow:0 2px 10px rgba(0,0,0,.08);">
    <h3 style="margin:0;
    color:#17304f;
    font-size:16px">Total Products</h3>
    <span style=" display:block;
    margin-top:12px;
    font-size:32px;
    font-weight:700;
    color:#17304f;"><?= $totalProducts ?></span>
  </div>

  <div class="dashboard-card" style=" background:#fff;
    padding:25px;
    border-radius:12px;
    box-shadow:0 2px 10px rgba(0,0,0,.08);">
    <h3 style="margin:0;
    color:#17304f;
    font-size:16px">Categories</h3>
    <span style=" display:block;
    margin-top:12px;
    font-size:32px;
    font-weight:700;
    color:#17304f;"><?= $totalCategories ?></span>
  </div>

  <div class="dashboard-card" style=" background:#fff;
    padding:25px;
    border-radius:12px;
    box-shadow:0 2px 10px rgba(0,0,0,.08);">
    <h3 style="margin:0;
    color:#17304f;
    font-size:16px">Active Offers</h3>
    <span style=" display:block;
    margin-top:12px;
    font-size:32px;
    font-weight:700;
    color:#17304f;"><?= $activeOffers ?></span>
  </div>

  <div class="dashboard-card" style=" background:#fff;
    padding:25px;
    border-radius:12px;
    box-shadow:0 2px 10px rgba(0,0,0,.08);">
    <h3 style="margin:0;
    color:#17304f;
    font-size:16px">Expired Offers</h3>
    <span style=" display:block;
    margin-top:12px;
    font-size:32px;
    font-weight:700;
    color:#17304f;"><?= $expiredOffers ?></span>
  </div>

</div>



<div class="quick-actions" style="display:flex;
    gap:15px;
    flex-wrap:wrap;
    margin-bottom:25px;">

  <a href="addproducts.php" class="action-btn" style="padding:12px 20px;
    background:#17304f;
    color:#fff;
    text-decoration:none;
    border-radius:8px;
    font-weight:600;">
    Add Product
  </a>

  <a href="add_offer.php" class="action-btn" style="padding:12px 20px;
    background:#17304f;
    color:#fff;
    text-decoration:none;
    border-radius:8px;
    font-weight:600;">
    Add Offer
  </a>

  <a href="categoriesadmin.php" class="action-btn" style="padding:12px 20px;
    background:#17304f;
    color:#fff;
    text-decoration:none;
    border-radius:8px;
    font-weight:600;">
    View Categories
  </a>

</div>



<div class="table-card">

  <h2 class="table-title" style="  font-size: 23px;

  color: #17304f;

 
  font-family: 'Playfair Display', serif; margin-bottom: 7px;">Recent Products</h2>

  <table class="admin-table">

    <thead>
      <tr>
        <th>Name</th>
        <th>Category</th>
        <th>Price</th>
      </tr>
    </thead>

    <tbody>

    <?php while($product = $recentProducts->fetch_assoc()): ?>

      <tr>

        <td>
          <?= htmlspecialchars($product['name']) ?>
        </td>

        <td>
          <?= htmlspecialchars($product['category']) ?>
        </td>

        <td>
          Ksh <?= number_format($product['price']) ?>
        </td>

      </tr>

    <?php endwhile; ?>

    </tbody>

  </table>

</div>


<br>

<div class="table-card">

  <h2 style="  font-size: 23px;

  color: #17304f;

 
  font-family: 'Playfair Display', serif; margin-bottom: 7px;">Offers Expiring Soon</h2>

  <table class="admin-table">

    <thead>

      <tr>
        <th>Product</th>
        <th>Offer</th>
        <th>End Date</th>
      </tr>

    </thead>

    <tbody>

    <?php while($offer = $expiringOffers->fetch_assoc()): ?>

      <tr>

        <td>
          <?= htmlspecialchars($offer['name']) ?>
        </td>

        <td>
          <?= htmlspecialchars($offer['offer_title']) ?>
        </td>

        <td>
          <?= $offer['end_date'] ?>
        </td>

      </tr>

    <?php endwhile; ?>

    </tbody>

  </table>

</div>

  </main>

  <script>
const menuToggle = document.getElementById("menu-toggle");
const sidebar = document.getElementById("sidebar");
const icon = menuToggle.querySelector("i");

menuToggle.addEventListener("click", function(){

    sidebar.classList.toggle("active");

    if(sidebar.classList.contains("active")){

        icon.classList.remove("fa-bars");
        icon.classList.add("fa-xmark");

    }else{

        icon.classList.remove("fa-xmark");
        icon.classList.add("fa-bars");

    }

});

</script>
</body>
</html>