<?php $__env->startSection('title', __('New Purchase')); ?>
<?php $__env->startSection('content'); ?><form method="POST" action="<?php echo e(route('purchases.store')); ?>"><?php echo csrf_field(); ?><div class="row g-3"><div class="col-lg-8"><div class="card"><div class="card-body p-4"><div class="d-flex justify-content-between mb-3"><h6><?php echo e(__('Purchase Items')); ?></h6><button type="button" id="add-row" class="btn btn-light btn-sm"><i class="fa-solid fa-plus"></i> <?php echo e(__('Add Item')); ?></button></div><div class="table-responsive"><table class="table" id="items-table"><thead><tr><th><?php echo e(__('Existing Product')); ?></th><th><?php echo e(__('New Product Name')); ?></th><th><?php echo e(__('Unit')); ?></th><th><?php echo e(__('Qty')); ?></th><th><?php echo e(__('Cost')); ?></th><th><?php echo e(__('Total')); ?></th><th></th></tr></thead><tbody></tbody></table></div><small class="text-muted"><?php echo e(__('Select an existing product, or type a new product name. New products are created and stock is received automatically.')); ?></small></div></div></div><div class="col-lg-4"><div class="card"><div class="card-body p-4"><label class="form-label"><?php echo e(__('Supplier')); ?></label><select name="supplier_id" class="form-select mb-3"><option value="">—</option><?php $__currentLoopData = $suppliers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $supplier): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($supplier->id); ?>"><?php echo e($supplier->name); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select><label class="form-label"><?php echo e(__('Currency')); ?></label><select name="currency_code" class="form-select mb-3"><?php $__currentLoopData = $currencies; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $currency): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($currency->code); ?>" <?php if($currency->is_default): echo 'selected'; endif; ?>><?php echo e($currency->code); ?> — <?php echo e($currency->name); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select><label class="form-label"><?php echo e(__('Date')); ?></label><input type="date" name="purchase_date" class="form-control mb-3" value="<?php echo e(now()->toDateString()); ?>"><label class="form-label"><?php echo e(__('Amount Paid Now')); ?></label><input type="number" name="paid_amount" class="form-control" value="0" min="0" step="0.01"><select name="payment_method" class="form-select mt-2"><option value="cash"><?php echo e(__('Cash')); ?></option><option value="bank"><?php echo e(__('Bank')); ?></option><option value="mobile"><?php echo e(__('Mobile Money')); ?></option></select><hr><div class="d-flex justify-content-between"><span><?php echo e(__('Total')); ?></span><strong id="total">0.00</strong></div><label class="form-label mt-3"><?php echo e(__('Notes')); ?></label><textarea name="notes" class="form-control" rows="2"></textarea><button class="btn btn-success w-100 mt-4"><i class="fa-solid fa-box-open me-1"></i><?php echo e(__('Receive Purchase')); ?></button></div></div></div></div></form><?php $__env->stopSection(); ?>
<?php $__env->startPush('scripts'); ?><script>const products=<?php echo json_encode($productsForPurchase, 15, 512) ?>,units=<?php echo json_encode($unitsForPurchase, 15, 512) ?>;let n=0,body=document.querySelector('#items-table tbody');function total(){let t=0;body.querySelectorAll('tr').forEach(r=>{let q=+r.querySelector('.qty').value||0,c=+r.querySelector('.cost').value||0,l=q*c;r.querySelector('.line').textContent=l.toFixed(2);t+=l});document.querySelector('#total').textContent=t.toFixed(2)}function row(){let i=n++,p=['<option value=""><?php echo e(__('New product')); ?></option>',...products.map(x=>`<option value="${x.id}">${x.name} (${x.sku})</option>`)].join(''),u=units.map(x=>`<option value="${x.short}">${x.name}</option>`).join('');body.insertAdjacentHTML('beforeend',`<tr><td><select name="items[${i}][product_id]" class="form-select product">${p}</select></td><td><input name="items[${i}][product_name]" class="form-control new-name"></td><td><select name="items[${i}][unit]" class="form-select unit">${u}</select></td><td><input name="items[${i}][quantity]" class="form-control qty" type="number" value="1" min="0.01" step="0.01" required></td><td><input name="items[${i}][unit_cost]" class="form-control cost" type="number" value="0" min="0" step="0.01" required></td><td class="line">0.00</td><td><button type="button" class="btn btn-light btn-sm remove text-danger"><i class="fa-solid fa-xmark"></i></button></td></tr>`);let r=body.lastElementChild;r.querySelector('.product').onchange=()=>{let x=products.find(x=>x.id==r.querySelector('.product').value);if(x){r.querySelector('.new-name').value='';r.querySelector('.unit').value=x.unit;r.querySelector('.cost').value=x.cost}total()};r.querySelector('.qty').oninput=total;r.querySelector('.cost').oninput=total;r.querySelector('.remove').onclick=()=>{r.remove();total()}}document.querySelector('#add-row').onclick=row;row()</script><?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\xampp\htdocs\TradeERP\resources\views/purchases/create.blade.php ENDPATH**/ ?>