

<?php $__env->startSection('title', 'Create GRN from IGP'); ?>

<?php $__env->startSection('content'); ?>
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h4 class="card-title">Create GRN from IGP</h4>
                </div>
                <div class="card-body">
                    <form action="<?php echo e(route('grns.store-from-igp')); ?>" method="POST" id="grn-form">
                        <?php echo csrf_field(); ?>
                        <input type="hidden" name="inward_gate_pass_id" value="<?php echo e($igpData['igp']->id); ?>">

                        <!-- IGP Information -->
                        <div class="card mb-4">
                            <div class="card-header">
                                <h5 class="card-title mb-0">IGP Information</h5>
                            </div>
                            <div class="card-body">
                                <div class="row g-3">
                                    <div class="col-md-3">
                                        <label class="form-label">IGP Number</label>
                                        <input type="text" class="form-control" value="<?php echo e($igpData['igp']->igp_number); ?>" readonly>
                                    </div>
                                    <div class="col-md-3">
                                        <label class="form-label">Gate Entry Date/Time</label>
                                        <input type="text" class="form-control" value="<?php echo e($igpData['igp']->gate_entry_datetime->format('d/m/Y h:i A')); ?>" readonly>
                                    </div>
                                    <div class="col-md-3">
                                        <label class="form-label">Supplier</label>
                                        <input type="text" class="form-control" value="<?php echo e($igpData['supplier']?->name ?? 'Not Specified'); ?>" readonly>
                                    </div>
                                    <div class="col-md-3">
                                        <label class="form-label">Vehicle No</label>
                                        <input type="text" class="form-control" value="<?php echo e($igpData['igp']->vehicle_no ?? 'Not Specified'); ?>" readonly>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- GRN Information -->
                        <div class="row g-3 mb-4">
                            <div class="col-md-3">
                                <label class="form-label">GRN Number</label>
                                <input type="text" name="grn_number" class="form-control <?php $__errorArgs = ['grn_number'];
$__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('grn_number', $autoGRNNumber)); ?>" readonly>
                                <?php $__errorArgs = ['grn_number'];
$__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-3">
                                <label class="form-label">Purchase Order *</label>
                                <select name="purchase_order_id" id="purchase_order_id" class="form-select <?php $__errorArgs = ['purchase_order_id'];
$__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); ?>" required>
                                    <option value="">Select Purchase Order</option>
                                    <?php $__currentLoopData = $purchaseOrders; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $id => $poNumber): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($id); ?>" <?php echo e(old('purchase_order_id') == $id ? 'selected' : ''); ?>>
                                            <?php echo e($poNumber); ?>

                                        </option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                                <?php $__errorArgs = ['purchase_order_id'];
$__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-3">
                                <label class="form-label">Warehouse</label>
                                <select name="warehouse_id" class="form-select <?php $__errorArgs = ['warehouse_id'];
$__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); ?>">
                                    <option value="">Select Warehouse</option>
                                    <?php $__currentLoopData = $warehouses; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $id => $name): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($id); ?>" <?php echo e(old('warehouse_id') == $id ? 'selected' : ''); ?>>
                                            <?php echo e($name); ?>

                                        </option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                                <?php $__errorArgs = ['warehouse_id'];
$__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-3">
                                <label class="form-label">Received Date</label>
                                <input type="date" name="received_date" class="form-control <?php $__errorArgs = ['received_date'];
$__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('received_date', now()->format('Y-m-d'))); ?>" required>
                                <?php $__errorArgs = ['received_date'];
$__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>

                        <!-- Items Section -->
                        <div class="card mb-4">
                            <div class="card-header">
                                <h5 class="card-title mb-0">Items</h5>
                            </div>
                            <div class="card-body">
                                <div class="table-responsive">
                                    <table class="table table-bordered" id="items-table">
                                        <thead>
                                            <tr>
                                                <th width="20%">Item Name</th>
                                                <th width="15%">Description</th>
                                                <th width="10%">IGP Qty</th>
                                                <th width="10%">UOM</th>
                                                <th width="15%">Accepted Qty</th>
                                                <th width="10%">Lot No</th>
                                                <th width="10%">Expiry Date</th>
                                                <th width="10%">Remarks</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php $__currentLoopData = $igpData['items']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <tr class="item-row">
                                                <td>
                                                    
                                                    <input type="hidden" name="items[<?php echo e($index); ?>][item_id]" value="<?php echo e($item['item_id']); ?>">
                                                    <input type="hidden" name="items[<?php echo e($index); ?>][uom_id]" value="<?php echo e($item['uom_id']); ?>">
                                                    <?php echo e($item['item_name']); ?>

                                                </td>
                                                <td><?php echo e($item['description'] ?? '-'); ?></td>
                                                <td class="text-center"><?php echo e(number_format($item['igp_qty'], 2)); ?></td>
                                                <td class="text-center"><?php echo e($item['uom_name']); ?></td>
                                                <td>
                                                    <input type="number" name="items[<?php echo e($index); ?>][accepted_quantity]"
                                                           class="form-control quantity-input" step="0.01" min="0.01"
                                                           max="<?php echo e($item['max_qty']); ?>" value="<?php echo e(old("items.{$index}.accepted_quantity", $item['max_qty'])); ?>"
                                                           required placeholder="0.00">
                                                    <div class="quantity-error invalid-feedback"></div>
                                                </td>
                                                <td>
                                                    <input type="text" name="items[<?php echo e($index); ?>][lot_no]"
                                                           class="form-control" value="<?php echo e(old("items.{$index}.lot_no")); ?>" placeholder="Lot No">
                                                </td>
                                                <td>
                                                    <input type="date" name="items[<?php echo e($index); ?>][expiry_date]"
                                                           class="form-control" value="<?php echo e(old("items.{$index}.expiry_date")); ?>">
                                                </td>
                                                <td>
                                                    <input type="text" name="items[<?php echo e($index); ?>][notes]"
                                                           class="form-control" value="<?php echo e(old("items.{$index}.notes")); ?>" placeholder="Remarks">
                                                </td>
                                            </tr>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>

                        <!-- General Remarks -->
                        <div class="row g-3 mb-4">
                            <div class="col-12">
                                <label class="form-label">General Remarks</label>
                                <textarea name="notes" class="form-control <?php $__errorArgs = ['notes'];
