<?php $__env->startSection('title', 'Record Expense'); ?>
<?php $__env->startSection('page_title', 'Record Expense'); ?>
<?php $__env->startSection('breadcrumbs'); ?>
    <li class="breadcrumb-item"><a href="<?php echo e(route('journals.index')); ?>">Journals</a></li><li class="breadcrumb-item active">Expense</li>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>
    <div class="card"><div class="card-body">
        <form method="POST" action="<?php echo e(route('journals.expense.store')); ?>">
            <?php echo csrf_field(); ?>
            <div class="row g-3 mb-3">
                <div class="col-md-3"><label class="form-label">Date</label>
                    <input type="date" name="date" value="<?php echo e(old('date', now()->format('Y-m-d'))); ?>" class="form-control" required></div>
                <div class="col-md-4"><label class="form-label">Paid From (Credit)</label>
                    <select name="pay_account_id" class="form-select" required>
                        <?php $__currentLoopData = $payAccounts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $a): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($a->id); ?>" <?php if(old('pay_account_id')==$a->id): echo 'selected'; endif; ?>><?php echo e($a->label); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </select></div>
            </div>

            <label class="form-label fw-semibold">Expenses</label>
            <div class="table-responsive">
                <table class="table table-sm align-middle" id="expRows">
                    <thead><tr>
                        <th style="width:32%">Expense Account (Debit)</th>
                        <th style="width:16%">Amount</th>
                        <th style="width:34%">Description</th>
                        <th style="width:14%">Reference</th>
                        <th style="width:4%"></th>
                    </tr></thead>
                    <tbody>
                        <?php $old = old('expenses', [['']]); ?>
                        <?php $__currentLoopData = $old; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $i => $row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <tr class="exp-row">
                                <td>
                                    <select name="expenses[<?php echo e($i); ?>][expense_account_id]" class="form-select form-select-sm" required>
                                        <?php $__currentLoopData = $expenseAccounts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $a): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($a->id); ?>" <?php if(($row['expense_account_id'] ?? '')==$a->id): echo 'selected'; endif; ?>><?php echo e($a->label); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                </td>
                                <td><input type="number" step="0.01" min="0.01" name="expenses[<?php echo e($i); ?>][amount]" value="<?php echo e($row['amount'] ?? ''); ?>" class="form-control form-control-sm" required></td>
                                <td><input type="text" name="expenses[<?php echo e($i); ?>][description]" value="<?php echo e($row['description'] ?? ''); ?>" class="form-control form-control-sm" placeholder="What was this for?" required></td>
                                <td><input type="text" name="expenses[<?php echo e($i); ?>][reference]" value="<?php echo e($row['reference'] ?? ''); ?>" class="form-control form-control-sm" placeholder="Bill #"></td>
                                <td class="text-center"><button type="button" class="btn btn-sm btn-light border text-danger exp-del" title="Remove"><i class="bi bi-x-lg"></i></button></td>
                            </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </tbody>
                    <tfoot>
                        <tr>
                            <td><button type="button" class="btn btn-sm btn-outline-primary" id="expAdd"><i class="bi bi-plus-lg me-1"></i>Add More</button></td>
                            <td class="fw-bold">Rs <span id="expTotal">0</span></td>
                            <td colspan="3"></td>
                        </tr>
                    </tfoot>
                </table>
            </div>

            <div class="mt-2">
                <button class="btn btn-danger"><i class="bi bi-check-lg me-1"></i>Record Expense(s)</button>
                <a href="<?php echo e(route('journals.index')); ?>" class="btn btn-light border">Cancel</a>
            </div>
        </form>
    </div></div>

    
    <template id="expTemplate">
        <tr class="exp-row">
            <td>
                <select name="expenses[__i__][expense_account_id]" class="form-select form-select-sm" required>
                    <?php $__currentLoopData = $expenseAccounts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $a): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($a->id); ?>"><?php echo e($a->label); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
            </td>
            <td><input type="number" step="0.01" min="0.01" name="expenses[__i__][amount]" class="form-control form-control-sm" required></td>
            <td><input type="text" name="expenses[__i__][description]" class="form-control form-control-sm" placeholder="What was this for?" required></td>
            <td><input type="text" name="expenses[__i__][reference]" class="form-control form-control-sm" placeholder="Bill #"></td>
            <td class="text-center"><button type="button" class="btn btn-sm btn-light border text-danger exp-del" title="Remove"><i class="bi bi-x-lg"></i></button></td>
        </tr>
    </template>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
    (function () {
        const tbody = document.querySelector('#expRows tbody');
        const tpl = document.getElementById('expTemplate').innerHTML;
        let idx = tbody.querySelectorAll('.exp-row').length;

        function recalc() {
            let t = 0;
            tbody.querySelectorAll('input[name$="[amount]"]').forEach(i => t += parseFloat(i.value) || 0);
            document.getElementById('expTotal').textContent = t.toLocaleString();
        }
        document.getElementById('expAdd').addEventListener('click', () => {
            const html = tpl.replace(/__i__/g, idx++);
            tbody.insertAdjacentHTML('beforeend', html);
            recalc();
        });
        tbody.addEventListener('click', (e) => {
            if (e.target.closest('.exp-del')) {
                if (tbody.querySelectorAll('.exp-row').length > 1) e.target.closest('tr').remove();
                else { e.target.closest('tr').querySelectorAll('input').forEach(i => i.value = ''); }
                recalc();
            }
        });
        tbody.addEventListener('input', (e) => { if (e.target.name && e.target.name.endsWith('[amount]')) recalc(); });
        recalc();
    })();
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /home/u385418287/domains/itartificer.com/public_html/sms-erp/resources/views/accounting/journals/expense.blade.php ENDPATH**/ ?>