<?php
$payment   = $payment ?? [];
$suppliers = $suppliers ?? [];
$bills     = $bills ?? [];
$isEdit    = ! empty($payment['id']);
?>
<div class="enterprise-settings-fields">
    <div class="enterprise-field">
        <label class="enterprise-field-label">Supplier <span class="text-danger">*</span></label>
        <select name="supplier_id" class="form-select" required>
            <option value="">— Select supplier —</option>
            <?php foreach ($suppliers as $s): ?>
                <option value="<?= $s['id'] ?>" <?= (string) old('supplier_id', $payment['supplier_id'] ?? '') === (string) $s['id'] ? 'selected' : '' ?>>
                    <?= esc($s['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Supplier Bill</label>
        <select name="supplier_bill_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($bills as $b): ?>
                <option value="<?= $b['id'] ?>" <?= (string) old('supplier_bill_id', $payment['supplier_bill_id'] ?? '') === (string) $b['id'] ? 'selected' : '' ?>>
                    <?= esc($b['number']) ?><?= ! empty($b['supplier_name']) ? ' · ' . esc($b['supplier_name']) : '' ?>
                    · <?= number_format((float) ($b['total'] ?? 0) - (float) ($b['amount_paid'] ?? 0), 2) ?> due
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Payment Date <span class="text-danger">*</span></label>
        <input type="date" name="payment_date" class="form-control" required
               value="<?= esc(old('payment_date', $payment['payment_date'] ?? date('Y-m-d'))) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Amount <span class="text-danger">*</span></label>
        <input type="number" name="amount" class="form-control" step="0.01" min="0" required
               value="<?= esc(old('amount', $payment['amount'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Method</label>
        <input type="text" name="method" class="form-control" placeholder="e.g. bank transfer, cash, cheque"
               value="<?= esc(old('method', $payment['method'] ?? '')) ?>">
    </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', $payment['notes'] ?? '')) ?></textarea>
    </div>
</div>
