@extends('admin.layouts.app')
@section('title', 'Orders')
@section('content')
<div class="page-header d-print-none">
    <div class="container-xl">
        <div class="row g-2 align-items-center">
            <div class="col"><h2 class="page-title">Orders</h2></div>
        </div>
    </div>
</div>
<div class="page-body">
    <div class="card mb-3">
        <div class="card-body">
            <form method="GET" class="row g-3">
                <div class="col-md-3">
                    <input type="text" name="search" class="form-control" placeholder="Order # or customer name..." value="{{ request('search') }}">
                </div>
                <div class="col-md-2">
                    <select name="type" class="form-select">
                        <option value="">All Types</option>
                        @foreach(['ride','food','delivery','freelance'] as $t)
                            <option value="{{ $t }}" {{ request('type')==$t?'selected':'' }}>{{ ucfirst($t) }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2">
                    <select name="status" class="form-select">
                        <option value="">All Statuses</option>
                        @foreach(['pending','accepted','in_progress','completed','cancelled','disputed'] as $s)
                            <option value="{{ $s }}" {{ request('status')==$s?'selected':'' }}>{{ ucwords(str_replace('_',' ',$s)) }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-3 d-flex gap-2">
                    <button type="submit" class="btn btn-primary">Filter</button>
                    <a href="{{ route('admin.orders.index') }}" class="btn btn-outline-secondary">Clear</a>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-header"><h3 class="card-title">All Orders ({{ $orders->total() }})</h3></div>
        <div class="table-responsive">
            <table class="table table-vcenter card-table">
                <thead>
                    <tr><th>Order #</th><th>Customer</th><th>Vendor</th><th>Type</th><th>Total</th><th>Payment</th><th>Status</th><th>Date</th><th>Actions</th></tr>
                </thead>
                <tbody>
                    @forelse($orders as $order)
                    <tr>
                        <td><a href="{{ route('admin.orders.show', $order) }}" class="fw-medium">{{ $order->order_number }}</a></td>
                        <td>{{ $order->user?->name ?? '—' }}</td>
                        <td>{{ $order->vendor?->business_name ?? '—' }}</td>
                        <td><span class="badge bg-blue-lt text-capitalize">{{ $order->type }}</span></td>
                        <td>₹{{ number_format($order->total, 2) }}</td>
                        <td><span class="badge bg-indigo-lt text-capitalize">{{ $order->payment_method }}</span></td>
                        <td>
                            @php $c=['pending'=>'warning','accepted'=>'info','in_progress'=>'primary','completed'=>'success','cancelled'=>'danger','disputed'=>'red']; @endphp
                            <span class="badge bg-{{ $c[$order->status]??'secondary' }}-lt text-capitalize">{{ str_replace('_',' ',$order->status) }}</span>
                        </td>
                        <td>{{ $order->created_at->format('d M Y') }}</td>
                        <td>
                            <div class="d-flex gap-1">
                                <a href="{{ route('admin.orders.show', $order) }}" class="btn btn-sm btn-outline-secondary">View</a>
                                @if(!in_array($order->status,['cancelled','completed']))
                                <button class="btn btn-sm btn-danger" data-bs-toggle="modal" data-bs-target="#cancelOrder{{ $order->id }}">Cancel</button>
                                @endif
                            </div>
                        </td>
                    </tr>
                    <div class="modal modal-blur fade" id="cancelOrder{{ $order->id }}" tabindex="-1">
                        <div class="modal-dialog modal-sm modal-dialog-centered"><div class="modal-content">
                            <form action="{{ route('admin.orders.cancel', $order) }}" method="POST">@csrf
                                <div class="modal-header"><h5 class="modal-title">Cancel Order</h5></div>
                                <div class="modal-body"><textarea name="reason" class="form-control" rows="3" placeholder="Reason..." required></textarea></div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-outline-secondary btn-sm" data-bs-dismiss="modal">Cancel</button>
                                    <button type="submit" class="btn btn-danger btn-sm">Confirm Cancel</button>
                                </div>
                            </form>
                        </div></div>
                    </div>
                    @empty
                    <tr><td colspan="9" class="text-center text-muted py-4">No orders found</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="card-footer d-flex align-items-center">{{ $orders->links() }}</div>
    </div>
</div>
@endsection
