<?php if (isset($component)) { $__componentOriginal9ac128a9029c0e4701924bd2d73d7f54 = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54 = $attributes; } ?>
<?php $component = App\View\Components\AppLayout::resolve([] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('app-layout'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\App\View\Components\AppLayout::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes([]); ?>
     <?php $__env->slot('header', null, []); ?> <?php echo e(__('Add Purchase')); ?> <?php $__env->endSlot(); ?>

    
    <?php
        $itemsJson = $items->map(fn($i) => ['id'=>$i->id,'name'=>$i->name,'category'=>$i->category,'unit'=>$i->unit])->toJson();
    ?>

    <div class="card shadow-sm">
        <div class="card-body">
            <form action="<?php echo e(route('purchases.store')); ?>" method="POST">
                <?php echo csrf_field(); ?>
                <div class="row g-3 mb-2">
                    <div class="col-md-4">
                        <label class="form-label fw-semibold"><?php echo e(__('Purchase Invoice No')); ?></label>
                        <input type="text" name="invoice_no" class="form-control" value="<?php echo e(old('invoice_no', $nextInvoice)); ?>">
                    </div>
                </div>

                <div class="row g-3">
                    
                    <div class="col-md-4">
                        <label class="form-label" for="purchase_date"><?php echo e(__('Date')); ?> <span class="text-danger">*</span></label>
                        <input id="purchase_date" type="date" name="purchase_date" class="form-control"
                               value="<?php echo e(old('purchase_date', now()->format('Y-m-d'))); ?>" required>
                    </div>

                    
                    <div class="col-md-4">
                        <label class="form-label" for="item_category"><?php echo e(__('Item Category')); ?></label>
                        <select id="item_category" name="item_category" class="form-select">
                            <option value="">-- <?php echo e(__('Select Category')); ?> --</option>
                            <?php $__currentLoopData = $categories; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $cat): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($cat->name); ?>" <?php echo e(old('item_category') == $cat->name ? 'selected' : ''); ?>>
                                    <?php echo e($cat->name); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>

                    
                    <div class="col-md-4">
                        <label class="form-label" for="item_id"><?php echo e(__('Item')); ?></label>
                        <select id="item_id" name="item_id" class="form-select">
                            <option value="">-- <?php echo e(__('Select Item')); ?> --</option>
                            <?php $__currentLoopData = $items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($item->id); ?>"
                                    data-category="<?php echo e($item->category); ?>"
                                    <?php echo e(old('item_id') == $item->id ? 'selected' : ''); ?>>
                                    <?php echo e($item->name); ?> <?php if($item->unit): ?>(<?php echo e($item->unit); ?>)<?php endif; ?>
                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                </div>

                <div class="row g-3 mt-1">
                    
                    <div class="col-md-4">
                        <label class="form-label" for="vendor_id"><?php echo e(__('Vendor')); ?></label>
                        <select id="vendor_id" name="vendor_id" class="form-select">
                            <option value="">-- <?php echo e(__('Select Vendor')); ?> --</option>
                            <?php $__currentLoopData = $vendors; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $vendor): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($vendor->id); ?>" <?php echo e(old('vendor_id') == $vendor->id ? 'selected' : ''); ?>>
                                    <?php echo e($vendor->name); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                        <div class="form-text">
                            <a href="<?php echo e(route('vendors.create')); ?>" target="_blank">
                                <i class="bi bi-plus-circle me-1"></i><?php echo e(__('Add new vendor')); ?>

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

                    
                    <div class="col-md-4">
                        <label class="form-label" for="quantity"><?php echo e(__('Quantity')); ?></label>
                        <input id="quantity" type="number" step="0.01" min="0" name="quantity"
                               class="form-control" value="<?php echo e(old('quantity')); ?>">
                    </div>

                    
                    <div class="col-md-4">
                        <label class="form-label" for="cost"><?php echo e(__('Total Cost')); ?> <span class="text-danger">*</span></label>
                        <input id="cost" type="number" step="0.01" min="0" name="cost"
                               class="form-control" value="<?php echo e(old('cost')); ?>" required>
                    </div>
                </div>

                <div class="row g-3 mt-1">
                    
                    <div class="col-md-4">
                        <label class="form-label" for="payment_type"><?php echo e(__('Payment Type')); ?> <span class="text-danger">*</span></label>
                        <select id="payment_type" name="payment_type" class="form-select" required>
                            <option value="cash" <?php echo e(old('payment_type', 'cash') == 'cash' ? 'selected' : ''); ?>>
                                <i class="bi bi-cash"></i> <?php echo e(__('Cash (Paid Now)')); ?>

                            </option>
                            <option value="due" <?php echo e(old('payment_type') == 'due' ? 'selected' : ''); ?>>
                                <?php echo e(__('Due (Pay Later)')); ?>

                            </option>
                        </select>
                        <div id="due_note" class="form-text text-warning d-none">
                            <i class="bi bi-exclamation-triangle me-1"></i><?php echo e(__('This will be recorded in Vendor Ledger as due.')); ?>

                        </div>
                    </div>

                    
                    <div class="col-md-8">
                        <label class="form-label" for="notes"><?php echo e(__('Notes')); ?></label>
                        <input id="notes" type="text" name="notes" class="form-control" value="<?php echo e(old('notes')); ?>">
                    </div>
                </div>

                <div class="mt-4">
                    <button type="submit" class="btn btn-primary">
                        <i class="bi bi-save me-1"></i><?php echo e(__('Save Purchase')); ?>

                    </button>
                    <a href="<?php echo e(route('purchases.index')); ?>" class="btn btn-secondary"><?php echo e(__('Cancel')); ?></a>
                </div>
            </form>
        </div>
    </div>

    <script>
    (function () {
        const categorySelect = document.getElementById('item_category');
        const itemSelect     = document.getElementById('item_id');
        const paymentSelect  = document.getElementById('payment_type');
        const dueNote        = document.getElementById('due_note');
        const allOptions     = Array.from(itemSelect.options).slice(1); // skip placeholder

        function filterItems() {
            const chosen = categorySelect.value;
            // Clear and re-add placeholder
            while (itemSelect.options.length > 1) itemSelect.remove(1);
            allOptions.forEach(opt => {
                if (!chosen || opt.dataset.category === chosen) {
                    itemSelect.add(opt.cloneNode(true));
                }
            });

            if (itemSelect.tomselect) {
                itemSelect.tomselect.clear(true);
                itemSelect.tomselect.sync();
            }
        }

        function toggleDueNote() {
            dueNote.classList.toggle('d-none', paymentSelect.value !== 'due');
        }

        categorySelect.addEventListener('change', filterItems);
        paymentSelect.addEventListener('change', toggleDueNote);

        // Init
        filterItems();
        toggleDueNote();
    })();
    </script>
 <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54)): ?>
<?php $attributes = $__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54; ?>
<?php unset($__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal9ac128a9029c0e4701924bd2d73d7f54)): ?>
<?php $component = $__componentOriginal9ac128a9029c0e4701924bd2d73d7f54; ?>
<?php unset($__componentOriginal9ac128a9029c0e4701924bd2d73d7f54); ?>
<?php endif; ?>
<?php /**PATH C:\Users\anwar\OneDrive\Desktop\Babu KSA\software\restaurant-management\resources\views/purchases/create.blade.php ENDPATH**/ ?>