@extends('layouts.main')

@section('content')
<div class="d-flex justify-content-between align-items-center mb-4">
    <h4>Order: {{ $order->invoice_number }}</h4>
    <div>
        <a href="{{ route('orders.receipt', $order->id) }}" class="btn btn-secondary btn-sm" target="_blank">
            <i class="bi bi-printer"></i> Print
        </a>
        <a href="{{ route('orders.index') }}" class="btn btn-secondary btn-sm">
            <i class="bi bi-arrow-left"></i> Back
        </a>
    </div>
</div>

<div class="row">
    <div class="col-md-8">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">Order Items</h5>
            </div>
            <div class="card-body table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th>Product</th>
                            <th>SKU</th>
                            <th>Price</th>
                            <th>Qty</th>
                            <th>Subtotal</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($order->items as $item)
                            <tr>
                                <td>{{ $item->product_name }}</td>
                                <td>{{ $item->product_sku }}</td>
                                <td>{{ number_format($item->price, 2) }}</td>
                                <td>{{ $item->quantity }}</td>
                                <td>{{ number_format($item->subtotal, 2) }}</td>
                            </tr>
                        @endforeach
                    </tbody>
                    <tfoot>
                        <tr>
                            <td colspan="4" class="text-end"><strong>Subtotal:</strong></td>
                            <td>{{ number_format($order->subtotal, 2) }}</td>
                        </tr>
                        @if($order->discount > 0)
                        <tr>
                            <td colspan="4" class="text-end"><strong>Discount:</strong></td>
                            <td>{{ number_format($order->discount, 2) }}</td>
                        </tr>
                        @endif
                        @if($order->tax > 0)
                        <tr>
                            <td colspan="4" class="text-end"><strong>Tax ({{ $order->tax_rate }}%):</strong></td>
                            <td>{{ number_format($order->tax, 2) }}</td>
                        </tr>
                        @endif
                        <tr>
                            <td colspan="4" class="text-end"><strong>Total:</strong></td>
                            <td><strong>{{ number_format($order->total, 2) }}</strong></td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">Order Details</h5>
            </div>
            <div class="card-body">
                <table class="table table-borderless">
                    <tr>
                        <td><strong>Invoice #:</strong></td>
                        <td>{{ $order->invoice_number }}</td>
                    </tr>
                    <tr>
                        <td><strong>Date:</strong></td>
                        <td>{{ $order->created_at->format('M d, Y H:i') }}</td>
                    </tr>
                    <tr>
                        <td><strong>Customer:</strong></td>
                        <td>{{ $order->customer->name ?? 'Walk-in' }}</td>
                    </tr>
                    <tr>
                        <td><strong>Cashier:</strong></td>
                        <td>{{ $order->user->name }}</td>
                    </tr>
                    <tr>
                        <td><strong>Payment Method:</strong></td>
                        <td>
                            @if($order->payment_methods)
                                @foreach(json_decode($order->payment_methods, true) as $pm)
                                    <span class="badge bg-info me-1">{{ ucfirst($pm['method']) }}: {{ number_format($pm['amount'], 2) }}</span>
                                @endforeach
                            @else
                                {{ ucfirst($order->payment_method) }}
                            @endif
                        </td>
                    </tr>
                    <tr>
                        <td><strong>Paid Amount:</strong></td>
                        <td>{{ number_format($order->paid_amount, 2) }}</td>
                    </tr>
                    <tr>
                        <td><strong>Change:</strong></td>
                        <td>{{ number_format($order->change_amount, 2) }}</td>
                    </tr>
                    <tr>
                        <td><strong>Status:</strong></td>
                        <td>
                            <span class="badge {{ $order->status === 'completed' ? 'bg-success' : ($order->status === 'pending' ? 'bg-warning' : 'bg-danger') }}">
                                {{ ucfirst($order->status) }}
                            </span>
                        </td>
                    </tr>
                    @if($order->notes)
                    <tr>
                        <td><strong>Notes:</strong></td>
                        <td>{{ $order->notes }}</td>
                    </tr>
                    @endif
                </table>
            </div>
        </div>
    </div>
</div>
@endsection
