<x-app-layout>
<div class="container mt-5">
    
    <div class="d-flex justify-content-between align-items-center">
        <h2 class="text-center">Order Details</h2>
        <a href="{{ url()->previous() }}" class="btn btn-primary">
            <i class="bi bi-arrow-left"></i> Back
        </a>
    </div>

    <div class="card mt-4 shadow">
        <div class="card-body">
            <table class="table table-bordered">
                <tr>
                    <th>Order ID</th>
                    <td>{{ $order->order_id }}</td>
                </tr>
                <tr>
                    <th>Status</th>
                    <td><span class="badge bg-success">{{ ucfirst($order->status) }}</span></td>
                </tr>
                <tr>
                    <th>Created At</th>
                    <td>{{ date('d-m-Y H:i:s', strtotime($order->created_at)) }}</td>
                </tr>
            </table>
        </div>
    </div>

    <div class="row mt-4">
        <div class="col-md-6">
            <div class="card shadow">
                <div class="card-header bg-primary text-white">User Details</div>
                <div class="card-body">
                    <table class="table table-bordered">
                        <tr>
                            <th>Name</th>
                            <td>{{ $order->user_name }}</td>
                        </tr>
                        <tr>
                            <th>Email</th>
                            <td>{{ $order->user_email }}</td>
                        </tr>
                        <tr>
                            <th>Phone</th>
                            <td>{{ $order->user_phone }}</td>
                        </tr>
                        <tr>
                            <th>Address</th>
                            <td>{{ $order->customer_address }}</td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>

        <div class="col-md-6">
            <div class="card shadow">
                <div class="card-header bg-secondary text-white">Vendor Details</div>
                <div class="card-body">
                    <table class="table table-bordered">
                        <tr>
                            <th>Vendor ID</th>
                            <td>{{ $order->vendor_id }}</td>
                        </tr>
                        <tr>
                            <th>Address</th>
                            <td>{{ $order->vendor_address }}</td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <h4 class="mt-4 text-center">Payment & Costs</h4>
    <div class="card shadow">
        <div class="card-body">
            <table class="table table-bordered">
                <tr>
                    <th>User Total Cost</th>
                    <td>₹{{ number_format($order->user_total_cost, 2) }}</td>
                </tr>
                <tr>
                    <th>Vendor Total Cost</th>
                    <td>₹{{ number_format($order->vendor_total_cost, 2) }}</td>
                </tr>
                <tr>
                    <th>Cleaneo Profit</th>
                    <td>₹{{ number_format($order->cleaneo_profit, 2) }}</td>
                </tr>
                <tr>
                    <th>Cleaneo Commission</th>
                    <td>₹{{ number_format($order->cleaneo_commission, 2) }}</td>
                </tr>
                <tr>
                    <th>Delivery Charges</th>
                    <td>₹{{ number_format($order->delivery_charges, 2) }}</td>
                </tr>
                <tr>
                    <th>Payment Mode</th>
                    <td>
                        <span class="badge bg-info"><i class="bi bi-credit-card"></i> {{ ucfirst($order->payment_mode) }}</span>
                    </td>
                </tr>
            </table>
        </div>
    </div>

    <h4 class="mt-4 text-center">Items Ordered</h4>
    <div class="card shadow">
        <div class="card-body">
            <table class="table table-bordered table-hover table-striped">
                <thead class="bg-dark text-white">
                    <tr>
                        <th>Category</th>
                        <th>Item Name</th>
                        <th>Vendor Price (₹)</th>
                        <th>Customer Price (₹)</th>
                        <th>Quantity</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($items as $category => $categoryData)
                        @foreach ($categoryData['items'] as $item)
                            <tr>
                                <td>{{ $category }}</td>
                                <td>{{ $item['item_name'] ?? 'N/A' }}</td>
                                <td>₹{{ number_format($item['vendor_price'], 2) }}</td>
                                <td>₹{{ number_format($item['customer_price'], 2) }}</td>
                                <td>{{ $item['quantity'] }}</td>
                            </tr>
                        @endforeach
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>

    <h4 class="mt-4 text-center">Items by KG</h4>
    <div class="card shadow">
        <div class="card-body">
            <table class="table table-bordered table-hover table-striped">
                <thead class="bg-dark text-white">
                    <tr>
                        <th>Item Name</th>
                        <th>Weight (KG)</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($itemsByKg as $kgItem)
                        <tr>
                            <td>{{ $kgItem['name'] ?? 'N/A' }}</td>
                            <td>{{ $kgItem['count'] ?? '0' }}</td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>

</div>
</x-app-layout>
