<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Management Dashboard</title>
    <style>
      /* General Page Styling */


h1 {
    font-size: 1.8rem;
    text-align: center;
    margin: 10px 0;
    color: #004085;
}

/* Container Styling */
.tab {
    display: none;
    padding: 20px;
    margin: 10px auto;
    max-width: 800px; /* Reduced container width */
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

.tab-active {
    display: block;
    animation: fadeIn 0.5s ease-in-out;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Navigation Bar */
nav {
    display: flex;
    justify-content: center;
    background-color: #0d6dcc;
    padding: 10px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

nav button {
    background-color: #263941;
    color: #fff;
    font-size: 1rem;
    border: none;
    padding: 10px 20px;
    margin: 5px;
    cursor: pointer;
    border-radius: 50px;
    transition: transform 0.3s ease, background-color 0.3s ease;
}

nav button:hover {
    background-color: #6c757d;
    transform: translateY(-3px);
}

nav button:focus {
    outline: 2px solid #007bff;
    outline-offset: 3px;
}

/* Table Styling */
table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 20px;
    border-radius: 8px;
    overflow: hidden;
}

th, td {
    padding: 12px 15px;
    text-align: left;
    border-bottom: 1px solid #ddd;
}

th {
    background-color: #007bff;
    color: white;
    text-transform: uppercase;
    font-size: 0.9rem;
}

td {
    font-size: 0.95rem;
}

/* Edit and Delete Buttons Row */
td .action-buttons {
    display: flex;
    gap: 10px;
}

.action-buttons button {
    border: none;
    border-radius: 5px;
    padding: 8px 12px;
    cursor: pointer;
    font-size: 0.85rem;
    transition: all 0.3s ease;
}

/* Edit Button Styling */
.action-buttons .edit-button {
    background-color: #007bff;
    color: white;
}

.action-buttons .edit-button:hover {
    background-color: #0056b3;
}

/* Delete Button Styling */
.action-buttons .delete-button {
    background-color: #dc3545;
    color: white;
}

.action-buttons .delete-button:hover {
    background-color: #a71d2a;
}

/* Form Styling */
form {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 20px;
}

form label {
    flex: 1 1 100%;
    font-weight: bold;
    margin-bottom: 5px;
    color: #495057;
}

form input[type="text"],
form input[type="number"] {
    flex: 1 1 calc(50% - 10px);
    padding: 10px;
    font-size: 1rem;
    border: 1px solid #ddd;
    border-radius: 8px;
    transition: box-shadow 0.3s ease, border-color 0.3s ease;
}

form input:focus {
    outline: none;
    border-color: #007bff;
    box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
}

form button {
    flex: 1 1 100%;
    background-color: #007bff;
    color: white;
    border: none;
    padding: 12px 15px;
    font-size: 1rem;
    border-radius: 8px;
    cursor: pointer;
    transition: background-color 0.3s ease, transform 0.3s ease;
}

form button:hover {
    background-color: #0056b3;
    transform: translateY(-2px);
}

/* Responsive Behavior */
@media (max-width: 768px) {
    form input[type="text"],
    form input[type="number"],
    form button {
        flex: 1 1 100%;
    }
}


/* Table Action Buttons in Row */
td .action-buttons {
    display: flex; /* Align buttons in a row */
    gap: 8px; /* Add spacing between buttons */
    justify-content: flex-start; /* Align buttons to the left */
}

.action-buttons button {
    border: none;
    border-radius: 5px;
    padding: 8px 12px;
    width: 140px;
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.3s ease;
}

/* Edit Button Styling */
.action-buttons .edit-button {
    background-color: #007bff;
    color: white;
}

.action-buttons .edit-button:hover {
    background-color: #0056b3;
}

/* Delete Button Styling */
.action-buttons .delete-button {
    background-color: #dc3545;
    color: white;
}

.action-buttons .delete-button:hover {
    background-color: #a71d2a;
}

    </style>
</head>
<body>
    <nav>
        <button onclick="showTab('table')">Table</button>
        <button onclick="showTab('kitchen')">Kitchen</button>
        <button onclick="showTab('category')">Category</button>
    </nav>
    
    <div id="table" class="tab tab-active">
        <h1>Table Management</h1>
        <form id="tableForm">
            <label for="tableNo">Table Number:</label>
            <input type="number" id="tableNo" required>
            <button id="tableSubmitButton" type="submit">Add Table</button>
        </form>
        <table id="tableData">
            <tr><th>Table Number</th><th>Actions</th></tr>
        </table>
    </div>
    
    <div id="kitchen" class="tab">
        <h1>Kitchen Management</h1>
        <form id="kitchenForm">
            <label for="kitchenName">Kitchen Name:</label>
            <input type="text" id="kitchenName" required>
            <button id="kitchenSubmitButton" type="submit">Add Kitchen</button>
        </form>
        <table id="kitchenData">
            <tr><th>Kitchen Name</th><th>Actions</th></tr>
        </table>
    </div>
    
    <div id="category" class="tab">
        <h1>Category Management</h1>
        <form id="categoryForm">
            <label for="categoryName">Category Name:</label>
            <input type="text" id="categoryName" required>
            <button id="categorySubmitButton" type="submit">Add Category</button>
        </form>
        <table id="categoryData">
            <tr><th>Category Name</th><th>Actions</th></tr>
        </table>
    </div>
    <script>
        let editingTableId = null;
        let editingKitchenId = null;
        let editingCategoryId = null;

        function showTab(tabId) {
            document.querySelectorAll('.tab').forEach(tab => tab.classList.remove('tab-active'));
            document.getElementById(tabId).classList.add('tab-active');
        }

        // Table CRUD
        async function fetchTables() {
            const response = await fetch('/api/table');
            const tables = await response.json();
            const tableData = document.getElementById('tableData');
            tableData.innerHTML = `<tr><th>Table Number</th><th>Actions</th></tr>`;
            tables.forEach(table => {
                tableData.insertAdjacentHTML('beforeend', `
                    <tr>
                        <td>${table.No}</td>
                        <td>
                              <div class="action-buttons">
                            <button class="edit-button" onclick="startEditTable('${table._id}', '${table.No}')">Edit</button>
                            <button class="delete-button" onclick="deleteTable('${table._id})'">Delete</button>
                       </div>
                            </td>
                    </tr>
                `);
            });
        }

        async function addTable(e) {
            e.preventDefault();
            const tableNo = document.getElementById('tableNo').value;
            const url = editingTableId ? `/api/table/${editingTableId}` : '/api/table';
            const method = editingTableId ? 'PUT' : 'POST';

            await fetch(url, {
                method,
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ No: tableNo })
            });

            editingTableId = null;
            document.getElementById('tableForm').reset();
            fetchTables();
        }

        async function deleteTable(id) {
            await fetch(`/api/table/${id}`, { method: 'DELETE' });
            fetchTables();
        }

        function startEditTable(id, currentNo) {
            document.getElementById('tableNo').value = currentNo;
            editingTableId = id;
            document.getElementById('tableSubmitButton').textContent = 'Save Changes';
        }






        document.getElementById('tableForm').addEventListener('submit', addTable);
        fetchTables();

        // Kitchen CRUD
        async function fetchKitchens() {
            const response = await fetch('/api/kitchen');
            const kitchens = await response.json();
            const kitchenData = document.getElementById('kitchenData');
            kitchenData.innerHTML = `<tr><th>Kitchen Name</th><th>Actions</th></tr>`;
            kitchens.forEach(kitchen => {
                kitchenData.insertAdjacentHTML('beforeend', `
                    <tr>
                        <td>${kitchen.name}</td>
                        <td>
                            <div class="action-buttons">
                            <button class="edit-button" onclick="startEditKitchen('${kitchen._id}', '${kitchen.name}')">Edit</button>
                            <button class="delete-button" onclick="deleteKitchen('${kitchen._id})'">Delete</button>
                       </div>
                            </td>
                    </tr>
                `);
            });
        }

        async function addKitchen(e) {
            e.preventDefault();
            const kitchenName = document.getElementById('kitchenName').value;
            const url = editingKitchenId ? `/api/kitchen/${editingKitchenId}` : '/api/kitchen';
            const method = editingKitchenId ? 'PUT' : 'POST';

            await fetch(url, {
                method,
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ name: kitchenName })
            });

            editingKitchenId = null;
            document.getElementById('kitchenForm').reset();
            fetchKitchens();
        }

        async function deleteKitchen(id) {
            await fetch(`/api/kitchen/${id}`, { method: 'DELETE' });
            fetchKitchens();
        }

        function startEditKitchen(id, currentName) {
            document.getElementById('kitchenName').value = currentName;
            editingKitchenId = id;
            document.getElementById('kitchenSubmitButton').textContent = 'Save Changes';
   
        }



      
        
    
    
    



        document.getElementById('kitchenForm').addEventListener('submit', addKitchen);
        fetchKitchens();

        // Category CRUD
        async function fetchCategories() {
            const response = await fetch('/api/category');
            const categories = await response.json();
            const categoryData = document.getElementById('categoryData');
            categoryData.innerHTML = `<tr><th>Category Name</th><th>Actions</th></tr>`;
            categories.forEach(category => {
                categoryData.insertAdjacentHTML('beforeend', `
                    <tr>
                        <td>${category.name}</td>
                        <td>
                              <div class="action-buttons">
                            <button class="edit-button" onclick="startEditCategory('${category._id}', '${category.name}')">Edit</button>
                            <button class="delete-button" onclick="deleteCategory('${category._id}')">Delete</button>
                        </div
                            </td>
                    </tr>
                `);
            });
        }

        async function addCategory(e) {
            e.preventDefault();
         
            const categoryName = document.getElementById('categoryName').value;
            const url = editingCategoryId ? `/api/category/${editingCategoryId}` : '/api/category';
            const method = editingCategoryId ? 'PUT' : 'POST';

            await fetch(url, {
                method,
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ name: categoryName })
            });

            editingCategoryId = null;
            document.getElementById('categoryForm').reset();
            fetchCategories();
        }

        async function deleteCategory(id) {
            await fetch(`/api/category/${id}`, { method: 'DELETE' });
            fetchCategories();
        }

        function startEditCategory(id, currentName) {
            document.getElementById('categoryName').value = currentName;
            editingCategoryId = id;
            document.getElementById('categorySubmitButton').textContent = 'Save Changes';
    
        }

        document.getElementById('categoryForm').addEventListener('submit', addCategory);
        fetchCategories();
    </script>
</body>
</html>
