<?php
session_start();
include "../config/db.php";

if (!isset($_SESSION['admin_id'])) {
    header("Location: login.php");
    exit();
}

$result = mysqli_query($conn, "SELECT * FROM products");
?>

<!DOCTYPE html>
<html>
<head>
    <title>All Products</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

</head>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>

<body>

<h2>Product List</h2>

<table border="1" cellpadding="10">
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Price</th>
        <th>Image</th>
        <th>Description</th>
    </tr>

    <?php while ($row = mysqli_fetch_assoc($result)) { ?>
    <tr>
        <td><?php echo $row['id']; ?></td>
        <td><?php echo $row['name']; ?></td>
        <td>₹<?php echo $row['price']; ?></td>
        <td>
            <?php
            $adm_img = $row['images'] ?? '';
            $adm_fs = __DIR__ . '/../assets/images/products/' . $adm_img;
            $adm_src = (!empty($adm_img) && file_exists($adm_fs)) ? '../assets/images/products/' . $adm_img : '';
            ?>
            <img src="<?php echo $adm_src; ?>" width="80">
        </td>
        <td><?php echo $row['description']; ?></td>
    </tr>
    <?php } ?>

</table>

<br>
<a href="dashboard.php">Back to Dashboard</a>

</body>
</html>
