@extends('layouts.app', ['title' => 'Order Details | Print Cards'])

@section('content')
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="h3 mb-1">{{ $order->order_number }}</h1>
            <p class="text-muted mb-0">Status: {{ ucfirst($order->status) }} | Payment: {{ ucfirst($order->payment_status) }}</p>
        </div>
        <div class="d-flex gap-2">
            <a href="{{ route('orders.invoice', $order) }}" class="btn btn-outline-primary">Invoice</a>
            @if($order->payment_status !== 'paid')
                <a href="{{ route('payments.show', $order) }}" class="btn btn-primary">Pay Now</a>
            @endif
            <a href="{{ route('orders.index') }}" class="btn btn-outline-secondary">Back to Orders</a>
        </div>
    </div>

    <div class="row g-4">
        <div class="col-lg-8">
            <div class="card card-soft mb-4">
                <div class="card-body">
                    <h2 class="h5 mb-3">Order Items</h2>
                    @foreach($order->items as $item)
                        <div class="border rounded-4 p-3 mb-3">
                            <div class="fw-semibold">{{ $item->item_name }}</div>
                            <div class="small text-muted">{{ $item->page_size }} | {{ strtoupper($item->print_mode) }} | {{ ucfirst($item->sides) }} sided | {{ $item->copies }} copies</div>
                            <div class="small text-muted">Binding: {{ ucfirst($item->binding_type) }} | Lamination: {{ ucfirst($item->lamination_type) }} | PVC: {{ $item->pvc_printing ? 'Yes' : 'No' }}</div>
                            <div class="mt-2"><a href="{{ route('documents.download', $item->document) }}" class="btn btn-sm btn-outline-primary">Download Document</a></div>
                        </div>
                    @endforeach
                </div>
            </div>

            <div class="card card-soft">
                <div class="card-body">
                    <h2 class="h5 mb-3">Tracking Timeline</h2>
                    @forelse($order->trackingUpdates as $update)
                        <div class="timeline-item pb-4">
                            <div class="fw-semibold">{{ $update->title }}</div>
                            <div class="small text-muted">{{ ucfirst($update->status) }} | {{ $update->created_at->format('d M Y h:i A') }}</div>
                            @if($update->location)
                                <div class="small text-muted">Location: {{ $update->location }}</div>
                            @endif
                            @if($update->description)
                                <div class="mt-1">{{ $update->description }}</div>
                            @endif
                        </div>
                    @empty
                        <p class="text-muted mb-0">Tracking updates will appear here after the first workflow change.</p>
                    @endforelse
                </div>
            </div>
        </div>
        <div class="col-lg-4">
            <div class="card card-soft mb-4">
                <div class="card-body">
                    <h2 class="h5 mb-3">Billing</h2>
                    <div class="d-flex justify-content-between"><span>Subtotal</span><strong>Rs {{ number_format($order->subtotal, 2) }}</strong></div>
                    <div class="d-flex justify-content-between"><span>Discount</span><strong>Rs {{ number_format($order->discount_amount, 2) }}</strong></div>
                    <div class="d-flex justify-content-between"><span>GST</span><strong>Rs {{ number_format($order->gst_amount, 2) }}</strong></div>
                    <div class="d-flex justify-content-between"><span>Delivery</span><strong>Rs {{ number_format($order->delivery_amount, 2) }}</strong></div>
                    <hr>
                    <div class="d-flex justify-content-between"><span>Total</span><strong>Rs {{ number_format($order->total_amount, 2) }}</strong></div>
                    <div class="small text-muted mt-2">Invoice: {{ $order->invoice_number ?? 'Pending' }}</div>
                </div>
            </div>
            <div class="card card-soft">
                <div class="card-body">
                    <h2 class="h5 mb-3">Delivery Address</h2>
                    <div class="fw-semibold">{{ $order->address?->recipient_name }}</div>
                    <div>{{ $order->address?->line1 }}</div>
                    <div>{{ $order->address?->line2 }}</div>
                    <div>{{ $order->address?->city }}, {{ $order->address?->state }} - {{ $order->address?->postal_code }}</div>
                    <div class="text-muted mt-3">Courier: {{ $order->delivery_partner ?: 'Pending assignment' }}</div>
                    <div class="text-muted">{{ $order->tracking_number ? 'Tracking: ' . $order->tracking_number : 'Tracking not assigned yet' }}</div>
                </div>
            </div>
        </div>
    </div>
@endsection
