@extends('layouts.store')

@section('title', 'Track Order')

@section('content')
    <section class="dashboard-head">
        <div>
            <span class="eyebrow">Tracking</span>
            <h1>Track Order</h1>
        </div>
    </section>

    <section class="tracking-layout">
        <article class="panel tracking-search-panel">
            <h2>Order Lookup</h2>
            <form method="GET" action="{{ route('orders.track') }}" class="admin-form">
                <label>
                    <span>Order number</span>
                    <input name="order_number" value="{{ old('order_number', $orderNumber) }}" placeholder="ORD-20260624103000-1" required>
                </label>

                <label>
                    <span>Delivery phone</span>
                    <input name="delivery_phone" value="{{ old('delivery_phone', $deliveryPhone) }}" placeholder="03001234567" required>
                </label>

                @if ($errors->any())
                    <p class="form-error">{{ $errors->first() }}</p>
                @endif

                @if ($hasSearch && ! $order && ! $errors->any())
                    <p class="form-error">No order found with this order number and delivery phone.</p>
                @endif

                <button class="button primary" type="submit">Track order</button>
            </form>
        </article>

        <article class="panel tracking-result-panel">
            @if ($order)
                <div class="tracking-summary">
                    <div>
                        <span class="eyebrow">Current Status</span>
                        <h2>{{ $order->statusLabel() }}</h2>
                        <p>{{ $order->order_number }}</p>
                    </div>
                    <strong>{{ \App\Models\SiteSetting::formatPrice($order->total) }}</strong>
                </div>

                <ol class="tracking-timeline">
                    @foreach ($order->trackingSteps() as $step)
                        <li @class(['tracking-step', 'completed' => $step['completed'], 'current' => $step['current']])>
                            <span></span>
                            <div>
                                <strong>{{ $step['label'] }}</strong>
                                <small>
                                    @if ($step['timestamp'])
                                        {{ $step['timestamp']->format('d M Y, h:i A') }}
                                    @elseif ($step['completed'])
                                        Updated
                                    @else
                                        Pending
                                    @endif
                                </small>
                            </div>
                        </li>
                    @endforeach
                </ol>

                <div class="tracking-details">
                    <dl class="account-details">
                        <div><dt>Payment</dt><dd>{{ strtoupper($order->payment_method) }}</dd></div>
                        @if ($order->payment_approval_note)
                            <div><dt>Payment Approval Note</dt><dd>{{ $order->payment_approval_note }}</dd></div>
                        @endif
                        @if ($order->payment_rejection_reason)
                            <div><dt>Payment Rejection Reason</dt><dd>{{ $order->payment_rejection_reason }}</dd></div>
                        @endif
                        <div><dt>Delivery To</dt><dd>{{ $order->delivery_recipient_name ?: $order->customer?->name }} - {{ $order->delivery_phone }}</dd></div>
                        <div><dt>Address</dt><dd>{{ collect([$order->delivery_address_line, $order->delivery_area, $order->delivery_city])->filter()->implode(', ') ?: 'Not added' }}</dd></div>
                    </dl>
                </div>

                <div class="table-wrap">
                    <table>
                        <thead>
                            <tr>
                                <th>Product</th>
                                <th>Qty</th>
                                <th>Total</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($order->items as $item)
                                <tr>
                                    <td>
                                        {{ $item->product_name }}
                                        @if ($item->variant_label)
                                            <small>{{ $item->variant_label }}</small>
                                        @endif
                                    </td>
                                    <td>{{ $item->quantity }}</td>
                                    <td>{{ \App\Models\SiteSetting::formatPrice($item->line_total) }}</td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            @else
                <div class="tracking-empty">
                    <span class="eyebrow">Status</span>
                    <h2>Enter your order details</h2>
                    <p>Use your order number and delivery phone to view the latest payment, processing, delivery, and completion status.</p>
                </div>
            @endif
        </article>
    </section>
@endsection
