<?php
session_start();
$pageTitle = "Products";
require_once("./config/db.php");
require_once("./includes/auth.php");
include("./includes/header.php");

// Load product list
$stmt = $pdo->query("SELECT * FROM products ORDER BY ProductID DESC");
$products = $stmt->fetchAll();
?>

<style>
.card-product {
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 2px 6px rgba(0,0,0,0.1);
    transition: 0.2s;
    background: #fff;
}

.card-product:hover {
    transform: translateY(-4px);
    box-shadow: 0 4px 16px rgba(0,0,0,0.15);
}

/* FIXED IMAGE — always fully visible */
.card-product img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.stock-ok {
    color: green;
    font-weight: bold;
}
.stock-zero {
    color: red;
    font-weight: bold;
}
</style>

<div class="d-flex justify-content-between align-items-center mb-3">
    <h3><i class="bi bi-shop"></i> Products</h3>
</div>

<div class="row g-3">

<?php foreach ($products as $p): ?>
    <div class="col-6 col-md-4 col-lg-3 col-xl-2">
        <div class="card card-product">

            <!-- Product Image -->
            <?php if (!empty($p['Photo'])): ?>
                <img src="./uploads/<?= htmlspecialchars($p['Photo']) ?>">
            <?php else: ?>
                <div class="bg-light d-flex justify-content-center align-items-center"
                     style="height:160px;">
                    <span class="text-muted">No Photo</span>
                </div>
            <?php endif; ?>

            <div class="px-2 py-2">

                <!-- Name -->
                <h6 class="fw-bold mb-1"><?= htmlspecialchars($p['Name']) ?></h6>

                <!-- Price -->
                <div class="small mb-1">
                    Price: <strong><?= number_format($p['SellingPrice'], 2) ?></strong>
                </div>

                <!-- Stock -->
                <?php if ($p['StockQuantity'] > 0): ?>
                    <div class="stock-ok mb-2">In Stock</div>
                <?php else: ?>
                    <div class="stock-zero mb-2">Sold Out</div>
                <?php endif; ?>

                <!-- Cart Button -->
                <div class="d-grid">
                    <a href="checkout.php?id=<?= $p['ProductID'] ?>"
                       class="btn btn-primary btn-sm <?= $p['StockQuantity'] == 0 ? 'disabled' : '' ?>">
                        + Cart
                    </a>
                </div>
            </div>

        </div>
    </div>
<?php endforeach; ?>

</div>

<?php include("./includes/footer.php"); ?>
