

<?php $__env->startSection('title', 'Warehouse - Confirm Picking'); ?>

<?php $__env->startSection('content'); ?>
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-info text-white">
                    <h5 class="mb-0"><i class="fas fa-box"></i> Confirm Warehouse Picking</h5>
                </div>
                <div class="card-body">
                    <!-- Invoice Summary -->
                    <div class="row mb-4">
                        <div class="col-md-6">
                            <div class="card border-primary">
                                <div class="card-header bg-light">
                                    <h6 class="mb-0">Invoice Information</h6>
                                </div>
                                <div class="card-body">
                                    <div class="row">
                                        <div class="col-6">
                                            <p class="mb-1"><strong>Invoice #:</strong></p>
                                            <p class="text-primary fs-5"><?php echo e($invoice->invoice_number); ?></p>
                                        </div>
                                        <div class="col-6">
                                            <p class="mb-1"><strong>Date:</strong></p>
                                            <p><?php echo e($invoice->invoice_date->format('M d, Y')); ?></p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="card border-success">
                                <div class="card-header bg-light">
                                    <h6 class="mb-0">Customer & Supervisor</h6>
                                </div>
                                <div class="card-body">
                                    <div class="row">
                                        <div class="col-6">
                                            <p class="mb-1"><strong>Customer:</strong></p>
                                            <p class="fw-bold"><?php echo e($invoice->customer->name ?? 'N/A'); ?></p>
                                        </div>
                                        <div class="col-6">
                                            <p class="mb-1"><strong>Supervisor:</strong></p>
                                            <p class="fw-bold"><?php echo e($invoice->supervisingEmployee->name ?? 'N/A'); ?></p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Instructions -->
                    <div class="alert alert-warning">
                        <i class="fas fa-exclamation-triangle"></i>
                        <strong>Warehouse Instructions:</strong>
                        Verify that all items are available in stock. If any items are out of stock or partially available,
                        adjust the "Picked Quantity" accordingly. The customer will only be billed for items that are actually delivered.
                    </div>

                    <!-- Picking Form -->
                    <form id="pickingForm" action="<?php echo e(route('warehouse.factory-invoices.process', $invoice)); ?>" method="POST">
                        <?php echo csrf_field(); ?>

                        <div class="table-responsive">
                            <table class="table table-striped table-hover">
                                <thead class="table-light">
                                    <tr>
                                        <th>Item</th>
                                        <th class="text-center">Ordered Qty</th>
                                        <th class="text-center">Available Stock</th>
                                        <th class="text-center">Picked Qty</th>
                                        <th class="text-center">Unit</th>
                                        <th class="text-end">Unit Price</th>
                                        <th class="text-end">Total</th>
                                        <th>Notes</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php $__currentLoopData = $invoice->items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <tr>
                                            <td>
                                                <div class="fw-bold"><?php echo e($item->item->name ?? 'N/A'); ?></div>
                                                <small class="text-muted"><?php echo e($item->item->sku ?? ''); ?></small>
                                            </td>
                                            <td class="text-center">
                                                <span class="badge bg-primary fs-6"><?php echo e($item->ordered_quantity); ?></span>
                                            </td>
                                            <td class="text-center">
                                                <span class="text-success fw-bold">
                                                    <!-- This would be calculated from actual stock -->
                                                    <?php echo e($item->ordered_quantity); ?> <!-- Placeholder - in real app, calculate from stock -->
                                                </span>
                                            </td>
                                            <td class="text-center">
                                                <input type="number"
                                                       name="items[<?php echo e($item->id); ?>][picked_quantity]"
                                                       class="form-control form-control-sm text-center picked-qty"
                                                       value="<?php echo e($item->ordered_quantity); ?>"
                                                       min="0"
                                                       max="<?php echo e($item->ordered_quantity); ?>"
                                                       required>
                                            </td>
                                            <td class="text-center">
                                                <input type="hidden" name="items[<?php echo e($item->id); ?>][id]" value="<?php echo e($item->id); ?>">
                                                <?php echo e($item->item->uom->code ?? 'PCS'); ?>

                                            </td>
                                            <td class="text-end">$<?php echo e(number_format($item->ordered_unit_price, 2)); ?></td>
                                            <td class="text-end picked-total">$0.00</td>
                                            <td>
                                                <input type="text"
                                                       name="items[<?php echo e($item->id); ?>][notes]"
                                                       class="form-control form-control-sm"
                                                       style="width: 200px;"
                                                       placeholder="Optional notes">
                                            </td>
                                        </tr>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </tbody>
                                <tfoot class="table-light">
                                    <tr>
                                        <th colspan="6" class="text-end">Grand Total (Picked):</th>
                                        <th class="text-end" id="grandTotal">$0.00</th>
                                        <th></th>
                                    </tr>
                                </tfoot>
                            </table>
                        </div>

                        <!-- Confirmation Actions -->
                        <div class="row mt-4">
                            <div class="col-12">
                                <div class="card border-warning">
                                    <div class="card-body">
                                        <div class="row">
                                            <div class="col-md-8">
                                                <h6 class="text-warning mb-2"><i class="fas fa-exclamation-triangle"></i> Confirmation</h6>
                                                <p class="mb-0">
                                                    By confirming this picking, you acknowledge that all listed items have been picked from warehouse stock.
                                                    The invoice will move to "Picked" status and be ready for outward gate pass creation.
                                                </p>
                                            </div>
                                            <div class="col-md-4 d-flex align-items-center justify-content-end">
                                                <div class="d-flex gap-2">
                                                    <button type="button"
                                                            class="btn btn-success"
                                                            onclick="confirmPicking()"
                                                            id="confirmBtn">
                                                        <i class="fas fa-check me-1"></i>
                                                        Confirm Picking
                                                    </button>

                                                    <a href="<?php echo e(route('warehouse.factory-invoices.index')); ?>"
                                                    class="btn btn-outline-secondary">
                                                        <i class="fas fa-times me-1"></i>
                                                        Cancel
                                                    </a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Picking Confirmation Modal -->
