<?php
$mode  = $mode ?? 'priced';
$lines = old('lines', $lines ?? []);
if (! is_array($lines) || count($lines) === 0) {
    $empty = ['item_code' => '', 'item_name' => '', 'unit' => 'pcs'];
    if ($mode !== 'price_list') {
        $empty['quantity'] = '1';
    }
    if ($mode !== 'delivery') {
        $empty['unit_price'] = '';
    }
    $lines = [$empty];
}
$showQty   = $mode !== 'price_list';
$showPrice = $mode !== 'delivery';
$taxAmount = old('tax_amount', $taxAmount ?? 0);
?>
<div class="enterprise-field" style="grid-column: 1 / -1;">
    <div class="d-flex align-items-center justify-content-between mb-2">
        <label class="enterprise-field-label mb-0">Lines</label>
        <button type="button" class="btn btn-sm btn-outline-secondary" id="doc-lines-add">
            <i class="fa-solid fa-plus me-1"></i>Add line
        </button>
    </div>
    <div class="table-responsive">
        <table class="enterprise-data-table" id="doc-lines-table">
            <thead>
                <tr>
                    <th>Code</th>
                    <th>Item</th>
                    <?php if ($showQty): ?>
                        <th style="width:100px;">Qty</th>
                    <?php endif; ?>
                    <th style="width:90px;">Unit</th>
                    <?php if ($showPrice): ?>
                        <th style="width:120px;">Unit Price</th>
                    <?php endif; ?>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($lines as $i => $line): ?>
                    <tr>
                        <td>
                            <input type="text" name="lines[<?= (int) $i ?>][item_code]" class="form-control form-control-sm"
                                   value="<?= esc($line['item_code'] ?? '') ?>">
                        </td>
                        <td>
                            <input type="text" name="lines[<?= (int) $i ?>][item_name]" class="form-control form-control-sm"
                                   value="<?= esc($line['item_name'] ?? '') ?>">
                        </td>
                        <?php if ($showQty): ?>
                            <td>
                                <input type="number" name="lines[<?= (int) $i ?>][quantity]" class="form-control form-control-sm"
                                       step="0.001" min="0" value="<?= esc($line['quantity'] ?? '1') ?>">
                            </td>
                        <?php endif; ?>
                        <td>
                            <input type="text" name="lines[<?= (int) $i ?>][unit]" class="form-control form-control-sm"
                                   value="<?= esc($line['unit'] ?? 'pcs') ?>">
                        </td>
                        <?php if ($showPrice): ?>
                            <td>
                                <input type="number" name="lines[<?= (int) $i ?>][unit_price]" class="form-control form-control-sm"
                                       step="0.01" min="0" value="<?= esc($line['unit_price'] ?? '') ?>">
                            </td>
                        <?php endif; ?>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php if ($mode === 'priced'): ?>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Tax Amount</label>
        <input type="number" name="tax_amount" class="form-control" step="0.01" min="0"
               value="<?= esc($taxAmount) ?>">
    </div>
<?php endif; ?>
<script>
(function () {
    var btn = document.getElementById('doc-lines-add');
    var table = document.getElementById('doc-lines-table');
    if (!btn || !table) return;
    btn.addEventListener('click', function () {
        var tbody = table.querySelector('tbody');
        var rows = tbody.querySelectorAll('tr');
        var last = rows[rows.length - 1];
        if (!last) return;
        var clone = last.cloneNode(true);
        var next = rows.length;
        clone.querySelectorAll('input').forEach(function (input) {
            var name = input.getAttribute('name') || '';
            input.setAttribute('name', name.replace(/lines\[\d+]/, 'lines[' + next + ']'));
            if (input.type === 'number' && name.indexOf('quantity') !== -1) {
                input.value = '1';
            } else {
                input.value = '';
            }
            if (name.indexOf('[unit]') !== -1) {
                input.value = 'pcs';
            }
        });
        tbody.appendChild(clone);
    });
})();
</script>
