@extends('layouts.layout-admin')

@section('title', 'Warehouse')

@section('header_title', 'Warehouse')
@section('header_subtitle', 'Manage order shipping')

@section('content')
    <div class="container-fluid">
        <div class="card shadow">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-center mb-3">
                    <form action="{{ route('admin.warehouse.index') }}" method="GET" class="d-flex align-items-center">
                        <div class="input-group">
                            <input type="text" name="search" class="form-control form-control-sm"
                                placeholder="Search orders..." value="{{ request('search') }}">
                            <button type="submit" class="btn btn-sm btn-outline-secondary"><i class="bi bi-search"></i></button>
                        </div>
                    </form>
                </div>

                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>Order Code</th>
                                <th>Customer</th>
                                <th>Items</th>
                                <th>Shipping</th>
                                <th>Status</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($orders as $order)
                                <tr>
                                    <td>{{ $order->order_code }}</td>
                                    <td>{{ $order->user->name }}</td>
                                    <td>
                                        @foreach ($order->items as $item)
                                            <div>{{ $item->product->name }} x {{ $item->quantity }}</div>
                                        @endforeach
                                    </td>
                                    <td>{{ $order->shippingCost->destination ?? '-' }}</td>
                                    <td><span class="badge bg-{{ $order->status_color }}">{{ $order->status }}</span></td>
                                    <td>
                                        <form action="{{ route('admin.warehouse.send', $order) }}" method="POST"
                                            class="d-inline">
                                            @csrf
                                            <button type="submit" class="btn btn-sm btn-success"
                                                onclick="return confirm('Mark order {{ $order->order_code }} as dikirim?')">
                                                <i class="bi bi-truck"></i> Kirim
                                            </button>
                                        </form>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="6" class="text-center py-4">
                                        <p class="text-muted">No orders to ship</p>
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                    <div class="mt-4">{{ $orders->links() }}</div>
                </div>
            </div>
        </div>
    </div>
@endsection

@push('scripts')
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <script>
        @if (session('success'))
            Swal.fire({ icon: 'success', title: 'Success!', text: "{{ session('success') }}", showConfirmButton: false, timer: 3000, toast: true, position: 'top-end' });
        @endif
        @if (session('error'))
            Swal.fire({ icon: 'error', title: 'Error!', text: "{{ session('error') }}", showConfirmButton: true });
        @endif
    </script>
@endpush
