<div class="mb-6"><h1 class="text-2xl font-bold text-gray-900">API Center</h1><p class="text-sm text-gray-500 mt-1">Manage API keys and webhooks for external integrations</p></div>

<!-- API Keys -->
<div class="bg-white rounded-xl shadow-sm border border-gray-200 mb-6">
    <div class="px-6 py-4 border-b border-gray-100 flex justify-between items-center"><h3 class="font-semibold text-gray-900">API Keys</h3>
        <form method="POST" action="?page=api&action=createKey" class="flex gap-2"><?= CSRF::field() ?>
            <input type="text" name="name" placeholder="Key name" required class="px-3 py-1.5 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500">
            <button type="submit" class="px-3 py-1.5 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 text-sm font-medium">Generate</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">Name</th><th class="px-4 py-3 font-medium text-gray-600">Key Prefix</th><th class="px-4 py-3 font-medium text-gray-600">Permissions</th><th class="px-4 py-3 font-medium text-gray-600">Created</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($apiKeys)): ?><tr><td colspan="5" class="px-4 py-8 text-center text-gray-400">No API keys yet.</td></tr>
                <?php else: foreach ($apiKeys as $k): ?>
                <tr class="hover:bg-gray-50">
                    <td class="px-4 py-3 font-medium text-gray-900"><?= htmlspecialchars($k['name']) ?></td>
                    <td class="px-4 py-3 font-mono text-xs text-gray-600"><?= htmlspecialchars($k['prefix'] ?? '***') ?>...</td>
                    <td class="px-4 py-3"><?php foreach (json_decode($k['permissions'] ?? '[]', true) as $perm): ?><span class="inline-flex px-2 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-700 mr-1"><?= $perm ?></span><?php endforeach; ?></td>
                    <td class="px-4 py-3 text-gray-500 text-xs"><?= timeAgo($k['created_at']) ?></td>
                    <td class="px-4 py-3 text-right"><form method="POST" action="?page=api&action=deleteKey&id=<?= $k['id'] ?>" class="inline" onsubmit="return confirm('Revoke this key?')"><?= CSRF::field() ?><button class="text-red-600 hover:text-red-800 text-xs font-medium">Revoke</button></form></td>
                </tr>
                <?php endforeach; endif; ?>
            </tbody>
        </table>
    </div>
</div>

<!-- Webhooks -->
<div class="bg-white rounded-xl shadow-sm border border-gray-200 mb-6">
    <div class="px-6 py-4 border-b border-gray-100 flex justify-between items-center"><h3 class="font-semibold text-gray-900">Webhooks</h3></div>
    <div class="p-4 border-b border-gray-100">
        <form method="POST" action="?page=api&action=createWebhook" class="flex flex-wrap gap-2 items-end"><?= CSRF::field() ?>
            <div class="flex-1 min-w-[200px]"><label class="block text-xs text-gray-500 mb-1">Endpoint URL</label><input type="url" name="url" required placeholder="https://..." 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-xs text-gray-500 mb-1">Events</label>
                <select name="events[]" multiple class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500">
                    <?php foreach (['payment.created','payment.failed','lease.created','lease.expired','maintenance.created','maintenance.completed','tenant.created','tenant.deleted'] as $ev): ?><option value="<?= $ev ?>"><?= $ev ?></option><?php endforeach; ?>
                </select></div>
            <button type="submit" class="px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 text-sm font-medium">Add</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">URL</th><th class="px-4 py-3 font-medium text-gray-600">Events</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($webhooks)): ?><tr><td colspan="4" class="px-4 py-8 text-center text-gray-400">No webhooks configured.</td></tr>
                <?php else: foreach ($webhooks as $wh): ?>
                <tr class="hover:bg-gray-50">
                    <td class="px-4 py-3 font-mono text-xs text-gray-700 truncate max-w-xs"><?= htmlspecialchars($wh['url']) ?></td>
                    <td class="px-4 py-3 text-xs"><?php foreach (json_decode($wh['events'] ?? '[]', true) as $ev): ?><span class="inline-block px-1.5 py-0.5 bg-gray-100 text-gray-600 rounded mr-1 mb-1"><?= $ev ?></span><?php endforeach; ?></td>
                    <td class="px-4 py-3"><?= statusBadge($wh['status'] ?? 'active') ?></td>
                    <td class="px-4 py-3 text-right"><form method="POST" action="?page=api&action=deleteWebhook&id=<?= $wh['id'] ?>" class="inline" onsubmit="return confirm('Delete?')"><?= CSRF::field() ?><button class="text-red-600 hover:text-red-800 text-xs font-medium">Delete</button></form></td>
                </tr>
                <?php endforeach; endif; ?>
            </tbody>
        </table>
    </div>
</div>

<!-- Developer Docs Placeholder -->
<div class="bg-gradient-to-r from-gray-50 to-slate-50 rounded-xl border border-gray-200 p-6">
    <h3 class="font-semibold text-gray-900 mb-1">API Documentation</h3>
    <p class="text-sm text-gray-500">REST API documentation with endpoint references, authentication guides, and code examples. <span class="text-xs px-2 py-0.5 bg-gray-100 text-gray-600 rounded-full font-medium ml-1">Coming Soon</span></p>
</div>
