

<?php $__env->startSection('title', 'Edit Stock Transfer'); ?>

<?php $__env->startPush('plugin-style'); ?>
<style>
    .item-row {
        background-color: #f8f9fa;
        border-radius: 8px;
        padding: 15px;
        margin-bottom: 15px;
    }
    .remove-item {
        color: #dc3545;
        cursor: pointer;
    }
    .remove-item:hover {
        color: #c82333;
    }
</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">
                    <h4 class="card-title">Edit Stock Transfer</h4>
                    <p class="card-subtitle">Transfer #<?php echo e($stockTransfer->id); ?></p>
                </div>
                <div class="card-body">
                    <form action="<?php echo e(route('stock-transfers.update', $stockTransfer->id)); ?>" method="POST" id="stock-transfer-form">
                        <?php echo csrf_field(); ?>
                        <?php echo method_field('PUT'); ?>

                        <div class="row g-3">
                            <!-- Transfer Details -->
                            <div class="col-12">
                                <h6 class="card-title">Transfer Details</h6>
                            </div>

                            <div class="col-md-6">
                                <label class="form-label">From Bin <span class="text-danger">*</span></label>
                                <select name="from_bin_id" class="form-select <?php $__errorArgs = ['from_bin_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 Source Bin</option>
                                    <?php $__currentLoopData = $bins; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $bin): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($bin->id); ?>" <?php echo e(old('from_bin_id', $stockTransfer->from_bin_id) == $bin->id ? 'selected' : ''); ?>>
                                            <?php echo e($bin->code); ?> - <?php echo e($bin->warehouse->name); ?>

                                        </option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                                <?php $__errorArgs = ['from_bin_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-6">
                                <label class="form-label">To Bin <span class="text-danger">*</span></label>
                                <select name="to_bin_id" class="form-select <?php $__errorArgs = ['to_bin_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 Destination Bin</option>
                                    <?php $__currentLoopData = $bins; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $bin): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($bin->id); ?>" <?php echo e(old('to_bin_id', $stockTransfer->to_bin_id) == $bin->id ? 'selected' : ''); ?>>
                                            <?php echo e($bin->code); ?> - <?php echo e($bin->warehouse->name); ?>

                                        </option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                                <?php $__errorArgs = ['to_bin_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-6">
                                <label class="form-label">Transfer Date <span class="text-danger">*</span></label>
                                <input type="date" name="transfer_date" class="form-control <?php $__errorArgs = ['transfer_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('transfer_date', $stockTransfer->transfer_date->format('Y-m-d'))); ?>" required>
                                <?php $__errorArgs = ['transfer_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 class="col-md-6">
                                <label class="form-label">Status <span class="text-danger">*</span></label>
                                <select name="status" class="form-select <?php $__errorArgs = ['status'];
$__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="draft" <?php echo e(old('status', $stockTransfer->status) == 'draft' ? 'selected' : ''); ?>>Draft</option>
                                    <option value="pending" <?php echo e(old('status', $stockTransfer->status) == 'pending' ? 'selected' : ''); ?>>Pending</option>
                                    <option value="approved" <?php echo e(old('status', $stockTransfer->status) == 'approved' ? 'selected' : ''); ?>>Approved</option>
                                    <option value="completed" <?php echo e(old('status', $stockTransfer->status) == 'completed' ? 'selected' : ''); ?>>Completed</option>
                                    <option value="cancelled" <?php echo e(old('status', $stockTransfer->status) == 'cancelled' ? 'selected' : ''); ?>>Cancelled</option>
                                </select>
                                <?php $__errorArgs = ['status'];
$__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-12">
                                <label class="form-label">Notes</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" maxlength="500"><?php echo e(old('notes', $stockTransfer->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>

                            <!-- Items Section -->
                            <div class="col-12">
                                <h6 class="card-title">Transfer Items</h6>
                                <div id="items-container">
                                    <?php $__currentLoopData = $stockTransfer->items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <div class="item-row" id="item-row-<?php echo e($index + 1); ?>">
                                        <div class="row g-2">
                                            <div class="col-md-4">
                                                <label class="form-label">Item <span class="text-danger">*</span></label>
                                                <?php echo $__env->make('common.item-select-simple', [
                                                    'name' => "items[{$index}][item_id]",
                                                    'id' => "items_{$index}_item_id",
                                                    'class' => 'form-select item-select',
                                                    'selectedItem' => $item->item_id,
                                                    'required' => true
                                                ], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                                            </div>
                                            <div class="col-md-2">
                                                <label class="form-label">Quantity <span class="text-danger">*</span></label>
                                                <input type="number" name="items[<?php echo e($index + 1); ?>][qty_transferred]" class="form-control"
                                                       step="0.01" min="0.01" value="<?php echo e($item->qty_transferred); ?>" required>
                                            </div>
                                            <div class="col-md-3">
                                                <label class="form-label">Lot Number</label>
                                                <input type="text" name="items[<?php echo e($index + 1); ?>][lot_no]" class="form-control"
                                                       maxlength="100" value="<?php echo e($item->lot_no); ?>">
                                            </div>
                                            <div class="col-md-2">
                                                <label class="form-label">&nbsp;</label>
                                                <div>
                                                    <button type="button" class="btn btn-outline-danger btn-sm remove-item"
                                                            onclick="removeItem(<?php echo e($index + 1); ?>)">
                                                        <i class="fas fa-trash"></i> Remove
                                                    </button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </div>
                                <button type="button" class="btn btn-outline-primary btn-sm" id="add-item">
                                    <i class="fas fa-plus"></i> Add Item
                                </button>
                            </div>

                            <!-- Submit Buttons -->
                            <div class="col-12">
                                <hr>
                                <div class="d-flex justify-content-end gap-2">
                                    <a href="<?php echo e(route('stock-transfers.show', $stockTransfer->id)); ?>" class="btn btn-secondary">
                                        <i class="fas fa-times"></i> Cancel
                                    </a>
                                    <button type="submit" class="btn btn-primary">
                                        <i class="fas fa-save"></i> Update Transfer
                                    </button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('plugin-scripts'); ?>
<?php echo $__env->make('common.item-select-script', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
<script>
$(document).ready(function() {
    let itemCount = <?php echo e(count($stockTransfer->items)); ?>;

    // Add item row
    $('#add-item').click(function() {
        addItemRow();
    });

    function addItemRow() {
        itemCount++;
        const itemRow = `
            <div class="item-row" id="item-row-${itemCount}">
                <div class="row g-2">
                    <div class="col-md-4">
                        <label class="form-label">Item <span class="text-danger">*</span></label>
                        <select name="items[${itemCount}][item_id]" class="form-select item-select" required>
                            <option value="">Select Item</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label">Quantity <span class="text-danger">*</span></label>
                        <input type="number" name="items[${itemCount}][qty_transferred]" class="form-control"
                               step="0.01" min="0.01" required>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Lot Number</label>
                        <input type="text" name="items[${itemCount}][lot_no]" class="form-control" maxlength="100">
                    </div>
                    <div class="col-md-2">
                        <label class="form-label">&nbsp;</label>
                        <div>
                            <button type="button" class="btn btn-outline-danger btn-sm remove-item"
                                    onclick="removeItem(${itemCount})">
                                <i class="fas fa-trash"></i> Remove
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        `;
        $('#items-container').append(itemRow);
    }

    // Remove item row
    window.removeItem = function(itemCount) {
        $(`#item-row-${itemCount}`).remove();
    };

    // Form validation
    $('#stock-transfer-form').submit(function(e) {
        const fromBin = $('select[name="from_bin_id"]').val();
        const toBin = $('select[name="to_bin_id"]').val();

        if (fromBin === toBin) {
            e.preventDefault();
            alert('Source and destination bins cannot be the same.');
            return false;
        }

        const itemRows = $('.item-row');
        if (itemRows.length === 0) {
            e.preventDefault();
            alert('Please add at least one item to transfer.');
            return false;
        }

        // Check if all required fields are filled
        let isValid = true;
        itemRows.each(function() {
            const itemId = $(this).find('select[name*="[item_id]"]').val();
            const qty = $(this).find('input[name*="[qty_transferred]"]').val();

            if (!itemId || !qty) {
                isValid = false;
            }
        });

        if (!isValid) {
            e.preventDefault();
            alert('Please fill in all required fields for all items.');
            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\stock_transfers\edit.blade.php ENDPATH**/ ?>