<?php 
$pageTitle = 'Quick Transaction';
include __DIR__ . '/../layouts/header.php'; 
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h2><i class="bi bi-arrow-left-right me-2"></i>Record Transaction</h2>
    <a href="<?= $this->baseUrl('/clients') ?>" class="btn btn-outline-secondary">
        <i class="bi bi-arrow-left"></i> Back to Clients
    </a>
</div>

<div class="card">
    <div class="card-body">
        <form method="post" action="<?= $this->baseUrl('/clients/transaction') ?>">
            <div class="row">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Existing Client</label>
                        <select name="client_id" class="form-select" id="existingClient">
                            <option value="">-- Choose a client --</option>
                            <?php foreach ($clients as $c): ?>
                                <option value="<?= $c['id'] ?>">
                                    <?= htmlspecialchars($c['full_name']) ?> (<?= $c['client_code'] ?>)
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Or add new client on the fly</label>
                        <input type="text" name="new_client_name" class="form-control" 
                               id="newClientName" placeholder="Type full name for a new client"
                               oninput="document.getElementById('existingClient').disabled = this.value.trim() !== '';">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Transaction Type <span class="text-danger">*</span></label>
                        <select name="type" class="form-select" required>
                            <option value="debit">Debit (withdrawal / service fee)</option>
                            <option value="credit">Credit (adjustment / refund)</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Amount (KES) <span class="text-danger">*</span></label>
                        <input type="number" step="0.01" name="amount" class="form-control" required autofocus>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Description</label>
                        <input type="text" name="description" class="form-control" 
                               placeholder="e.g., Service fee, Withdrawal">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Reference</label>
                        <input type="text" name="reference" class="form-control" 
                               placeholder="Optional reference code">
                    </div>
                </div>
            </div>
            <hr>
            <div class="d-flex gap-2">
                <button type="submit" class="btn btn-primary">
                    <i class="bi bi-save"></i> Submit Transaction
                </button>
                <a href="<?= $this->baseUrl('/clients') ?>" class="btn btn-outline-secondary">
                    <i class="bi bi-x-circle"></i> Cancel
                </a>
            </div>
        </form>
    </div>
</div>

<?php include __DIR__ . '/../layouts/footer.php'; ?>