@extends('layouts.layout-admin')

@section('title', 'Orders')

@section('header_title', 'Orders')
@section('header_subtitle', 'Manage all orders')

@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.orders.index') }}" method="GET" class="d-flex align-items-center gap-2">
                        <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>
                        <select name="status" class="form-select form-select-sm" style="width:auto;" onchange="this.form.submit()">
                            <option value="">All Status</option>
                            <option value="unpaid" {{ request('status') == 'unpaid' ? 'selected' : '' }}>Unpaid</option>
                            <option value="paid" {{ request('status') == 'paid' ? 'selected' : '' }}>Paid</option>
                            <option value="verify" {{ request('status') == 'verify' ? 'selected' : '' }}>Verify</option>
                            <option value="cancelled" {{ request('status') == 'cancelled' ? 'selected' : '' }}>Cancelled</option>
                        </select>
                    </form>
                </div>

                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>Order Code</th>
                                <th>Customer</th>
                                <th>Items</th>
                                <th>Total</th>
                                <th>Payment</th>
                                <th>Shipping</th>
                                <th>Status</th>
                                <th>Date</th>
                                <th>Actions</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 class="d-flex align-items-center gap-1 mb-1">
                                                <span>{{ $item->product->name ?? 'Deleted' }} x {{ $item->quantity }}</span>
                                                <button type="button" class="btn btn-sm btn-outline-primary ms-1"
                                                    data-bs-toggle="modal"
                                                    data-bs-target="#editItemModal{{ $item->id }}"
                                                    title="Edit item">
                                                    <i class="bi bi-pencil"></i>
                                                </button>
                                            </div>
                                        @endforeach
                                    </td>
                                    <td>Rp {{ number_format($order->total_amount, 0, ',', '.') }}</td>
                                    <td>{{ $order->payment_method ?? '-' }}</td>
                                    <td>{{ $order->shippingCost->destination ?? '-' }}<br>
                                        <small>Rp {{ number_format($order->shippingCost->cost ?? 0, 0, ',', '.') }}</small>
                                    </td>
                                    <td><span class="badge bg-{{ $order->status_color }}">{{ $order->status }}</span></td>
                                    <td>{{ $order->created_at->format('d/m/Y') }}</td>
                                    <td>
                                        <div class="btn-group">
                                            <button type="button" class="btn btn-sm btn-info" data-bs-toggle="modal"
                                                data-bs-target="#detailModal{{ $order->id }}" title="Detail">
                                                <i class="bi bi-eye"></i>
                                            </button>
                                            <button type="button" class="btn btn-sm btn-warning" data-bs-toggle="modal"
                                                data-bs-target="#statusModal{{ $order->id }}" title="Update Status">
                                                <i class="bi bi-arrow-repeat"></i>
                                            </button>
                                        </div>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="9" class="text-center py-4"><p class="text-muted">No orders found</p></td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                    <div class="mt-4">{{ $orders->links() }}</div>
                </div>
            </div>
        </div>
    </div>

    <!-- Detail Modals -->
    @foreach ($orders as $order)
        <div class="modal fade" id="detailModal{{ $order->id }}" tabindex="-1" aria-hidden="true">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Order Detail: {{ $order->order_code }}</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>
                    <div class="modal-body">
                        <div class="row mb-3">
                            <div class="col-md-6">
                                <strong>Customer:</strong> {{ $order->user->name }}<br>
                                <strong>Phone:</strong> {{ $order->user->phone }}<br>
                                <strong>Email:</strong> {{ $order->user->email }}
                            </div>
                            <div class="col-md-6">
                                <strong>Status:</strong> <span class="badge bg-{{ $order->status_color }}">{{ $order->status }}</span><br>
                                <strong>Payment:</strong> {{ $order->payment_method ?? '-' }}<br>
                                <strong>Shipping:</strong> {{ $order->shippingCost->destination ?? '-' }} (Rp {{ number_format($order->shippingCost->cost ?? 0, 0, ',', '.') }})
                            </div>
                        </div>
                        <div class="mb-3">
                            <strong>Shipping Address:</strong><br>
                            {{ $order->shipping_address }}
                        </div>
                        @if ($order->notes)
                            <div class="mb-3">
                                <strong>Notes:</strong><br>
                                {{ $order->notes }}
                            </div>
                        @endif
                        <table class="table table-sm">
                            <thead>
                                <tr><th>Product</th><th>Price</th><th>Qty</th><th>Subtotal</th></tr>
                            </thead>
                            <tbody>
                                @foreach ($order->items as $item)
                                    <tr>
                                        <td>{{ $item->product->name ?? 'Deleted' }}</td>
                                        <td>Rp {{ number_format($item->price, 0, ',', '.') }}</td>
                                        <td>{{ $item->quantity }}</td>
                                        <td>Rp {{ number_format($item->price * $item->quantity, 0, ',', '.') }}</td>
                                    </tr>
                                @endforeach
                            </tbody>
                            <tfoot>
                                <tr>
                                    <th colspan="3" class="text-end">Total</th>
                                    <th>Rp {{ number_format($order->total_amount, 0, ',', '.') }}</th>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- Status Modal -->
        <div class="modal fade" id="statusModal{{ $order->id }}" tabindex="-1" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <form action="{{ route('admin.orders.updateStatus', $order) }}" method="POST">
                        @csrf
                        @method('PATCH')
                        <div class="modal-header">
                            <h5 class="modal-title">Update Status: {{ $order->order_code }}</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                        </div>
                        <div class="modal-body">
                            <p>Current Status: <span class="badge bg-{{ $order->status_color }}">{{ $order->status }}</span></p>
                            <div class="mb-3">
                                <label class="form-label">New Status</label>
                                <select name="status" class="form-select" required>
                                    <option value="">Select Status</option>
                                    @foreach ($order->getNextPossibleStatuses() as $status)
                                        <option value="{{ $status }}">{{ ucfirst($status) }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                            <button type="submit" class="btn btn-primary">Update Status</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- Edit Item Modals -->
        @foreach ($order->items as $item)
            <div class="modal fade" id="editItemModal{{ $item->id }}" tabindex="-1" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <form action="{{ route('admin.orders.updateItem', $order) }}" method="POST">
                            @csrf
                            <div class="modal-header">
                                <h5 class="modal-title">Edit Item</h5>
                                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                            </div>
                            <div class="modal-body">
                                <div class="mb-3">
                                    <label class="form-label">Current Item</label>
                                    <div class="p-3 bg-gray-50 rounded-lg">
                                        <strong>{{ $item->product->name ?? 'Deleted' }}</strong><br>
                                        <small>Price: Rp {{ number_format($item->price, 0, ',', '.') }} | Qty: {{ $item->quantity }}</small>
                                    </div>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">Change Product</label>
                                    <select name="items[0][product_id]" class="form-select" required>
                                        @foreach ($products as $product)
                                            <option value="{{ $product->id }}"
                                                {{ $item->product_id == $product->id ? 'selected' : '' }}>
                                                {{ $product->name }} - Rp {{ number_format($product->price, 0, ',', '.') }} (Stock: {{ $product->stock }})
                                            </option>
                                        @endforeach
                                    </select>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">Quantity</label>
                                    <input type="number" name="items[0][quantity]" class="form-control"
                                        value="{{ $item->quantity }}" min="1" required>
                                </div>
                                <input type="hidden" name="items[0][id]" value="{{ $item->id }}">
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                                <button type="submit" class="btn btn-primary"><i class="bi bi-save"></i> Update Item</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        @endforeach
    @endforeach
@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