<div class="modal fade" id="confirmPickingModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            
            <div class="modal-header">
                <h5 class="modal-title">
                    <i class="fas fa-check-circle text-success me-2"></i>
                    Confirm Picking
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>

            <div class="modal-body">
                Are you sure you want to confirm this picking?
                <br>
                
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-secondary"
                        data-bs-dismiss="modal">
                    Cancel
                </button>

                <button type="button" class="btn btn-success"
                        id="modalConfirmBtn">
                    Yes, Confirm
                </button>
            </div>

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

<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
$(document).ready(function() {
    // Calculate initial totals
    updateTotals();

    // Update totals when picked quantities change
    $('.picked-qty').on('input', function() {
        updateTotals();
    });
});

function updateTotals() {
    let grandTotal = 0;

    $('.picked-qty').each(function() {
        const $row = $(this).closest('tr');
        const pickedQty = parseFloat($(this).val()) || 0;

        // Get unit price from the row (you might need to adjust this selector)
        const unitPriceText = $row.find('td:nth-child(6)').text().replace('$', '');
        const unitPrice = parseFloat(unitPriceText) || 0;

        const total = pickedQty * unitPrice;
        $row.find('.picked-total').text('$' + total.toFixed(2));

        grandTotal += total;
    });

    $('#grandTotal').text('$' + grandTotal.toFixed(2));
}

// function confirmPicking() {
//     // Validate that at least one item has been picked
//     let hasPickedItems = false;
//     $('.picked-qty').each(function() {
//         if (parseFloat($(this).val()) > 0) {
//             hasPickedItems = true;
//             return false; // Break out of loop
//         }
//     });

//     if (!hasPickedItems) {
//         alert('Please pick at least one item with quantity greater than 0.');
//         return;
//     }

//     // Show confirmation dialog
//     if (!confirm('Are you sure you want to confirm this picking? This action cannot be undone.')) {
//         return;
//     }

//     // Disable button and show loading
//     $('#confirmBtn').prop('disabled', true).html('<i class="fas fa-spinner fa-spin"></i> Processing...');

//     // Submit form
//     $('#pickingForm').submit();
// }

function confirmPicking() {
    // Validate that at least one item has been picked
    let hasPickedItems = false;

    $('.picked-qty').each(function() {
        if (parseFloat($(this).val()) > 0) {
            hasPickedItems = true;
            return false;
        }
    });

    if (!hasPickedItems) {
        alert('Please pick at least one item with quantity greater than 0.');
        return;
    }

    // Show Bootstrap confirmation modal
    const modal = new bootstrap.Modal(
        document.getElementById('confirmPickingModal')
    );

    modal.show();
}


$('#modalConfirmBtn').on('click', function () {

    $(this)
        .prop('disabled', true)
        .html('<i class="fas fa-spinner fa-spin me-1"></i> Processing...');

    $('#confirmBtn').prop('disabled', true);

    $('#pickingForm').submit();
});

</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\laragon\www\habibcables-ERP\resources\views/warehouse/factory-invoices/confirm-picking.blade.php ENDPATH**/ ?>