<?php
$cheque       = $cheque ?? [];
$bankAccounts = $bankAccounts ?? [];
$cashAccounts = $cashAccounts ?? [];
$direction    = old('direction', $cheque['direction'] ?? 'issued');
?>
<div class="enterprise-settings-fields">
    <div class="enterprise-field">
        <label class="enterprise-field-label">Cheque Number <span class="text-danger">*</span></label>
        <input type="text" name="cheque_number" class="form-control" maxlength="60" required
               value="<?= esc(old('cheque_number', $cheque['cheque_number'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Direction <span class="text-danger">*</span></label>
        <select name="direction" class="form-select" required>
            <option value="issued" <?= $direction === 'issued' ? 'selected' : '' ?>>Issued</option>
            <option value="received" <?= $direction === 'received' ? 'selected' : '' ?>>Received</option>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Party Name <span class="text-danger">*</span></label>
        <input type="text" name="party_name" class="form-control" maxlength="200" required
               value="<?= esc(old('party_name', $cheque['party_name'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Cheque Date <span class="text-danger">*</span></label>
        <input type="date" name="cheque_date" class="form-control" required
               value="<?= esc(old('cheque_date', $cheque['cheque_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.01" required
               value="<?= esc(old('amount', $cheque['amount'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Bank Account <?= $direction === 'issued' ? '<span class="text-danger">*</span>' : '' ?></label>
        <select name="bank_account_id" class="form-select">
            <option value="">— Select —</option>
            <?php foreach ($bankAccounts as $a): ?>
                <option value="<?= $a['id'] ?>" <?= (string) old('bank_account_id', $cheque['bank_account_id'] ?? '') === (string) $a['id'] ? 'selected' : '' ?>>
                    <?= esc($a['code']) ?> · <?= esc($a['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
        <p class="form-text small text-muted mb-0">Required to clear; required for issued cheques.</p>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Cash / Offset Account</label>
        <select name="cash_account_id" class="form-select">
            <option value="">— Select —</option>
            <?php foreach ($cashAccounts as $a): ?>
                <option value="<?= $a['id'] ?>" <?= (string) old('cash_account_id', $cheque['cash_account_id'] ?? '') === (string) $a['id'] ? 'selected' : '' ?>>
                    <?= esc($a['code']) ?> · <?= esc($a['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
        <p class="form-text small text-muted mb-0">Required when clearing (offset side of the journal).</p>
    </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', $cheque['notes'] ?? '')) ?></textarea>
    </div>
</div>
