<?php
include("../config/db.php");
if (!isset($_SESSION['admin'])) header("Location: login.php");

$data = $conn->query("
    SELECT m.*, c.name AS category 
    FROM menu_items m 
    JOIN categories c ON m.category_id = c.id
");
?>
<!DOCTYPE html>
<html>
<head>
    <title>Admin Dashboard</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" rel="stylesheet">
</head>
<body class="bg-light">

<nav class="navbar navbar-dark bg-dark px-4">
    <span class="navbar-brand">🍽 Restaurant Admin</span>
    <a href="logout.php" class="btn btn-danger btn-sm">Logout</a>
</nav>

<div class="container mt-4">

    <div class="d-flex justify-content-between mb-3">
        <h3>Menu Items</h3>
        <a href="add-menu.php" class="btn btn-primary">
            <i class="fa fa-plus"></i> Add Item
        </a>
    </div>

    <div class="card shadow">
        <div class="card-body">
            <table class="table table-bordered table-hover align-middle">
                <thead class="table-dark">
                <tr>
                    <th>Image</th>
                    <th>Item</th>
                    <th>Category</th>
                    <th>Price</th>
                    <th>Type</th>
                    <th>Action</th>
                </tr>
                </thead>

                <?php while($row = $data->fetch_assoc()): ?>
                <tr>
                    <td>
                        <img src="../uploads/menu/<?= $row['image'] ?>" width="60">
                    </td>
                    <td><?= $row['item_name'] ?></td>
                    <td><?= $row['category'] ?></td>
                    <td>₹<?= $row['price'] ?></td>
                    <td>
                        <?= $row['type'] == 'veg'
                            ? '<span class="badge bg-success">🥬 Veg</span>'
                            : '<span class="badge bg-danger">🍗 Non-Veg</span>' ?>
                    </td>
                    <td>
                        <a href="edit-menu.php?id=<?= $row['id'] ?>" class="btn btn-warning btn-sm">Edit</a>
                        <a href="delete-menu.php?id=<?= $row['id'] ?>" class="btn btn-danger btn-sm"
                           onclick="return confirm('Delete this item?')">Delete</a>
                    </td>
                </tr>
                <?php endwhile; ?>

            </table>
        </div>
    </div>
</div>

</body>
</html>
