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

<?php $__env->startSection('header_title', 'Order'); ?>
<?php $__env->startSection('header_subtitle', 'Manage your order'); ?>

<?php $__env->startSection('content'); ?>
    <div class="container-fluid">
        <!-- Header Section -->
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h1 class="h3">Order List</h1>
        </div>

        <!-- Order Table -->
        <div class="card shadow">
            <div class="card-body">
                <div class="table-responsive">
                    
                    <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 ID..." value="<?php echo e(request('search')); ?>">
                                <select name="status" class="form-select form-select-sm" id="">
                                    <option value="">All Status</option>
                                    <?php $__currentLoopData = ['processing', 'shipped']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $status): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($status); ?>"
                                            <?php echo e(request('status') == $status ? 'selected' : ''); ?>>
                                            <?php echo e(ucfirst($status)); ?>

                                        </option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                                <button type="submit" class="btn btn-sm btn-outline-secondary">
                                    <i class="bi bi-search"></i>
                                </button>
                            </div>
                            <?php if(request('search') || request('status')): ?>
                                <a href="<?php echo e(route('admin.orders.index')); ?>" class="btn btn-sm btn-outline-secondary">
                                    <i class="bi bi-x-circle me-1"></i>
                                </a>
                            <?php endif; ?>
                        </form>
                    </div>

                    
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>Order Code</th>
                                <th>Customer</th>
                                <th>Total Amount</th>
                                <th>Status</th>
                                <th>Payment Method</th>
                                <th>Created At</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $__currentLoopData = $orders; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $order): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <tr>
                                    <td><?php echo e($order->order_code); ?></td>
                                    <td><?php echo e($order->user->name ?? 'N/A'); ?></td>
                                    <td>Rp <?php echo e(number_format($order->total_amount, 0, ',', '.')); ?></td>
                                    <td>
                                        <span class="badge bg-<?php echo e($order->status_color); ?>">
                                            <?php echo e(ucfirst($order->status)); ?>

                                        </span>
                                    </td>
                                    <td><?php echo e($order->midtrans_payment_type); ?></td>
                                    <td><?php echo e($order->created_at->format('d M Y')); ?></td>
                                    <td>
                                        <button type="button" class="btn btn-sm btn-info" data-bs-toggle="modal"
                                            data-bs-target="#orderDetailModal<?php echo e($order->id); ?>">
                                            <i class="bi bi-eye"></i>
                                        </button>
                                        <button type="button" class="btn btn-sm btn-primary" data-bs-toggle="modal"
                                            data-bs-target="#UpdateStatusModal<?php echo e($order->id); ?>"
                                            <?php if(empty($order->getNextPossibleStatuses())): ?> disabled <?php endif; ?>>
                                            <i class="bi bi-pencil"></i>
                                        </button>
                                    </td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                            <?php if($orders->isEmpty()): ?>
                                <tr>
                                    <td colspan="7" class="text-center py-4">
                                        <div class="d-flex flex-column align-items-center">
                                            <i class="bi bi-inbox display-4 text-muted mb-2"></i>
                                            <p class="text-muted mb-0">No Order Found</p>
                                        </div>
                                    </td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                    
                    <div class="mt-4">
                        <?php echo e($orders->links()); ?>

                    </div>
                </div>
            </div>
        </div>

        
        <?php $__currentLoopData = $orders; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $order): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            
            <div class="modal fade" id="orderDetailModal<?php echo e($order->id); ?>" tabindex="-1"
                aria-labelledby="orderDetailModalLabel<?php echo e($order->id); ?>">
                <div class="modal-dialog modal-lg">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title fw-bold" id="orderDetailModallabel<?php echo e($order->id); ?>">
                                <i class="bi bi-box-seam me-2"></i>Order #<?php echo e($order->id); ?>

                            </h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <div class="row gap-4">
                                
                                <div class="col-12">
                                    <div class="card border-0 bg-light">
                                        <div class="card-body p-4">
                                            <div class="d-flex justify-content-between align-items-center mb-3">
                                                <div>
                                                    <p class="text-muted mb-1">Order Status</p>
                                                    <h4 class="mb-0">
                                                        <span class="badge bg-<?php echo e($order->status_color); ?>">
                                                            <?php echo e(ucfirst($order->status)); ?>

                                                        </span>
                                                    </h4>
                                                </div>
                                                <div class="text-end">
                                                    <p class="text-muted mb-1">Order Date</p>
                                                    <h6 class="mb-0"><?php echo e($order->created_at->format('d M Y')); ?></h6>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                
                                <div class="col-md-6">
                                    <div class="card h-100 border-0 shadow-sm">
                                        <div class="card-body">
                                            <h6 class="card-title mb-3">
                                                <i class="bi bi-person me-2"></i>Customer Information
                                            </h6>
                                            <div class="mb-2">
                                                <label class="text-muted small">Name</label>
                                                <p class="mb-0"><?php echo e($order->user->name ?? 'N/A'); ?></p>
                                            </div>
                                            <div class="mb-2">
                                                <label class="text-muted small">Email</label>
                                                <p class="mb-0"><?php echo e($order->user->email ?? 'N/A'); ?></p>
                                            </div>
                                            <div class="mb-0">
                                                <label class="text-muted small">Phone</label>
                                                <p class="mb-0"><?php echo e($order->user->phone ?? 'N/A'); ?></p>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                
                                <div class="col-md-6">
                                    <div class="card h-100 border-0 shadow-sm">
                                        <div class="card-body">
                                            <h6 class="card-title mb-3">
                                                <i class="bi bi-truck me-2"></i>Shipping Information
                                            </h6>
                                            <div class="mb-2">
                                                <label class="text-muted small">Address</label>
                                                <p class="mb-1 fw-medium"><?php echo e($order->shipping_address ?? 'N/A'); ?></p>
                                            </div>
                                            <?php if($order->resi_code): ?>
                                                <div class="mb-0">
                                                    <label class="text-muted small">Tracking Number</label>
                                                    <p class="mb-1 fw-medium"><?php echo e($order->resi_code); ?></p>
                                                </div>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </div>

                                
                                <div class="col-12">
                                    <div class="card border-0 shadow-sm">
                                        <div class="card-body">
                                            <h6 class="card-title mb-3">
                                                <i class="bi bi-credit-card me-2"></i>Payment Information
                                            </h6>
                                            <div class="row gap-3">
                                                <?php if($order->midtrans_transaction_id): ?>
                                                    <div class="col-md-4">
                                                        <label class="text-muted small">Transaction ID</label>
                                                        <p class="mb-1 fw-medium">
                                                            <?php echo e($order->midtrans_transaction_id); ?>

                                                        </p>
                                                    </div>
                                                    <div class="col-md-4">
                                                        <label class="text-muted small">Payment Type</label>
                                                        <p class="mb-1 fw-medium">
                                                            <?php echo e(ucwords(str_replace('_', ' ', $order->midtrans_payment_type))); ?>

                                                        </p>
                                                    </div>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                
                                <div class="col-12">
                                    <div class="card border-0 shadow-sm">
                                        <div class="card-body">
                                            <h6 class="card-title mb-3">
                                                <i class="bi bi-box me-2"></i>Order Items
                                            </h6>
                                            <div class="table-responsive">
                                                <table class="table table-borderless align-middle">
                                                    <thead class="bg-light">
                                                        <tr>
                                                            <th>Product</th>
                                                            <th class="text-end">Price</th>
                                                            <th class="text-end">Quantity</th>
                                                            <th class="text-end">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); ?></td>
                                                                <td class="text-end">Rp
                                                                    <?php echo e(number_format($item->product->price, 0, ',', '.')); ?>

                                                                </td>
                                                                <td class="text-end"><?php echo e($item->quantity); ?></td>
                                                                <td class="text-end">Rp
                                                                    <?php echo e(number_format($item->product->price * $item->quantity, 0, ',', '.')); ?>

                                                                </td>
                                                            </tr>
                                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                    </tbody>
                                                    <tfoot class="border-top">
                                                        <tr>
                                                            <td colspan="3" class="text-end fw-medium">Total</td>
                                                            <td class="text-end fw-medium">Rp
                                                                <?php echo e(number_format($order->total_amount, 0, ',', '.')); ?></td>
                                                        </tr>
                                                    </tfoot>
                                                </table>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer border-0 pt-8">
                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                        </div>
                    </div>
                </div>
            </div>


            
            <div class="modal fade" id="UpdateStatusModal<?php echo e($order->id); ?>" tabindex="-1">
                <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 Order Status #<?php echo e($order->id); ?></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 Status</label>
                                    <div>
                                        <span class="badge bg-<?php echo e($order->status_color); ?>"><?php echo e(ucfirst($order->status)); ?>

                                        </span>
                                    </div>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">New Status</label>
                                    <select name="status" class="form-select status-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 if($status == 'cancelled'): ?> class="text-danger" <?php endif; ?>>
                                                <?php echo e(ucfirst($status)); ?>

                                            </option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                    <?php if(empty($order->getNextPossibleStatuses())): ?>
                                        <small class="text-muted">No Status changes available</small>
                                    <?php endif; ?>
                                </div>
                                <div class="mb-3" id="resiField<?php echo e($order->id); ?>" style="display: none">
                                    <label class="form-label">Resi Number</label>
                                    <input type="text" name="resi_code" class="form-control"
                                        placeholder="Enter Resi Number">
                                    <div class="form-text">Required for shipped status</div>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                                <?php if(!empty($order->getNextPossibleStatuses())): ?>
                                    <button type="submit" class="btn btn-primary">Update Status</button>
                                <?php endif; ?>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
    </div>
    <?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; ?>

        // Show error message
        <?php if(session('error')): ?>
            Swal.fire({
                icon: 'error',
                title: 'Error!',
                text: "<?php echo e(session('error')); ?>",
                showConfirmButton: true
            });
        <?php endif; ?>
        document.querySelectorAll('select[name="status"]').forEach(select => {
            select.addEventListener('change', function() {
                const modal = this.closest('.modal');
                const resiField = modal.querySelector('[id^=resiField]');
                const resiInput = resiField.querySelector('input');

                if (this.value === 'shipped') {
                    resiField.style.display = 'block';
                    resiInput.required = true;
                } else {
                    resiField.style.display = 'none';
                    resiInput.required = false;
                }
            });
        });
    </script>
<?php $__env->stopPush(); ?>

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