@extends('layouts.admin')

@section('title', $shipment->tracking_number)
@section('heading', $shipment->tracking_number)

@section('actions')
    <a class="btn-admin btn-back" href="{{ route('admin.shipments.index') }}">&lt;- Back</a>
@endsection

@section('content')
<div style="display:flex; gap:.6rem; flex-wrap:wrap; margin-bottom:1rem;">
    <a class="btn-admin btn-primary" href="{{ route('admin.shipments.edit', $shipment) }}">Edit</a>
    <form method="POST" action="{{ route('admin.shipments.destroy', $shipment) }}" onsubmit="return confirm('Delete this shipment?');">
        @csrf
        @method('DELETE')
        <button class="btn-admin act-delete" type="submit">Delete</button>
    </form>
</div>
<div class="grid-2">
    <div class="panel">
        <p><span class="badge {{ $shipment->status }}">{{ $shipment->statusLabel() }}</span></p>
        <p><strong>Service:</strong> {{ $shipment->service->name ?? '—' }}</p>
        <p><strong>From:</strong> {{ $shipment->sender_name }}, {{ $shipment->origin_city }}, {{ $shipment->origin_country }}</p>
        <p><strong>To:</strong> {{ $shipment->receiver_name }}, {{ $shipment->destination_city }}, {{ $shipment->destination_country }}</p>
        <p><strong>Current location:</strong> {{ $shipment->current_location }}</p>
        <p><strong>Package:</strong> {{ $shipment->package_description }} ({{ $shipment->weight_kg }} kg, {{ $shipment->pieces }} pcs)</p>
        <p><strong>Public tracking:</strong> <a href="{{ route('tracking', ['code' => $shipment->tracking_number]) }}" target="_blank">{{ $shipment->tracking_number }}</a></p>
    </div>
    <div class="panel">
        <h3>Add tracking event</h3>
        <form class="form" method="POST" action="{{ route('admin.shipments.events.store', $shipment) }}">
            @csrf
            <div class="field">
                <label>Status</label>
                <select name="status" required>
                    @foreach($statuses as $key => $label)
                        <option value="{{ $key }}" @selected($shipment->status === $key)>{{ $label }}</option>
                    @endforeach
                </select>
            </div>
            <div class="field">
                <label>Location</label>
                <input name="location" value="{{ $shipment->current_location }}" required>
            </div>
            <div class="field">
                <label>Description</label>
                <textarea name="description" required></textarea>
            </div>
            <div class="field">
                <label>Event time</label>
                <input type="datetime-local" name="event_at" value="{{ now()->format('Y-m-d\TH:i') }}" required>
            </div>
            <button class="btn" type="submit">Add event</button>
        </form>
    </div>
</div>
<div class="panel" style="margin-top:1rem;">
    <h3>History</h3>
    <div class="timeline">
        @foreach($shipment->events as $event)
            <div class="event">
                <strong>{{ $event->event_at->format('d M Y, H:i') }}</strong>
                <span class="badge {{ $event->status }}">{{ \App\Models\Shipment::STATUSES[$event->status] ?? $event->status }}</span>
                <div>{{ $event->location }}</div>
                <div>{{ $event->description }}</div>
            </div>
        @endforeach
    </div>
</div>
@endsection
