<?php
if (Auth('admin')->User()->dashboard_style == 'light') {
    $text = 'dark';
} else {
    $text = 'light';
}
?>
@extends('layouts.app')
@section('content')
    @include('admin.topmenu')
    @include('admin.sidebar')
    <div class="main-panel">
        <div class="content">
            <div class="page-inner">
                <div class="page-header">
                    <h4 class="page-title text-{{ $text }}">Order #{{ $order->order_number }}</h4>
                    <ul class="breadcrumbs">
                        <li class="nav-home"><a href="{{ route('admin.dashboard') }}"><i class="flaticon-home"></i></a></li>
                        <li class="separator"><i class="flaticon-right-arrow"></i></li>
                        <li class="nav-item"><a href="{{ route('admin.car-orders.index') }}">Car Orders</a></li>
                        <li class="separator"><i class="flaticon-right-arrow"></i></li>
                        <li class="nav-item"><span class="text-{{ $text }}">{{ $order->order_number }}</span></li>
                    </ul>
                </div>

                @if(session('success'))
                    <div class="alert alert-success alert-dismissible fade show">
                        {{ session('success') }}
                        <button type="button" class="close" data-dismiss="alert"><span>&times;</span></button>
                    </div>
                @endif

                {{-- Status Change --}}
                <div class="card mb-3">
                    <div class="card-body py-3">
                        <form action="{{ route('admin.car-orders.update-status', $order) }}" method="POST" class="form-inline">
                            @csrf @method('PUT')
                            <label class="mr-2"><strong>Change Status:</strong></label>
                            <select name="status" class="form-control mr-2">
                                @foreach(['pending','approved','shipped','in-transit','delivered','completed','cancelled','refunded'] as $s)
                                    <option value="{{ $s }}" {{ $order->status == $s ? 'selected' : '' }}>{{ ucfirst($s) }}</option>
                                @endforeach
                            </select>
                            <button type="submit" class="btn btn-primary"><i class="fas fa-sync"></i> Update Status</button>
                        </form>
                    </div>
                </div>

                <div class="row">
                    {{-- Left: Customer & Payment --}}
                    <div class="col-md-6">
                        <div class="card">
                            <div class="card-header"><h4 class="card-title text-{{ $text }}">Customer & Billing</h4></div>
                            <div class="card-body">
                                <table class="table table-borderless table-sm">
                                    <tr><td><strong>Name:</strong></td><td>{{ $order->billing_name }}</td></tr>
                                    <tr><td><strong>Email:</strong></td><td>{{ $order->billing_email }}</td></tr>
                                    <tr><td><strong>Phone:</strong></td><td>{{ $order->billing_phone ?? 'N/A' }}</td></tr>
                                    <tr><td><strong>Company:</strong></td><td>{{ $order->billing_company ?? 'N/A' }}</td></tr>
                                    <tr><td><strong>Address:</strong></td><td>{{ $order->billing_address }}</td></tr>
                                    <tr><td><strong>City:</strong></td><td>{{ $order->billing_city }}</td></tr>
                                    <tr><td><strong>State:</strong></td><td>{{ $order->billing_state ?? 'N/A' }}</td></tr>
                                    <tr><td><strong>Postal Code:</strong></td><td>{{ $order->billing_postal_code ?? 'N/A' }}</td></tr>
                                    <tr><td><strong>Country:</strong></td><td>{{ $order->billing_country }}</td></tr>
                                    <tr><td><strong>User Account:</strong></td><td>{{ $order->user->name ?? 'N/A' }} ({{ $order->user->email ?? '' }})</td></tr>
                                </table>
                            </div>
                        </div>

                        <div class="card">
                            <div class="card-header"><h4 class="card-title text-{{ $text }}">Payment Details</h4></div>
                            <div class="card-body">
                                <table class="table table-borderless table-sm">
                                    <tr><td><strong>Method:</strong></td><td>{{ $order->payment_method }}</td></tr>
                                    <tr>
                                        <td><strong>Status:</strong></td>
                                        <td>
                                            @if($order->payment_status == 'verified')
                                                <span class="badge badge-success">Verified</span>
                                            @elseif($order->payment_status == 'failed')
                                                <span class="badge badge-danger">Failed</span>
                                            @else
                                                <span class="badge badge-warning">Pending</span>
                                                <form action="{{ route('admin.car-orders.verify-payment', $order) }}" method="POST" class="d-inline ml-2">
                                                    @csrf
                                                    <button class="btn btn-success btn-sm"><i class="fas fa-check"></i> Verify</button>
                                                </form>
                                            @endif
                                        </td>
                                    </tr>
                                    <tr><td><strong>Unit Price:</strong></td><td>${{ number_format($order->unit_price, 2) }}</td></tr>
                                    <tr><td><strong>Quantity:</strong></td><td>{{ $order->quantity }}</td></tr>
                                    <tr><td><strong>Total:</strong></td><td><strong>${{ number_format($order->total_price, 2) }}</strong></td></tr>
                                </table>

                                @if($order->payment_proof)
                                    <hr>
                                    <p><strong>Payment Proof:</strong></p>
                                    <a href="{{ asset('dash/' . $order->payment_proof) }}" target="_blank">
                                        <img src="{{ asset('dash/' . $order->payment_proof) }}" class="img-fluid rounded" style="max-height:300px;">
                                    </a>
                                @endif
                            </div>
                        </div>
                    </div>

                    {{-- Right: Car & Tracking --}}
                    <div class="col-md-6">
                        <div class="card">
                            <div class="card-header"><h4 class="card-title text-{{ $text }}">Car Details</h4></div>
                            <div class="card-body text-center">
                                @if($order->car && $order->car->photos->count() > 0)
                                    <img src="{{ asset('dash/' . $order->car->primary_photo) }}" class="img-fluid rounded mb-3" style="max-height:200px;">
                                @endif
                                <h5>{{ $order->car->name ?? 'Deleted Car' }}</h5>
                                @if($order->car)
                                    <p class="text-muted">{{ $order->car->make }} {{ $order->car->model }} {{ $order->car->year }} &bull; {{ $order->car->color }}</p>
                                @endif
                            </div>
                        </div>

                        {{-- Tracking Section --}}
                        <div class="card">
                            <div class="card-header"><h4 class="card-title text-{{ $text }}">Shipment Tracking</h4></div>
                            <div class="card-body">
                                @if($order->tracking_number)
                                    <p><strong>Tracking #:</strong> {{ $order->tracking_number }}</p>
                                @endif
                                @if($order->tracking_status)
                                    <p><strong>Current Status:</strong> <span class="badge badge-info">{{ $order->tracking_status }}</span></p>
                                @endif

                                {{-- Timeline --}}
                                @if($order->tracking_updates && count($order->tracking_updates) > 0)
                                    <div class="mt-3">
                                        @foreach(array_reverse($order->tracking_updates) as $update)
                                            <div class="d-flex mb-3">
                                                <div class="mr-3">
                                                    <div class="bg-primary rounded-circle d-flex align-items-center justify-content-center" style="width:32px;height:32px;">
                                                        <i class="fas fa-truck text-white" style="font-size:14px;"></i>
                                                    </div>
                                                </div>
                                                <div>
                                                    <strong>{{ $update['status'] }}</strong>
                                                    <br><small class="text-muted">{{ $update['date'] }}</small>
                                                    @if(!empty($update['note']))
                                                        <br><span>{{ $update['note'] }}</span>
                                                    @endif
                                                </div>
                                            </div>
                                        @endforeach
                                    </div>
                                @else
                                    <p class="text-muted">No tracking updates yet.</p>
                                @endif

                                <hr>
                                <h6>Add Tracking Update</h6>
                                <form action="{{ route('admin.car-orders.add-tracking', $order) }}" method="POST">
                                    @csrf
                                    <div class="form-group">
                                        <input type="text" name="tracking_number" class="form-control" placeholder="Tracking Number (optional)" value="{{ $order->tracking_number }}">
                                    </div>
                                    <div class="form-group">
                                        <input type="text" name="tracking_status" class="form-control" placeholder="Status (e.g. Shipped, In Transit, Delivered)" required>
                                    </div>
                                    <div class="form-group">
                                        <textarea name="tracking_note" class="form-control" rows="2" placeholder="Note (optional)"></textarea>
                                    </div>
                                    <button type="submit" class="btn btn-primary btn-block"><i class="fas fa-plus"></i> Add Update</button>
                                </form>
                            </div>
                        </div>

                        {{-- Admin Notes --}}
                        <div class="card">
                            <div class="card-header"><h4 class="card-title text-{{ $text }}">Quick Actions</h4></div>
                            <div class="card-body">
                                @if(!in_array($order->status, ['cancelled', 'refunded']))
                                    <form action="{{ route('admin.car-orders.cancel', $order) }}" method="POST" class="mb-2" onsubmit="return confirm('Cancel this order? Car quantity will be restored.')">
                                        @csrf
                                        <button class="btn btn-danger btn-block"><i class="fas fa-ban"></i> Cancel Order</button>
                                    </form>
                                    <form action="{{ route('admin.car-orders.refund', $order) }}" method="POST" onsubmit="return confirm('Refund this order? Car quantity will be restored.')">
                                        @csrf
                                        <button class="btn btn-warning btn-block"><i class="fas fa-undo"></i> Refund Order</button>
                                    </form>
                                @else
                                    <p class="text-muted text-center">Order is {{ $order->status }}.</p>
                                @endif
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
