<?php $__env->startSection('title', 'Orders'); ?>

<?php $__env->startSection('header_title', 'Orders'); ?>
<?php $__env->startSection('header_subtitle', 'Manage all orders'); ?>

<?php $__env->startSection('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="<?php echo e(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="<?php echo e(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" <?php echo e(request('status') == 'unpaid' ? 'selected' : ''); ?>>Unpaid</option>
                            <option value="paid" <?php echo e(request('status') == 'paid' ? 'selected' : ''); ?>>Paid</option>
                            <option value="verify" <?php echo e(request('status') == 'verify' ? 'selected' : ''); ?>>Verify</option>
                            <option value="cancelled" <?php echo e(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>
                            <?php $__empty_1 = true; $__currentLoopData = $orders; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $order): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <tr>
                                    <td><?php echo e($order->order_code); ?></td>
                                    <td><?php echo e($order->user->name); ?></td>
                                    <td>
                                        <?php $__currentLoopData = $order->items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <div class="d-flex align-items-center gap-1 mb-1">
                                                <span><?php echo e($item->product->name ?? 'Deleted'); ?> x <?php echo e($item->quantity); ?></span>
                                                <button type="button" class="btn btn-sm btn-outline-primary ms-1"
                                                    data-bs-toggle="modal"
                                                    data-bs-target="#editItemModal<?php echo e($item->id); ?>"
                                                    title="Edit item">
                                                    <i class="bi bi-pencil"></i>
                                                </button>
                                            </div>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </td>
                                    <td>Rp <?php echo e(number_format($order->total_amount, 0, ',', '.')); ?></td>
                                    <td><?php echo e($order->payment_method ?? '-'); ?></td>
                                    <td><?php echo e($order->shippingCost->destination ?? '-'); ?><br>
                                        <small>Rp <?php echo e(number_format($order->shippingCost->cost ?? 0, 0, ',', '.')); ?></small>
                                    </td>
                                    <td><span class="badge bg-<?php echo e($order->status_color); ?>"><?php echo e($order->status); ?></span></td>
                                    <td><?php echo e($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<?php echo e($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<?php echo e($order->id); ?>" title="Update Status">
                                                <i class="bi bi-arrow-repeat"></i>
                                            </button>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                <tr>
                                    <td colspan="9" class="text-center py-4"><p class="text-muted">No orders found</p></td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                    <div class="mt-4"><?php echo e($orders->links()); ?></div>
                </div>
            </div>
        </div>
    </div>

    <!-- Detail Modals -->
    <?php $__currentLoopData = $orders; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $order): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <div class="modal fade" id="detailModal<?php echo e($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: <?php echo e($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> <?php echo e($order->user->name); ?><br>
                                <strong>Phone:</strong> <?php echo e($order->user->phone); ?><br>
                                <strong>Email:</strong> <?php echo e($order->user->email); ?>

                            </div>
                            <div class="col-md-6">
                                <strong>Status:</strong> <span class="badge bg-<?php echo e($order->status_color); ?>"><?php echo e($order->status); ?></span><br>
                                <strong>Payment:</strong> <?php echo e($order->payment_method ?? '-'); ?><br>
                                <strong>Shipping:</strong> <?php echo e($order->shippingCost->destination ?? '-'); ?> (Rp <?php echo e(number_format($order->shippingCost->cost ?? 0, 0, ',', '.')); ?>)
                            </div>
                        </div>
                        <div class="mb-3">
                            <strong>Shipping Address:</strong><br>
                            <?php echo e($order->shipping_address); ?>

                        </div>
                        <?php if($order->notes): ?>
                            <div class="mb-3">
                                <strong>Notes:</strong><br>
                                <?php echo e($order->notes); ?>

                            </div>
                        <?php endif; ?>
                        <table class="table table-sm">
                            <thead>
                                <tr><th>Product</th><th>Price</th><th>Qty</th><th>Subtotal</th></tr>
                            </thead>
                            <tbody>
                                <?php $__currentLoopData = $order->items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <tr>
                                        <td><?php echo e($item->product->name ?? 'Deleted'); ?></td>
                                        <td>Rp <?php echo e(number_format($item->price, 0, ',', '.')); ?></td>
                                        <td><?php echo e($item->quantity); ?></td>
                                        <td>Rp <?php echo e(number_format($item->price * $item->quantity, 0, ',', '.')); ?></td>
                                    </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </tbody>
                            <tfoot>
                                <tr>
                                    <th colspan="3" class="text-end">Total</th>
                                    <th>Rp <?php echo e(number_format($order->total_amount, 0, ',', '.')); ?></th>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- Status Modal -->
        <div class="modal fade" id="statusModal<?php echo e($order->id); ?>" tabindex="-1" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <form action="<?php echo e(route('admin.orders.updateStatus', $order)); ?>" method="POST">
                        <?php echo csrf_field(); ?>
                        <?php echo method_field('PATCH'); ?>
                        <div class="modal-header">
                            <h5 class="modal-title">Update Status: <?php echo e($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-<?php echo e($order->status_color); ?>"><?php echo e($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>
                                    <?php $__currentLoopData = $order->getNextPossibleStatuses(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $status): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($status); ?>"><?php echo e(ucfirst($status)); ?></option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </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 -->
        <?php $__currentLoopData = $order->items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <div class="modal fade" id="editItemModal<?php echo e($item->id); ?>" tabindex="-1" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <form action="<?php echo e(route('admin.orders.updateItem', $order)); ?>" method="POST">
                            <?php echo csrf_field(); ?>
                            <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><?php echo e($item->product->name ?? 'Deleted'); ?></strong><br>
                                        <small>Price: Rp <?php echo e(number_format($item->price, 0, ',', '.')); ?> | Qty: <?php echo e($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>
                                        <?php $__currentLoopData = $products; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $product): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option value="<?php echo e($product->id); ?>"
                                                <?php echo e($item->product_id == $product->id ? 'selected' : ''); ?>>
                                                <?php echo e($product->name); ?> - Rp <?php echo e(number_format($product->price, 0, ',', '.')); ?> (Stock: <?php echo e($product->stock); ?>)
                                            </option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">Quantity</label>
                                    <input type="number" name="items[0][quantity]" class="form-control"
                                        value="<?php echo e($item->quantity); ?>" min="1" required>
                                </div>
                                <input type="hidden" name="items[0][id]" value="<?php echo e($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>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
<?php $__env->stopSection(); ?>

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

<?php echo $__env->make('layouts.layout-admin', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH C:\xampp\htdocs\e-commerce\resources\views/admin/order.blade.php ENDPATH**/ ?>