<?php
session_start();
require_once(__DIR__ . '/../config.php');

if (!isset($_SESSION['user_id']) || $_SESSION['role'] != 'admin') {
    header("Location: dashboard.php");
    exit();
}

$stmt = $pdo->query("SELECT * FROM products ORDER BY id DESC");
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);

include(__DIR__ . '/../header/header.php');
?>

<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="fw-bold">Products</h2>
        <a href="add_product.php" class="btn btn-primary shadow-sm">
            <i class="fas fa-plus-circle me-2"></i>Add Product
        </a>
    </div>

    <div class="card glass-card border-0 shadow-sm">
        <div class="card-header bg-white border-0 py-3">
            <h5 class="fw-bold mb-0">Products Directory</h5>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="ps-4">Product</th>
                            <th>Price</th>
                            <th>Stock</th>
                            <th>Added</th>
                            <th class="text-end pe-4">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($products as $product): ?>
                            <tr>
                                <td class="ps-4">
                                    <div class="d-flex align-items-center">
                                        <?php
                                            $image_src = '../images/lapi.jpg';
                                            if (!empty($product['image']) && file_exists(__DIR__ . '/../uploads/' . $product['image'])) {
                                                $image_src = '../uploads/' . $product['image'];
                                            }
                                        ?>
                                        <img src="<?= htmlspecialchars($image_src); ?>" alt="<?= htmlspecialchars($product['name']); ?>" style="width:45px;height:45px;object-fit:cover;border-radius:6px;" class="me-3 shadow-sm">
                                        <div>
                                            <div class="fw-bold text-dark"><?= htmlspecialchars($product['name']); ?></div>
                                            <small class="text-muted"><?= htmlspecialchars($product['description']); ?></small>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <span class="badge bg-success px-3 py-2 rounded-pill">
                                        KES <?= number_format($product['price']); ?>
                                    </span>
                                </td>
                                <td>
                                    <span class="badge bg-info px-3 py-2 rounded-pill">
                                        <?= (int)$product['stock']; ?>
                                    </span>
                                </td>
                                <td>
                                    <i class="far fa-calendar-alt me-2 text-muted"></i><?= date('M d, Y', strtotime($product['created_at'])); ?>
                                </td>
                                <td class="text-end pe-4">
                                    <div class="d-flex justify-content-end align-items-center gap-2">
                                        <a href="product_view.php?id=<?= $product['id']; ?>" class="btn btn-sm btn-outline-secondary">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <!-- Future: Add edit/delete buttons here -->
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($products)): ?>
                            <tr>
                                <td colspan="5" class="text-center text-muted py-4">No products found.</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<?php include(__DIR__ . '/../footer/footer.php'); ?>
