<?php 
$pageTitle = 'Manual M‑Pesa Entry';
$clients = $clients ?? [];
include __DIR__ . '/../layouts/header.php'; 
?>

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

<div class="row">
    <div class="col-lg-8">
        <div class="card">
            <div class="card-body">
                <form method="post" action="<?= $this->baseUrl('/mpesa') ?>">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Receipt Number</label>
                                <input type="text" name="receipt_number" class="form-control" 
                                       placeholder="e.g., QW7H8K9L2M">
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Completion Date / Time</label>
                                <input type="datetime-local" name="completion_date" class="form-control" 
                                       value="<?= date('Y-m-d\TH:i') ?>">
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Details / Description</label>
                                <input type="text" name="details" class="form-control" 
                                       placeholder="Payment from customer, etc.">
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Status</label>
                                <select name="transaction_status" class="form-select">
                                    <option value="Completed">Completed</option>
                                    <option value="Pending">Pending</option>
                                    <option value="Failed">Failed</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Paid In (KES)</label>
                                <input type="number" step="0.01" name="paid_in" class="form-control" value="0" autofocus>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Withdrawn (KES)</label>
                                <input type="number" step="0.01" name="withdrawn" class="form-control" value="0">
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Balance After Transaction</label>
                                <input type="number" step="0.01" name="balance" class="form-control">
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Transaction Type</label>
                                <select name="transaction_type" class="form-select">
                                    <option value="Pay Bill">Pay Bill</option>
                                    <option value="Buy Goods">Buy Goods</option>
                                    <option value="Send Money">Send Money</option>
                                    <option value="Withdrawal">Withdrawal</option>
                                    <option value="Other">Other</option>
                                </select>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Other Party (Sender / Receiver)</label>
                                <input type="text" name="other_party" class="form-control" 
                                       placeholder="254712345678 - John Doe">
                            </div>
                        </div>
                    </div>
                    <hr>
                    <h5 class="mb-3"><i class="bi bi-link-45deg me-2"></i>Link to Client (Optional)</h5>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Select Client</label>
                                <select name="client_id" class="form-select">
                                    <option value="">-- None (just record transaction) --</option>
                                    <?php foreach ($clients as $c): ?>
                                        <option value="<?= $c['id'] ?>">
                                            <?= htmlspecialchars($c['full_name']) ?> (<?= $c['client_code'] ?>)
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label d-block">&nbsp;</label>
                                <div class="form-check">
                                    <input type="checkbox" name="create_deposit" value="1" class="form-check-input" id="createDeposit">
                                    <label class="form-check-label" for="createDeposit">
                                        Automatically create a deposit for this client (if Paid In > 0)
                                    </label>
                                </div>
                            </div>
                        </div>
                    </div>
                    <hr>
                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary"><i class="bi bi-save"></i> Save Transaction</button>
                        <a href="<?= $this->baseUrl('/mpesa') ?>" class="btn btn-outline-secondary">Cancel</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="col-lg-4">
        <div class="card">
            <div class="card-body">
                <h5 class="card-title"><i class="bi bi-info-circle me-2"></i>Manual Entry Tips</h5>
                <p class="text-muted small">Use this form to record a single M‑Pesa transaction that wasn’t part of a bulk upload.</p>
                <hr>
                <ul class="small text-muted">
                    <li>If you select a client and <strong>Paid In > 0</strong>, a deposit will be created automatically.</li>
                    <li>Leave the client empty to just log the transaction for reconciliation.</li>
                </ul>
            </div>
        </div>
    </div>
</div>

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