<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>Orders - {{ $hotelName ?? hotelName() }} POS</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    
    @hotelSettings
   <style>
        :root {
            --primary-color: var(--primary-green, #1a936f);
            --primary-dark: var(--dark-green, #0f6b4f);
            --primary-light: var(--secondary-green, #88d498);
            --secondary-color: var(--dark-green, #114b5f);
            --accent-color: var(--light-green, #c6dabf);
            --border-radius: var(--border-radius, 12px);
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f8f9fa;
            padding: 20px;
        }
        
        .container-fluid {
            max-width: 1600px;
            margin: 0 auto;
        }
        
        .header {
            background: linear-gradient(135deg, var(--primary-color), var(--primary-light));
            color: white;
            padding: 25px;
            border-radius: 10px;
            margin-bottom: 25px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
        }
        
        .stats-card {
            background: white;
            border-radius: 10px;
            padding: 20px;
            text-align: center;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            margin-bottom: 20px;
            border-bottom: 3px solid var(--primary-color);
        }
        
        .stats-card h3 {
            color: var(--primary-color);
            font-weight: 700;
            margin-bottom: 5px;
            font-size: 2rem;
        }
        
        .stats-card p {
            color: #6c757d;
            margin: 0;
            font-size: 0.9rem;
        }
        
        .filter-card {
            background: white;
            border-radius: var(--border-radius, 12px);
            padding: 20px;
            margin-bottom: 25px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            border-left: 4px solid var(--primary-color);
        }
        
        .table-container {
            background: white;
            border-radius: var(--border-radius, 12px);
            padding: 20px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            overflow-x: auto;
        }
        
        table {
            width: 100%;
            border-collapse: collapse;
        }
        
        th {
            background: #f8f9fa;
            color: var(--secondary-color);
            font-weight: 700;
            padding: 15px;
            text-align: left;
            border-bottom: 2px solid var(--accent-color);
        }
        
        td {
            padding: 15px;
            border-bottom: 1px solid #e9ecef;
            vertical-align: middle;
        }
        
        tr:hover {
            background-color: #f8f9fa;
        }
        
        .status-badge {
            padding: 5px 12px;
            border-radius: 20px;
            font-size: 0.85rem;
            font-weight: 600;
            display: inline-block;
        }
        
        .status-pending { 
            background: #fff3cd; 
            color: #856404; 
        }
        .status-processing { 
            background: #cce7ff; 
            color: #004085; 
        }
        .status-completed { 
            background: #d4edda; 
            color: #155724; 
        }
        .status-cancelled { 
            background: #f8d7da; 
            color: #721c24; 
        }
        
        .btn-action {
            padding: 6px 12px;
            border-radius: 5px;
            font-size: 0.85rem;
            margin: 0 2px;
            border: none;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .btn-show { 
            background: #17a2b8; 
            color: white; 
        }
        .btn-receipt { 
            background: var(--primary-color); 
            color: white; 
        }
        .btn-complete { 
            background: var(--secondary-color); 
            color: white; 
        }
        
        .btn-show:hover { 
            background: #138496; 
        }
        .btn-receipt:hover { 
            background: var(--primary-dark); 
        }
        .btn-complete:hover { 
            background: #0a3645; 
        }
        
        .order-number {
            font-weight: 700;
            color: var(--secondary-color);
        }
        
        .amount {
            font-weight: 700;
            color: var(--primary-color);
        }
        
        .btn-primary {
            background-color: var(--primary-color);
            border-color: var(--primary-color);
        }
        
        .btn-primary:hover {
            background-color: var(--primary-dark);
            border-color: var(--primary-dark);
        }
        
        .btn-outline-primary {
            color: var(--primary-color);
            border-color: var(--primary-color);
        }
        
        .btn-outline-primary:hover {
            background-color: var(--primary-color);
            border-color: var(--primary-color);
            color: white;
        }
        
        .text-primary {
            color: var(--primary-color) !important;
        }
        
        .bg-primary {
            background-color: var(--primary-color) !important;
        }
        
        .pagination .page-link {
            color: var(--primary-color);
        }
        
        .pagination .page-item.active .page-link {
            background-color: var(--primary-color);
            border-color: var(--primary-color);
            color: white;
        }
        
        a {
            color: var(--primary-color);
            text-decoration: none;
        }
        
        a:hover {
            color: var(--primary-dark);
            text-decoration: underline;
        }
        
        .modal-content {
            border-radius: var(--border-radius, 12px);
            border: none;
        }
        
        .modal-header {
            background: linear-gradient(135deg, var(--primary-color), var(--primary-light));
            color: white;
            border-radius: 12px 12px 0 0;
            padding: 20px;
        }
        
        .modal-header .btn-close {
            filter: brightness(0) invert(1);
        }
        
        .modal-body {
            padding: 25px;
        }
        
        .detail-row {
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 1px solid #e9ecef;
        }
        
        .detail-label {
            font-weight: 600;
            color: #6c757d;
            font-size: 0.9rem;
        }
        
        .detail-value {
            font-weight: 600;
            color: var(--secondary-color);
            font-size: 1.1rem;
        }
        
        .items-table {
            width: 100%;
            margin: 20px 0;
            border-collapse: collapse;
        }
        
        .items-table th {
            background: #f8f9fa;
            padding: 12px;
            color: var(--secondary-color);
        }
        
        .items-table td {
            padding: 12px;
        }
        
        .total-section {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 8px;
            margin-top: 20px;
        }
        
        .hotel-name {
            color: var(--primary-color);
            font-weight: bold;
        }
        
        @media print {
            .no-print { display: none; }
            .receipt-print { 
                font-family: 'Courier New', monospace;
                max-width: 300px;
                margin: 0 auto;
            }
        }
        
        .pagination {
            margin-top: 20px;
            justify-content: center;
        }
        
        .page-link {
            color: var(--primary-color);
            border: none;
            margin: 0 3px;
            border-radius: 5px;
        }
        
        .page-item.active .page-link {
            background: var(--primary-color);
            color: white;
        }
        
        .badge {
            font-size: 0.85rem;
            padding: 5px 10px;
        }
        
        .table-responsive {
            overflow-x: auto;
        }
        
        .form-control, .form-select {
            border-radius: 8px;
            border: 1px solid #dee2e6;
            padding: 10px 15px;
        }
        
        .form-control:focus, .form-select:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 0.2rem rgba(26, 147, 111, 0.25);
        }
    </style>
    
    @php
        $defaultSettings = [
            'name' => hotelName(),
            'address' => hotelAddress(),
            'phone' => hotelPhone(),
            'vat_rate' => setting('tax_rate', 18),
            'service_charge' => setting('pos_service_charge', 10),
            'currency' => 'UGX'
        ];
        
        $mergedSettings = isset($hotelSettings) ? array_merge($defaultSettings, $hotelSettings) : $defaultSettings;
    @endphp
    
    <script>
        window.hotelSettings = @json($mergedSettings);
    </script>
</head>
<body>
    <div class="container-fluid">
        <!-- Header -->
        <div class="header">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <h2 class="mb-2"><i class="fas fa-receipt me-2"></i>Orders Management</h2>
                    <p class="mb-0 opacity-75">Track and manage all customer orders</p>
                </div>
                <div>
                    <a href="{{ route('pos.index') }}" class="btn btn-light me-2">
                        <i class="fas fa-cash-register"></i> POS System
                    </a>
                    <a href="{{ route('menu-items.index') }}" class="btn btn-outline-light">
                        <i class="fas fa-utensils"></i> Menu Items
                    </a>
                </div>
            </div>
        </div>

        <!-- Stats Cards -->
        <div class="row g-4">
            <div class="col-md-3">
                <div class="stats-card">
                    <h3>{{ $totalOrders ?? 0 }}</h3>
                    <p>Total Orders</p>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stats-card">
                    <h3>{{ $pendingOrders ?? 0 }}</h3>
                    <p>Pending</p>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stats-card">
                    <h3>{{ $processingOrders ?? 0 }}</h3>
                    <p>Processing</p>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stats-card">
                    <h3>{{ $completedOrders ?? 0 }}</h3>
                    <p>Completed</p>
                </div>
            </div>
        </div>

        <!-- Filters -->
        <div class="filter-card">
            <form method="GET" action="{{ route('pos.orders.index') }}">
                <div class="row g-3">
                    <div class="col-md-3">
                        <select name="status" class="form-select">
                            <option value="">All Status</option>
                            <option value="pending" {{ request('status') == 'pending' ? 'selected' : '' }}>Pending</option>
                            <option value="processing" {{ request('status') == 'processing' ? 'selected' : '' }}>Processing</option>
                            <option value="completed" {{ request('status') == 'completed' ? 'selected' : '' }}>Completed</option>
                            <option value="cancelled" {{ request('status') == 'cancelled' ? 'selected' : '' }}>Cancelled</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <input type="date" name="date_from" class="form-control" value="{{ request('date_from') }}" placeholder="From Date">
                    </div>
                    <div class="col-md-2">
                        <input type="date" name="date_to" class="form-control" value="{{ request('date_to') }}" placeholder="To Date">
                    </div>
                    <div class="col-md-2">
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="fas fa-filter me-2"></i>Filter
                        </button>
                    </div>
                    <div class="col-md-3">
                        <a href="{{ route('pos.orders.index') }}" class="btn btn-secondary w-100">
                            <i class="fas fa-sync me-2"></i>Reset
                        </a>
                    </div>
                </div>
            </form>
        </div>

        <!-- Sample Data Alert -->
        @if(isset($isSampleData) && $isSampleData)
        <div class="alert alert-warning alert-dismissible fade show" role="alert">
            <i class="fas fa-info-circle me-2"></i>
            You are viewing sample data. 
            <a href="{{ route('pos.index') }}" class="alert-link">Create real orders</a> in the POS system.
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        @endif

        <!-- Orders Table -->
        <div class="table-container">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>Order #</th>
                            <th>Customer</th>
                            <th>Type</th>
                            <th>Items</th>
                            <th>Total</th>
                            <th>Status</th>
                            <th>Date (UG Time)</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($orders as $order)
                        <tr>
                            <td>
                                <span class="order-number">{{ $order->order_number }}</span>
                                <br>
                                <small class="text-muted">ID: {{ $order->id }}</small>
                            </td>
                            <td>
                                <strong>{{ $order->customer_name ?? 'Walk-in Customer' }}</strong>
                                @if($order->table_number)
                                    <br><small><i class="fas fa-chair me-1"></i>Table {{ $order->table_number }}</small>
                                @endif
                                @if($order->room_number)
                                    <br><small><i class="fas fa-door-open me-1"></i>Room {{ $order->room_number }}</small>
                                @endif
                            </td>
                            <td>
                                @switch($order->order_type)
                                    @case('room_service')
                                        <span class="badge bg-info">Room Service</span>
                                        @break
                                    @case('restaurant')
                                        <span class="badge bg-success">Restaurant</span>
                                        @break
                                    @case('takeaway')
                                        <span class="badge bg-warning text-dark">Takeaway</span>
                                        @break
                                    @default
                                        <span class="badge bg-secondary">{{ $order->order_type }}</span>
                                @endswitch
                            </td>
                            <td>
                                <span class="badge bg-light text-dark">
                                    {{ $order->items_count ?? '0' }} items
                                </span>
                            </td>
                            <td>
                                <span class="amount">{{ $mergedSettings['currency'] ?? 'UGX' }} {{ number_format($order->total_amount ?? 0, 2) }}</span>
                            </td>
                            <td>
                                <span class="status-badge status-{{ $order->status }}">
                                    {{ ucfirst($order->status) }}
                                </span>
                            </td>
                            <td>
                                <small>
                                    <i class="far fa-clock me-1"></i>
                                    {{ $order->created_at ? \Carbon\Carbon::parse($order->created_at)->setTimezone('Africa/Kampala')->format('d/m/Y H:i') : 'N/A' }}
                                </small>
                            </td>
                            <td>
                                <div class="d-flex gap-1">
                                    <button class="btn-action btn-show" onclick="showOrderDetails({{ $order->id }})" title="View Details">
                                        <i class="fas fa-eye"></i>
                                    </button>
                                    
                                    <button class="btn-action btn-receipt" onclick="showReceipt({{ $order->id }})" title="View Receipt">
                                        <i class="fas fa-receipt"></i>
                                    </button>
                                    
                                    @if($order->status != 'completed' && $order->status != 'cancelled')
                                    <form action="{{ route('pos.orders.updateStatus', $order->id) }}" method="POST" class="d-inline">
                                        @csrf
                                        @method('PUT')
                                        <input type="hidden" name="status" value="completed">
                                        <button type="submit" class="btn-action btn-complete" onclick="return confirm('Mark this order as completed?')" title="Mark as Complete">
                                            <i class="fas fa-check"></i>
                                        </button>
                                    </form>
                                    @endif
                                </div>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="8" class="text-center py-5">
                                <i class="fas fa-receipt fa-4x text-muted mb-3"></i>
                                <h5 class="text-muted">No Orders Found</h5>
                                <p class="text-muted mb-4">No orders have been placed yet</p>
                                <a href="{{ route('pos.index') }}" class="btn btn-primary">
                                    <i class="fas fa-cash-register me-2"></i>Go to POS
                                </a>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <!-- Pagination -->
            @if(method_exists($orders, 'links'))
            <div class="d-flex justify-content-center mt-4">
                {{ $orders->links() }}
            </div>
            @endif
        </div>
    </div>

    <!-- Order Details Modal -->
    <div class="modal fade" id="orderDetailsModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">
                        <i class="fas fa-eye me-2"></i>Order Details
                    </h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body" id="orderDetailsContent">
                    <div class="text-center py-4">
                        <div class="spinner-border text-primary" role="status">
                            <span class="visually-hidden">Loading...</span>
                        </div>
                        <p class="mt-2 text-muted">Loading order details...</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Receipt Modal -->
    <div class="modal fade" id="receiptModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">
                        <i class="fas fa-receipt me-2"></i>Receipt
                    </h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body" id="receiptContent">
                    <div class="text-center py-4">
                        <div class="spinner-border text-primary" role="status">
                            <span class="visually-hidden">Loading...</span>
                        </div>
                        <p class="mt-2 text-muted">Generating receipt...</p>
                    </div>
                </div>
                <div class="modal-footer no-print">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary" onclick="printReceipt()">
                        <i class="fas fa-print me-2"></i>Print
                    </button>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
  
    <script>
        let currentReceiptData = null;
        const hotel = window.hotelSettings || {
            name: '{{ hotelName() }}',
            address: '{{ hotelAddress() }}',
            phone: '{{ hotelPhone() }}',
            vat_rate: {{ setting('tax_rate', 18) }},
            service_charge: {{ setting('pos_service_charge', 10) }},
            currency: 'UGX'
        };

        async function showOrderDetails(orderId) {
            const modal = new bootstrap.Modal(document.getElementById('orderDetailsModal'));
            modal.show();

            try {
                const response = await fetch(`/pos/orders/${orderId}/details`);
                const data = await response.json();

                if (data.success) {
                    displayOrderDetails(data.data);
                } else {
                    document.getElementById('orderDetailsContent').innerHTML = `
                        <div class="alert alert-danger">Failed to load order details</div>
                    `;
                }
            } catch (error) {
                console.error('Error:', error);
                document.getElementById('orderDetailsContent').innerHTML = `
                    <div class="alert alert-danger">Error loading order details</div>
                `;
            }
        }

        function displayOrderDetails(order) {
            const ugTime = new Date(order.created_at).toLocaleString('en-UG', { 
                timeZone: 'Africa/Kampala',
                year: 'numeric',
                month: 'long',
                day: 'numeric',
                hour: '2-digit',
                minute: '2-digit'
            });

            let itemsHtml = '';
            if (order.items && order.items.length > 0) {
                order.items.forEach(item => {
                    itemsHtml += `
                        <tr>
                            <td>${item.name}</td>
                            <td class="text-center">${item.quantity}</td>
                            <td class="text-end">${hotel.currency} ${formatNumber(item.unit_price)}</td>
                            <td class="text-end">${hotel.currency} ${formatNumber(item.total_price)}</td>
                        </tr>
                    `;
                });
            }

            const html = `
                <div class="detail-row">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="detail-label">Order Number</div>
                            <div class="detail-value">${order.order_number}</div>
                        </div>
                        <div class="col-md-6">
                            <div class="detail-label">Status</div>
                            <div class="detail-value">
                                <span class="status-badge status-${order.status}">${order.status}</span>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="detail-row">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="detail-label">Customer</div>
                            <div class="detail-value">${order.customer_name || 'Walk-in Customer'}</div>
                        </div>
                        <div class="col-md-6">
                            <div class="detail-label">Order Type</div>
                            <div class="detail-value">${order.order_type_display || order.order_type}</div>
                        </div>
                    </div>
                </div>

                <div class="detail-row">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="detail-label">Date & Time</div>
                            <div class="detail-value">${ugTime}</div>
                        </div>
                        <div class="col-md-6">
                            <div class="detail-label">Location</div>
                            <div class="detail-value">
                                ${order.table_number ? 'Table ' + order.table_number : ''}
                                ${order.room_number ? 'Room ' + order.room_number : ''}
                                ${!order.table_number && !order.room_number ? 'N/A' : ''}
                            </div>
                        </div>
                    </div>
                </div>

                ${order.special_instructions ? `
                <div class="detail-row">
                    <div class="detail-label">Special Instructions</div>
                    <div class="detail-value">${order.special_instructions}</div>
                </div>
                ` : ''}

                <h6 class="mt-4 mb-3">Order Items</h6>
                <table class="items-table">
                    <thead>
                        <tr>
                            <th>Item</th>
                            <th class="text-center">Qty</th>
                            <th class="text-end">Unit Price</th>
                            <th class="text-end">Total</th>
                        </tr>
                    </thead>
                    <tbody>
                        ${itemsHtml || '<tr><td colspan="4" class="text-center">No items found</td></tr>'}
                    </tbody>
                </table>

                <div class="total-section">
                    <div class="row">
                        <div class="col-md-6 offset-md-6">
                            <table class="w-100">
                                <tr>
                                    <td>Subtotal:</td>
                                    <td class="text-end">${hotel.currency} ${formatNumber(order.subtotal || 0)}</td>
                                </tr>
                                <tr>
                                    <td>Tax (${hotel.vat_rate}%):</td>
                                    <td class="text-end">${hotel.currency} ${formatNumber(order.tax_amount || 0)}</td>
                                </tr>
                                <tr>
                                    <td>Service (${hotel.service_charge || 10}%)::</td>
                                    <td class="text-end">${hotel.currency} ${formatNumber(order.service_charge || 0)}</td>
                                </tr>
                                <tr>
                                    <td><strong>Total:</strong></td>
                                    <td class="text-end"><strong>${hotel.currency} ${formatNumber(order.total_amount || 0)}</strong></td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>
            `;

            document.getElementById('orderDetailsContent').innerHTML = html;
        }

        async function showReceipt(orderId) {
            const modal = new bootstrap.Modal(document.getElementById('receiptModal'));
            modal.show();

            try {
                const response = await fetch(`/pos/orders/${orderId}/receipt`);
                const data = await response.json();

                if (data.success) {
                    currentReceiptData = data.data;
                    displayReceipt(data.data);
                } else {
                    document.getElementById('receiptContent').innerHTML = `
                        <div class="alert alert-danger">Failed to generate receipt</div>
                    `;
                }
            } catch (error) {
                console.error('Error:', error);
                document.getElementById('receiptContent').innerHTML = `
                    <div class="alert alert-danger">Error generating receipt</div>
                `;
            }
        }

        function displayReceipt(order) {
            const ugTime = new Date(order.created_at).toLocaleString('en-UG', { 
                timeZone: 'Africa/Kampala',
                year: 'numeric',
                month: '2-digit',
                day: '2-digit',
                hour: '2-digit',
                minute: '2-digit'
            });

            let itemsHtml = '';
            if (order.items && order.items.length > 0) {
                order.items.forEach(item => {
                    itemsHtml += `
                        <tr>
                            <td>${item.name}</td>
                            <td class="text-center">${item.quantity}</td>
                            <td class="text-end">${hotel.currency} ${formatNumber(item.total_price)}</td>
                        </tr>
                    `;
                });
            }

            const html = `
                <div class="receipt-print">
                    <div class="text-center mb-3">
                        <h5 class="hotel-name">${hotel.name}</h5>
                        <p class="mb-0 small">${hotel.address}</p>
                        <p class="small">Tel: ${hotel.phone}</p>
                        <hr>
                    </div>

                    <div class="small">
                        <p><strong>Order #:</strong> ${order.order_number}</p>
                        <p><strong>Date:</strong> ${ugTime}</p>
                        <p><strong>Customer:</strong> ${order.customer_name || 'Walk-in Customer'}</p>
                        <p><strong>Type:</strong> ${order.order_type_display || order.order_type}</p>
                        ${order.table_number ? `<p><strong>Table:</strong> ${order.table_number}</p>` : ''}
                        ${order.room_number ? `<p><strong>Room:</strong> ${order.room_number}</p>` : ''}
                    </div>

                    <hr>

                    <table class="w-100 small">
                        <thead>
                            <tr>
                                <th>Item</th>
                                <th class="text-center">Qty</th>
                                <th class="text-end">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            ${itemsHtml || '<tr><td colspan="3" class="text-center">No items</td></tr>'}
                        </tbody>
                    </table>

                    <hr>

                    <div class="small">
                        <div class="d-flex justify-content-between">
                            <span>Subtotal:</span>
                            <span>${hotel.currency} ${formatNumber(order.subtotal || 0)}</span>
                        </div>
                        <div class="d-flex justify-content-between">
                            <span>Tax (${hotel.vat_rate}%):</span>
                            <span>${hotel.currency} ${formatNumber(order.tax_amount || 0)}</span>
                        </div>
                        <div class="d-flex justify-content-between">
                            <span>Service (${hotel.service_charge || 10}%):</span>
                            <span>${hotel.currency} ${formatNumber(order.service_charge || 0)}</span>
                        </div>
                        <hr>
                        <div class="d-flex justify-content-between fw-bold">
                            <span>TOTAL:</span>
                            <span>${hotel.currency} ${formatNumber(order.total_amount || 0)}</span>
                        </div>
                    </div>

                    <hr>

                    <div class="text-center small">
                        <p>Thank you for your order!</p>
                        <p>Please visit again</p>
                    </div>
                </div>
            `;

            document.getElementById('receiptContent').innerHTML = html;
        }

        function printReceipt() {
            if (!currentReceiptData) return;

            const printWindow = window.open('', '_blank');
            printWindow.document.write(`
                <!DOCTYPE html>
                <html>
                <head>
                    <title>Receipt - ${currentReceiptData.order_number}</title>
                    <style>
                        body { 
                            font-family: 'Courier New', monospace; 
                            padding: 20px;
                            max-width: 300px;
                            margin: 0 auto;
                        }
                        .text-center { text-align: center; }
                        .text-end { text-align: right; }
                        table { width: 100%; border-collapse: collapse; }
                        td, th { padding: 4px; }
                        hr { border-top: 1px dashed #000; }
                        .small { font-size: 14px; }
                        .hotel-name { color: #1a936f; font-weight: bold; }
                    </style>
                </head>
                <body>
                    ${document.getElementById('receiptContent').innerHTML}
                    <script>
                        window.onload = function() { window.print(); window.close(); }
                    <\/script>
                </body>
                </html>
            `);
            printWindow.document.close();
        }

        function formatNumber(num) {
            return parseFloat(num || 0).toLocaleString('en-US', {
                minimumFractionDigits: 2,
                maximumFractionDigits: 2
            });
        }
    </script>
      <script src="{{ asset('js/theme-applier.js') }}"></script>
</body>
</html>
