<?php $editing = !empty($payment); ?>
<div class="max-w-3xl mx-auto">
    <div class="flex items-center gap-3 mb-6">
        <a href="?page=payments" class="text-gray-400 hover:text-gray-600"><svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"/></svg></a>
        <h1 class="text-2xl font-bold text-gray-900"><?= $editing ? 'Edit Payment' : 'Record Payment' ?></h1>
    </div>
    <form method="POST" action="?page=payments&action=<?= $editing ? 'update&id='.$payment['id'] : 'store' ?>" class="space-y-6">
        <?= CSRF::field() ?>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Tenant <span class="text-red-500">*</span></label>
                    <select name="tenant_id" required class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><option value="">Select Tenant</option>
                    <?php foreach ($tenants as $t): ?><option value="<?= $t['id'] ?>" <?= old('tenant_id', $payment['tenant_id'] ?? $_GET['tenant_id'] ?? '') == $t['id'] ? 'selected' : '' ?>><?= htmlspecialchars($t['first_name'].' '.$t['last_name']) ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Lease</label>
                    <select name="lease_id" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><option value="">Select Lease</option>
                    <?php foreach ($leases as $l): ?><option value="<?= $l['id'] ?>" <?= old('lease_id', $payment['lease_id'] ?? $_GET['lease_id'] ?? '') == $l['id'] ? 'selected' : '' ?>><?= htmlspecialchars($l['first_name'].' '.$l['last_name'].' - '.($l['unit_number'] ?? '').' ('.formatMoney($l['rent_amount']).')') ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Amount <span class="text-red-500">*</span></label><input type="number" name="amount" value="<?= old('amount', $payment['amount'] ?? '') ?>" step="0.01" min="0" required class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Payment Date <span class="text-red-500">*</span></label><input type="date" name="payment_date" value="<?= old('payment_date', $payment['payment_date'] ?? date('Y-m-d')) ?>" required class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Method</label>
                    <select name="payment_method" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500">
                    <?php foreach (['manual'=>'Manual','ach'=>'ACH','credit_card'=>'Credit Card','debit_card'=>'Debit Card','check'=>'Check','cash'=>'Cash','money_order'=>'Money Order','online'=>'Online'] as $v=>$l): ?>
                        <option value="<?= $v ?>" <?= old('payment_method', $payment['payment_method'] ?? 'manual') === $v ? 'selected' : '' ?>><?= $l ?></option>
                    <?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Status</label>
                    <select name="status" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500">
                    <?php foreach (['pending'=>'Pending','completed'=>'Completed','failed'=>'Failed','refunded'=>'Refunded'] as $v=>$l): ?>
                        <option value="<?= $v ?>" <?= old('status', $payment['status'] ?? 'completed') === $v ? 'selected' : '' ?>><?= $l ?></option>
                    <?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Reference #</label><input type="text" name="reference_number" value="<?= htmlspecialchars(old('reference_number', $payment['reference_number'] ?? '')) ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
            </div>
            <div class="mt-4"><label class="block text-sm font-medium text-gray-700 mb-1">Notes</label><textarea name="notes" rows="2" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><?= htmlspecialchars(old('notes', $payment['notes'] ?? '')) ?></textarea></div>
        </div>
        <div class="flex justify-end gap-3">
            <a href="?page=payments" class="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 text-sm font-medium">Cancel</a>
            <button type="submit" class="px-6 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 text-sm font-medium"><?= $editing ? 'Update' : 'Record Payment' ?></button>
        </div>
    </form>
</div>