$__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); ?>"
                                          rows="3" placeholder="Enter any general remarks"><?php echo e(old('notes')); ?></textarea>
                                <?php $__errorArgs = ['notes'];
$__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>

                        <!-- Submit Buttons -->
                        <div class="row">
                            <div class="col-12">
                                <div class="d-flex gap-2">
                                    <button type="submit" class="btn btn-primary">
                                        <i class="fas fa-save me-1"></i>Create GRN
                                    </button>
                                    <a href="<?php echo e(route('inward-gate-passes.show', $igpData['igp'])); ?>" class="btn btn-secondary">
                                        <i class="fas fa-times me-1"></i>Cancel
                                    </a>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('plugin-styles'); ?>
<link rel="stylesheet" href="<?php echo e(asset('theme/vendors/select2/select2.min.css')); ?>">

<style>
    .quantity-input:disabled {
        background-color: #f8f9fa !important;
        color: #6c757d !important;
        cursor: not-allowed !important;
    }
</style>
<?php $__env->stopPush(); ?>

<?php $__env->startPush('plugin-scripts'); ?>
<script src="<?php echo e(asset('theme/vendors/jquery/jquery.min.js')); ?>"></script>
<script src="<?php echo e(asset('theme/vendors/select2/select2.min.js')); ?>"></script>
<script src="<?php echo e(asset('theme/vendors/sweetalert2/sweetalert2.min.js')); ?>"></script>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const form = document.getElementById('grn-form');

    // Initialize Select2 for purchase order
    $('#purchase_order_id').select2({
        theme: 'bootstrap-5',
        placeholder: 'Select Purchase Order',
        allowClear: true
    });

    // Quantity validation
    function validateQuantityInput(input) {
        const value = parseFloat(input.value);
        const min = parseFloat(input.min);
        const max = parseFloat(input.max);
        const row = input.closest('tr');
        const errorDiv = row.querySelector('.quantity-error');

        // Clear previous errors
        input.classList.remove('is-invalid');
        errorDiv.style.display = 'none';
        errorDiv.textContent = '';

        // Check if value is a valid number
        if (isNaN(value)) {
            showQuantityError(input, errorDiv, 'Please enter a valid number');
            return false;
        }

        // Check for negative values
        if (value < 0) {
            showQuantityError(input, errorDiv, 'Quantity cannot be negative');
            return false;
        }

        // Check minimum value
        if (min && value < min) {
            showQuantityError(input, errorDiv, `Quantity must be at least ${min}`);
            return false;
        }

        // Check maximum value
        if (max && value > max) {
            showQuantityError(input, errorDiv, `Quantity cannot exceed ${max}`);
            return false;
        }

        // Check for zero or empty
        if (value === 0 || input.value.trim() === '') {
            showQuantityError(input, errorDiv, 'Quantity is required and must be greater than 0');
            return false;
        }

        return true;
    }

    function showQuantityError(input, errorDiv, message) {
        input.classList.add('is-invalid');
        errorDiv.textContent = message;
        errorDiv.style.display = 'block';
    }

    // Apply validation to all quantity inputs
    function setupQuantityValidation() {
        document.querySelectorAll('.quantity-input').forEach(input => {
            input.addEventListener('input', function() {
                validateQuantityInput(this);
            });

            input.addEventListener('blur', function() {
                validateQuantityInput(this);
            });
        });
    }



        // Initialize quantity input states - only on form load
    function initializeQuantityInputStates() {
        document.querySelectorAll('.quantity-input').forEach(input => {
            // Check if the current value already equals or exceeds the max
            const currentValue = parseFloat(input.value) || 0;
            const maxValue = parseFloat(input.max) || 0;

            // Only disable if the field is already at or above max on form load
            if (currentValue >= maxValue) {
                input.disabled = true;
                input.classList.add('bg-light');
                input.title = 'Quantity disabled: Received quantity equals or exceeds PO quantity';

                // Add visual indicator
                let indicator = input.parentNode.querySelector('.quantity-disabled-indicator');
                if (!indicator) {
                    indicator = document.createElement('small');
                    indicator.className = 'text-muted quantity-disabled-indicator';
                    indicator.textContent = '✓ Max quantity reached';
                    input.parentNode.appendChild(indicator);
                }
            }
        });
    }

    // Form submission validation
    form.addEventListener('submit', function(e) {
        let hasErrors = false;

        // Validate purchase order selection
        const poSelect = document.getElementById('purchase_order_id');
        if (!poSelect.value) {
            poSelect.classList.add('is-invalid');
            hasErrors = true;
        } else {
            poSelect.classList.remove('is-invalid');
        }

        // Validate all quantity inputs
        document.querySelectorAll('.quantity-input').forEach(input => {
            if (!validateQuantityInput(input)) {
                hasErrors = true;
            }
        });

        if (hasErrors) {
            e.preventDefault();
            Swal.fire({
                icon: 'error',
                title: 'Validation Error',
                text: 'Please fix the errors before submitting.'
            });
        }
    });

    // Initialize validation
    setupQuantityValidation();

    // Initialize quantity input states
    initializeQuantityInputStates();
});
</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\grns\create_from_igp.blade.php ENDPATH**/ ?>