<div class="flex items-center justify-between mb-6">
    <div><h1 class="text-2xl font-bold text-gray-900">Payments</h1><p class="text-sm text-gray-500 mt-1"><?= $total ?? 0 ?> total payments</p></div>
    <a href="?page=payments&action=create" class="inline-flex items-center gap-2 px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 transition text-sm font-medium">
        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/></svg>Record Payment
    </a>
</div>

<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-6">
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
        <p class="text-xs font-medium text-gray-500 uppercase">Collected This Month</p>
        <p class="text-2xl font-bold text-green-600 mt-1"><?= formatMoney($totalCollected ?? 0) ?></p>
    </div>
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
        <p class="text-xs font-medium text-gray-500 uppercase">Pending</p>
        <p class="text-2xl font-bold text-amber-600 mt-1"><?= formatMoney($totalPending ?? 0) ?></p>
    </div>
</div>

<div class="bg-white rounded-xl shadow-sm border border-gray-200">
    <div class="p-4 border-b border-gray-100">
        <form method="GET" class="flex flex-wrap gap-3">
            <input type="hidden" name="page" value="payments">
            <input type="text" name="search" value="<?= htmlspecialchars($_GET['search'] ?? '') ?>" placeholder="Search payments..." class="flex-1 min-w-[200px] px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500">
            <select name="status" class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500">
                <option value="">All Statuses</option>
                <?php foreach (['pending','completed','failed','refunded','reversed'] as $s): ?><option value="<?= $s ?>" <?= ($_GET['status'] ?? '') === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></option><?php endforeach; ?>
            </select>
            <select name="method" class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500">
                <option value="">All Methods</option>
                <?php foreach (['manual','ach','credit_card','debit_card','check','cash','money_order','online'] as $m): ?><option value="<?= $m ?>" <?= ($_GET['method'] ?? '') === $m ? 'selected' : '' ?>><?= ucwords(str_replace('_',' ',$m)) ?></option><?php endforeach; ?>
            </select>
            <button type="submit" class="px-4 py-2 bg-gray-100 text-gray-700 rounded-lg hover:bg-gray-200 text-sm font-medium">Filter</button>
        </form>
    </div>
    <div class="overflow-x-auto">
        <table class="w-full text-sm">
            <thead class="bg-gray-50 text-left"><tr>
                <th class="px-4 py-3 font-medium text-gray-600">Date</th>
                <th class="px-4 py-3 font-medium text-gray-600">Tenant</th>
                <th class="px-4 py-3 font-medium text-gray-600">Property</th>
                <th class="px-4 py-3 font-medium text-gray-600">Amount</th>
                <th class="px-4 py-3 font-medium text-gray-600">Method</th>
                <th class="px-4 py-3 font-medium text-gray-600">Status</th>
                <th class="px-4 py-3 font-medium text-gray-600 text-right">Actions</th>
            </tr></thead>
            <tbody class="divide-y divide-gray-100">
                <?php if (empty($payments)): ?>
                    <tr><td colspan="7" class="px-4 py-12 text-center text-gray-400">No payments found.</td></tr>
                <?php else: foreach ($payments as $p): ?>
                    <tr class="hover:bg-gray-50 transition">
                        <td class="px-4 py-3 text-gray-600"><?= formatDate($p['payment_date']) ?></td>
                        <td class="px-4 py-3 font-medium text-gray-900"><?= htmlspecialchars(($p['first_name'] ?? '').' '.($p['last_name'] ?? '')) ?></td>
                        <td class="px-4 py-3 text-gray-600"><?= htmlspecialchars($p['property_name'] ?? '-') ?></td>
                        <td class="px-4 py-3 font-bold text-gray-900"><?= formatMoney($p['amount']) ?></td>
                        <td class="px-4 py-3 text-gray-600"><?= ucwords(str_replace('_',' ',$p['payment_method'] ?? '-')) ?></td>
                        <td class="px-4 py-3"><?= statusBadge($p['status'] ?? 'pending') ?></td>
                        <td class="px-4 py-3 text-right">
                            <a href="?page=payments&action=show&id=<?= $p['id'] ?>" class="text-indigo-600 hover:text-indigo-800 text-xs font-medium">View</a>
                        </td>
                    </tr>
                <?php endforeach; endif; ?>
            </tbody>
        </table>
    </div>
    <?php if (!empty($pagination)): ?><div class="px-4 py-3 border-t border-gray-100"><?= $pagination ?></div><?php endif; ?>
</div>
