

<?php $__env->startSection('title', 'Edit Outward Gate Pass - ' . $outwardGatePass->gate_pass_number); ?>

<?php $__env->startPush('plugin-styles'); ?>
<link rel="stylesheet" href="<?php echo e(asset('theme/vendors/select2/select2.min.css')); ?>">
<link rel="stylesheet" href="<?php echo e(asset('theme/vendors/select2-bootstrap-5-theme/select2-bootstrap-5-theme.min.css')); ?>">
<style>
    .required:after {
        content: ' *';
        color: red;
    }
    .invoice-card {
        border: 1px solid #dee2e6;
        border-radius: 0.375rem;
        margin-bottom: 1rem;
    }
    .invoice-header {
        background-color: #f8f9fa;
        padding: 0.75rem 1rem;
        border-bottom: 1px solid #dee2e6;
        border-radius: 0.375rem 0.375rem 0 0;
    }
    .invoice-body {
        padding: 1rem;
    }
    .select2-container--bootstrap-5 .select2-selection {
        border: 1px solid #dee2e6;
        border-radius: 0.375rem;
        min-height: 38px;
    }
    .select2-container--bootstrap-5 .select2-selection__rendered {
        padding: 0.375rem 0.75rem;
        font-size: 0.875rem;
        line-height: 1.5;
    }
    .select2-container--bootstrap-5 .select2-selection__choice {
        background-color: #e9ecef;
        border: 1px solid #adb5bd;
        border-radius: 0.25rem;
        padding: 0.125rem 0.5rem;
        margin: 0.125rem 0.25rem 0.125rem 0;
        font-size: 0.75rem;
    }
    .select2-container--bootstrap-5 .select2-selection__choice__remove {
        color: #6c757d;
        margin-right: 0.25rem;
    }
</style>
<?php $__env->stopPush(); ?>

