<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Order Management System</title>
    <style>
        /* Base Styles */
        :root {
            --primary-color: #2A5C82;
            --secondary-color: #5C9EAD;
            --success-color: #28a745;
            --warning-color: #ffc107;
            --danger-color: #dc3545;
            --text-color: #2c3e50;
            --background-color: #f8f9fa;
        }

        body {
            font-family: 'Roboto', sans-serif;
            margin: 0;
            padding: 20px;
            background-color: var(--background-color);
            color: var(--text-color);
        }

        /* Header Section */
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 2rem;
            padding: 1rem 2rem;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }

        .system-title {
            font-size: 1.8rem;
            font-weight: 700;
            color: var(--primary-color);
        }

        /* Passcode Input */
        .passcode-container {
            display: flex;
            align-items: center;
            gap: 1rem;
        }

        .passcode-input {
            padding: 0.8rem 1.2rem;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 1rem;
            transition: border-color 0.3s ease;
        }

        .passcode-input:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 2px rgba(42,92,130,0.1);
        }

        /* Orders Table */
        .orders-table {
            width: 100%;
            border-collapse: collapse;
            background: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 4px 12px rgba(0,0,0,0.05);
        }

        .orders-table thead {
            background-color: var(--primary-color);
            color: white;
        }

        .orders-table th {
            padding: 1.2rem 1.5rem;
            font-weight: 600;
            text-align: left;
        }

        .orders-table td {
            padding: 1.2rem 1.5rem;
            border-bottom: 1px solid #eee;
        }

        /* Order Number Styling */
        .order-number {
            color: var(--primary-color);
            font-weight: 700;
            font-size: 1.1rem;
        }

        /* Status Badges */
        .status-badge {
            display: inline-block;
            padding: 0.4rem 0.8rem;
            border-radius: 20px;
            font-size: 0.85rem;
            font-weight: 500;
        }

        .status-paid {
            background-color: #e8f5e9;
            color: #2e7d32;
        }

        .status-pending {
            background-color: #fff3e0;
            color: #ef6c00;
        }

        .status-cancelled {
            background-color: #ffebee;
            color: #c62828;
        }

        /* Total Price */
        .total-price {
            font-size: 0.9rem;
            color: var(--text-color);
            margin-top: 0.5rem;
        }

        /* Action Dropdown */
        .action-menu {
            position: relative;
            display: inline-block;
        }

        .menu-trigger {
            padding: 0.5rem;
            border: none;
            background: none;
            cursor: pointer;
        }

        .menu-content {
            display: none;
            position: absolute;
            right: 0;
            background: white;
            min-width: 160px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.15);
            border-radius: 6px;
            z-index: 1;
        }

        .menu-content.show {
            display: block;
        }

        .menu-item {
            padding: 0.8rem 1.2rem;
            font-size: 0.9rem;
            color: var(--text-color);
            cursor: pointer;
            transition: background-color 0.2s ease;
        }

        .menu-item:hover {
            background-color: #f8f9fa;
        }

        /* Responsive Design */
        @media (max-width: 768px) {
            .header {
                flex-direction: column;
                gap: 1rem;
                padding: 1rem;
            }

            .orders-table {
                display: block;
                overflow-x: auto;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="system-title">Order Management System</div>
        <div class="passcode-container">
            <label>Access Code:</label>
            <input type="password" class="passcode-input" id="passCodeInput" 
                   placeholder="••••" maxlength="4" required>
        </div>
    </div>

    <table class="orders-table">
        <thead>
            <tr>
                <th>Order Details</th>
                <th>Items</th>
                <th>Status</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody id="orders-container">
            <!-- Orders will be populated here -->
        </tbody>
    </table>

    <script>
        const companyId = sessionStorage.getItem('companyId');
        let currentUserId = null;

        // Sample Data (Replace with API calls)
        const sampleOrders = [
            {
                _id: "1",
                orderNo: "1001",
                tableNo: "5",
                userName: "John Doe",
                createdAt: "2023-10-01T12:00:00Z",
                status: "pending",
                items: [
                    { itemName: "Burger", selling_price: 10 },
                    { itemName: "Fries", selling_price: 5 }
                ]
            },
            {
                _id: "2",
                orderNo: "1002",
                tableNo: "3",
                userName: "Jane Smith",
                createdAt: "2023-10-01T11:30:00Z",
                status: "paid",
                items: [
                    { itemName: "Pizza", selling_price: 15 },
                    { itemName: "Soda", selling_price: 3 }
                ]
            }
        ];

        // Render Orders
        function renderOrders(orders) {
            const container = document.getElementById('orders-container');
            container.innerHTML = '';

            orders.forEach(order => {
                const totalPrice = order.items.reduce((sum, item) => sum + item.selling_price, 0);
                const row = document.createElement('tr');
                row.innerHTML = `
                    <td>
                        <div class="order-number">#${order.orderNo}</div>
                        <div class="order-meta">
                            <small>Table: ${order.tableNo}</small><br>
                            <small>Waiter: ${order.userName}</small><br>
                            <small>${new Date(order.createdAt).toLocaleString()}</small>
                        </div>
                    </td>
                    <td>
                        <div class="item-list">
                            ${order.items.map(item => `
                                <div class="item">• ${item.itemName} ($${item.selling_price})</div>
                            `).join('')}
                        </div>
                    </td>
                    <td>
                        <div class="status-badge ${getStatusClass(order.status)}">
                            ${order.status}
                        </div>
                        <div class="total-price">
                            Total: $${totalPrice.toFixed(2)}
                        </div>
                    </td>
                    <td>
                        <div class="action-menu">
                            <button class="menu-trigger" onclick="toggleMenu('${order._id}')">⋮</button>
                            <div id="menu-${order._id}" class="menu-content">
                                <div class="menu-item" onclick="payOrder('${order._id}')">Mark as Paid</div>
                                <div class="menu-item" onclick="resumeOrder('${order._id}')">Resume Order</div>
                                <div class="menu-item" onclick="cancelOrder('${order._id}')" style="color: var(--danger-color);">Cancel Order</div>
                            </div>
                        </div>
                    </td>
                `;
                container.appendChild(row);
            });
        }

        // Get Status Class
        function getStatusClass(status) {
            return {
                'paid': 'status-paid',
                'pending': 'status-pending',
                'cancelled': 'status-cancelled'
            }[status.toLowerCase()] || '';
        }

        // Toggle Dropdown Menu
        function toggleMenu(orderId) {
            const menu = document.getElementById(`menu-${orderId}`);
            menu.classList.toggle('show');
        }

        // Close Dropdown on Outside Click
        document.addEventListener('click', function(e) {
            if (!e.target.closest('.action-menu')) {
                document.querySelectorAll('.menu-content').forEach(d => d.classList.remove('show'));
            }
        });

        // Pay Order
        function payOrder(orderId) {
            alert(`Paying order: ${orderId}`);
            // Implement API call to mark order as paid
        }

        // Resume Order
        function resumeOrder(orderId) {
            alert(`Resuming order: ${orderId}`);
            // Implement API call to resume order
        }

        // Cancel Order
        function cancelOrder(orderId) {
            alert(`Cancelling order: ${orderId}`);
            // Implement API call to cancel order
        }

        // Verify Passcode
        const passCodeInput = document.getElementById('passCodeInput');
        passCodeInput.addEventListener('input', function () {
            if (this.value.length === 4) {
                verifyPassCode(this.value);
            }
        });

        async function verifyPassCode(passcode) {
            try {
                // Replace with actual API call
                currentUserId = "123"; // Mock user ID
                renderOrders(sampleOrders); // Render sample orders
                passCodeInput.value = ""; // Clear input field
            } catch (error) {
                console.error("Error verifying passcode:", error);
            }
        }

        // Initial Render (for testing)
        renderOrders(sampleOrders);
    </script>
</body>
</html>