<?php
require_once '../db/config.php';
require_once 'header.php';

$db = db();

// Handle form submissions for adding, editing, and deleting cuisines
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['add_cuisine'])) {
        $name = trim($_POST['name']);
        if (!empty($name)) {
            $stmt = $db->prepare("INSERT INTO cuisines (name) VALUES (:name) ON CONFLICT (name) DO NOTHING");
            $stmt->bindValue(':name', $name, PDO::PARAM_STR);
            $stmt->execute();
        }
    } elseif (isset($_POST['delete_cuisine'])) {
        $id = $_POST['id'];
        $stmt = $db->prepare("DELETE FROM cuisines WHERE id = :id");
        $stmt->bindValue(':id', $id, PDO::PARAM_INT);
        $stmt->execute();
    }
}

// Fetch all cuisines
$cuisines = $db->query("SELECT * FROM cuisines ORDER BY name ASC")->fetchAll();

?>

<div class="container mx-auto px-4 py-8">
    <h1 class="text-3xl font-bold mb-6">Manage Cuisines</h1>

    <!-- Add Cuisine Form -->
    <div class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
        <h2 class="text-2xl mb-4">Add New Cuisine</h2>
        <form action="cuisines.php" method="POST">
            <div class="mb-4">
                <label class="block text-gray-700 text-sm font-bold mb-2" for="name">
                    Cuisine Name
                </label>
                <input class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="name" name="name" type="text" placeholder="e.g., Italian, Mexican" required>
            </div>
            <div class="flex items-center justify-between">
                <button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" type="submit" name="add_cuisine">
                    Add Cuisine
                </button>
            </div>
        </form>
    </div>

    <!-- Cuisines Table -->
    <div class="bg-white shadow-md rounded px-8 pt-6 pb-8">
        <h2 class="text-2xl mb-4">Existing Cuisines</h2>
        <table class="min-w-full table-auto">
            <thead class="bg-gray-200">
                <tr>
                    <th class="px-4 py-2">ID</th>
                    <th class="px-4 py-2">Name</th>
                    <th class="px-4 py-2">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($cuisines as $cuisine): ?>
                    <tr class="border-b">
                        <td class="px-4 py-2 text-center"><?php echo htmlspecialchars($cuisine['id']); ?></td>
                        <td class="px-4 py-2"><?php echo htmlspecialchars($cuisine['name']); ?></td>
                        <td class="px-4 py-2 text-center">
                            <form action="cuisines.php" method="POST" onsubmit="return confirm('Are you sure you want to delete this cuisine?');">
                                <input type="hidden" name="id" value="<?php echo $cuisine['id']; ?>">
                                <button type="submit" name="delete_cuisine" class="text-red-500 hover:text-red-700">Delete</button>
                            </form>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<?php require_once 'footer.php'; ?>
