<?php
$note      = $note ?? [];
$companies = $companies ?? [];
$invoices  = $invoices ?? [];
$lines     = $lines ?? [];
$taxAmount = $taxAmount ?? ($note['tax_amount'] ?? 0);
?>
<div class="enterprise-settings-fields">
    <div class="enterprise-field">
        <label class="enterprise-field-label">Customer <span class="text-danger">*</span></label>
        <select name="company_id" class="form-select" required>
            <option value="">— Select customer —</option>
            <?php foreach ($companies as $c): ?>
                <option value="<?= $c['id'] ?>" <?= (string) old('company_id', $note['company_id'] ?? '') === (string) $c['id'] ? 'selected' : '' ?>>
                    <?= esc($c['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Customer Invoice</label>
        <select name="customer_invoice_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($invoices as $inv): ?>
                <option value="<?= $inv['id'] ?>" <?= (string) old('customer_invoice_id', $note['customer_invoice_id'] ?? '') === (string) $inv['id'] ? 'selected' : '' ?>>
                    <?= esc($inv['number']) ?><?= ! empty($inv['company_name']) ? ' · ' . esc($inv['company_name']) : '' ?>
                    · <?= number_format((float) ($inv['total'] ?? 0) - (float) ($inv['amount_paid'] ?? 0), 2) ?> due
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Credit Date <span class="text-danger">*</span></label>
        <input type="date" name="credit_date" class="form-control" required
               value="<?= esc(old('credit_date', $note['credit_date'] ?? date('Y-m-d'))) ?>">
    </div>
    <div class="enterprise-field" style="grid-column: 1 / -1;">
        <label class="enterprise-field-label">Notes</label>
        <textarea name="notes" class="form-control" rows="3"><?= esc(old('notes', $note['notes'] ?? '')) ?></textarea>
    </div>
    <?= $this->include('Modules\Sale\Views\partials\document_lines', ['lines' => $lines, 'mode' => 'priced', 'taxAmount' => $taxAmount]) ?>
</div>