<?php $__env->startSection('content'); ?>
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">Edit Outward Gate Pass - <?php echo e($outwardGatePass->gate_pass_number); ?></h5>
                </div>
                <div class="card-body">
                    <?php if($errors->any()): ?>
                        <div class="alert alert-danger">
                            <ul class="mb-0">
                                <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <li><?php echo e($error); ?></li>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </ul>
                        </div>
                    <?php endif; ?>

                    <form id="outwardGatePassForm" action="<?php echo e(route('outward-gate-passes.update', $outwardGatePass)); ?>" method="POST">
                        <?php echo csrf_field(); ?>
                        <?php echo method_field('PATCH'); ?>

                        <!-- Factory Invoice Selection -->
                        <div class="row g-3 mb-4">
                            <div class="col-md-12">
                                <label for="factory_invoice_ids" class="form-label required">Factory Invoices</label>
                                <select id="factory_invoice_ids" name="factory_invoice_ids[]" class="form-select <?php $__errorArgs = ['factory_invoice_ids'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" multiple required>
                                    <?php $__currentLoopData = $factoryInvoices; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $invoice): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($invoice->id); ?>"
                                                data-invoice-number="<?php echo e($invoice->invoice_number); ?>"
                                                data-customer-name="<?php echo e($invoice->customer->name ?? 'N/A'); ?>"
                                                data-items="<?php echo e($invoice->items->toJson()); ?>"
                                                <?php echo e(in_array($invoice->id, $outwardGatePass->items->pluck('factory_invoice_id')->unique()->toArray()) ? 'selected' : ''); ?>>
                                            Invoice #<?php echo e($invoice->invoice_number); ?> | <?php echo e($invoice->customer->name ?? 'N/A'); ?> | <?php echo e($invoice->items->count()); ?> items | <?php echo e($invoice->invoice_date->format('M d, Y')); ?>

                                        </option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                                <small class="form-text text-muted">You can select multiple invoices for this gatepass</small>
                                <?php $__errorArgs = ['factory_invoice_ids'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                    <div class="invalid-feedback"><?php echo e($message); ?></div>
                                <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                            </div>
                        </div>

                        <!-- Selected Invoices Display -->
                        <div id="selectedInvoicesContainer" class="mb-4">
                            <?php
                                $invoicesById = $outwardGatePass->items->groupBy('factory_invoice_id');
                            ?>
                            <?php $__currentLoopData = $invoicesById; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $invoiceId => $invoiceItems): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php
                                    $invoice = $invoiceItems->first()->factoryInvoice;
                                ?>
                                <div class="invoice-card" data-invoice-id="<?php echo e($invoiceId); ?>">
                                    <div class="invoice-header">
                                        <div class="d-flex justify-content-between align-items-center">
                                            <h6 class="mb-0">Invoice #<?php echo e($invoice->invoice_number); ?> - <?php echo e($invoice->customer->name ?? 'N/A'); ?></h6>
                                            <button type="button" class="btn btn-sm btn-outline-danger remove-invoice" data-invoice-id="<?php echo e($invoiceId); ?>">
                                                <i class="fas fa-times"></i> Remove
                                            </button>
                                        </div>
                                    </div>
                                    <div class="invoice-body">
                                        <div class="table-responsive">
                                            <table class="table table-sm">
                                                <thead>
                                                    <tr>
                                                        <th>Item</th>
                                                        <th>Ordered Qty</th>
                                                        <th>Picked Qty</th>
                                                        <th>Quantity to Dispatch</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php $__currentLoopData = $invoice->items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                    <tr>
                                                        <td><?php echo e($item->item->name ?? 'Unknown Item'); ?></td>
                                                        <td><?php echo e($item->ordered_quantity); ?></td>
                                                        <td><?php echo e($item->picked_quantity ?? $item->ordered_quantity); ?></td>
                                                        <td>
                                                            <input type="hidden" name="invoices[<?php echo e($loop->parent->index); ?>][id]" value="<?php echo e($invoiceId); ?>">
                                                            <input type="hidden" name="invoices[<?php echo e($loop->parent->index); ?>][items][<?php echo e($index); ?>][item_id]" value="<?php echo e($item->id); ?>">
                                                            <input type="number" class="form-control form-control-sm"
                                                                   name="invoices[<?php echo e($loop->parent->index); ?>][items][<?php echo e($index); ?>][quantity]"
                                                                   min="0.01" max="<?php echo e($item->picked_quantity ?? $item->ordered_quantity); ?>"
                                                                   step="0.01" required
                                                                   value="<?php echo e($invoiceItems->where('factory_invoice_item_id', $item->id)->first()->quantity ?? ''); ?>">
                                                        </td>
                                                    </tr>
                                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </div>

                        <!-- Vehicle and Driver Information -->
                        <div class="row g-3 mb-4">
                            <div class="col-md-4">
                                <label for="vehicle_no" class="form-label required">Vehicle Number</label>
                                <input type="text" id="vehicle_no" name="vehicle_no" class="form-control <?php $__errorArgs = ['vehicle_no'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" value="<?php echo e(old('vehicle_no', $outwardGatePass->vehicle_no)); ?>" required>
                                <?php $__errorArgs = ['vehicle_no'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                    <div class="invalid-feedback"><?php echo e($message); ?></div>
                                <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                            </div>
                            <div class="col-md-4">
                                <label for="driver_name" class="form-label required">Driver Name</label>
                                <input type="text" id="driver_name" name="driver_name" class="form-control <?php $__errorArgs = ['driver_name'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" value="<?php echo e(old('driver_name', $outwardGatePass->driver_name)); ?>" required>
                                <?php $__errorArgs = ['driver_name'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                    <div class="invalid-feedback"><?php echo e($message); ?></div>
                                <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                            </div>
                            <div class="col-md-4">
                                <label for="other_persons" class="form-label">Other Persons</label>
                                <input type="text" id="other_persons" name="other_persons" class="form-control" value="<?php echo e(old('other_persons', $outwardGatePass->other_persons)); ?>" placeholder="Other persons accompanying">
                            </div>
                        </div>

                        <div class="row g-3 mb-4">
                            <div class="col-12">
                                <label for="remarks" class="form-label">Remarks</label>
                                <textarea id="remarks" name="remarks" class="form-control" rows="2" placeholder="Any additional remarks"><?php echo e(old('remarks', $outwardGatePass->remarks)); ?></textarea>
                            </div>
                        </div>

                        <div class="d-flex gap-2 mt-4">
                            <button type="submit" class="btn btn-primary" id="submitBtn">
                                <i class="fas fa-save me-2"></i>Update Gate Pass
                            </button>
                            <a href="<?php echo e(route('outward-gate-passes.show', $outwardGatePass)); ?>" class="btn btn-secondary">
                                <i class="fas fa-times me-2"></i>Cancel
                            </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('plugin-scripts'); ?>
<script src="<?php echo e(asset('theme/vendors/select2/select2.min.js')); ?>"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
    // Initialize select2 for factory invoices (multiple selection)
    $('#factory_invoice_ids').select2({
        theme: 'bootstrap-5',
        placeholder: 'Select Factory Invoices',
        allowClear: true,
        multiple: true
    });

    // Handle invoice selection
    $('#factory_invoice_ids').on('change', function() {
        const selectedOptions = $(this).find('option:selected');
        const container = $('#selectedInvoicesContainer');

        // Remove existing invoices that are no longer selected
        $('.invoice-card').each(function() {
            const invoiceId = $(this).data('invoice-id');
            const isSelected = selectedOptions.toArray().some(option => $(option).val() == invoiceId);
            if (!isSelected) {
                $(this).remove();
            }
        });

        // Add new selected invoices
        selectedOptions.each(function(index) {
            const invoiceId = $(this).val();
            const invoiceNumber = $(this).data('invoice-number');
            const customerName = $(this).data('customer-name');
            const items = $(this).data('items');

            // Check if already displayed
            if (!$(`.invoice-card[data-invoice-id="${invoiceId}"]`).length) {
                addInvoiceToContainer(invoiceId, invoiceNumber, customerName, items, $('.invoice-card').length);
            }
        });
    });

    function addInvoiceToContainer(invoiceId, invoiceNumber, customerName, items, invoiceIndex) {
        const container = $('#selectedInvoicesContainer');

        const invoiceCard = `
            <div class="invoice-card" data-invoice-id="${invoiceId}">
                <div class="invoice-header">
                    <div class="d-flex justify-content-between align-items-center">
                        <h6 class="mb-0">Invoice #${invoiceNumber} - ${customerName}</h6>
                        <button type="button" class="btn btn-sm btn-outline-danger remove-invoice" data-invoice-id="${invoiceId}">
                            <i class="fas fa-times"></i> Remove
                        </button>
                    </div>
                </div>
                <div class="invoice-body">
                    <div class="table-responsive">
                        <table class="table table-sm">
                            <thead>
                                <tr>
                                    <th>Item</th>
                                    <th>Ordered Qty</th>
                                    <th>Picked Qty</th>
                                    <th>Quantity to Dispatch</th>
                                </tr>
                            </thead>
                            <tbody>
                                ${items.map((item, itemIndex) => `
                                    <tr>
                                        <td>${item.item?.name || 'Unknown Item'}</td>
                                        <td>${item.ordered_quantity}</td>
                                        <td>${item.picked_quantity || item.ordered_quantity}</td>
                                        <td>
                                            <input type="hidden" name="invoices[${invoiceIndex}][id]" value="${invoiceId}">
                                            <input type="hidden" name="invoices[${invoiceIndex}][items][${itemIndex}][item_id]" value="${item.id}">
                                            <input type="number" class="form-control form-control-sm"
                                                   name="invoices[${invoiceIndex}][items][${itemIndex}][quantity]"
                                                   min="0.01" max="${item.picked_quantity || item.ordered_quantity}"
                                                   step="0.01" required>
                                        </td>
                                    </tr>
                                `).join('')}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        `;

        container.append(invoiceCard);

        // Handle remove invoice
        $('.remove-invoice[data-invoice-id="' + invoiceId + '"]').on('click', function() {
            $(this).closest('.invoice-card').remove();
            // Remove from select2
            const option = $('#factory_invoice_ids option[value="' + invoiceId + '"]');
            option.prop('selected', false);
            $('#factory_invoice_ids').trigger('change');
        });
    }

    // Form validation
    $('#outwardGatePassForm').on('submit', function(e) {
        const selectedInvoices = $('#factory_invoice_ids').val();
        if (!selectedInvoices || selectedInvoices.length === 0) {
            e.preventDefault();
            alert('Please select at least one factory invoice');
            return false;
        }

        // Check if at least one item has quantity
        const quantityInputs = $('input[name*="[quantity]"]');
        let hasQuantity = false;
        quantityInputs.each(function() {
            if (parseFloat($(this).val()) > 0) {
                hasQuantity = true;
                return false;
            }
        });

        if (!hasQuantity) {
            e.preventDefault();
            alert('Please enter quantity for at least one item');
            return false;
        }
    });
});
</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\outward-gate-passes\edit.blade.php ENDPATH**/ ?>