<?php

declare(strict_types=1);

require_once __DIR__ . '/../includes/bootstrap.php';

$config = require __DIR__ . '/../config/config.php';

$title = 'DB Test';

try {
    $pdo = Database::pdo();
    $services = $pdo->query('SELECT service_id, service_name, category, is_active FROM services ORDER BY service_id DESC LIMIT 10')->fetchAll();
    $dbStatus = 'Connected';
} catch (Throwable $e) {
    $services = [];
    $dbStatus = 'Not connected: ' . $e->getMessage();
}

ob_start();
?>
<div class="space-y-6">
    <div class="rounded-xl border bg-white p-6">
        <h1 class="text-xl font-semibold">Database Test</h1>
        <p class="mt-1 text-sm text-slate-600">Status: <span class="font-medium text-slate-900"><?= e($dbStatus) ?></span></p>
    </div>

    <div class="rounded-xl border bg-white p-6">
        <h2 class="text-base font-semibold">Latest Services</h2>
        <div class="mt-4 overflow-x-auto">
            <table class="min-w-full text-sm">
                <thead class="text-left text-slate-600">
                    <tr class="border-b">
                        <th class="py-2 pr-4">ID</th>
                        <th class="py-2 pr-4">Service</th>
                        <th class="py-2 pr-4">Category</th>
                        <th class="py-2">Active</th>
                    </tr>
                </thead>
                <tbody>
                <?php if (!$services): ?>
                    <tr class="border-b">
                        <td class="py-3 text-slate-500" colspan="4">No rows found.</td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($services as $row): ?>
                        <tr class="border-b last:border-0">
                            <td class="py-3 pr-4"><?= e((string)$row['service_id']) ?></td>
                            <td class="py-3 pr-4 font-medium"><?= e((string)$row['service_name']) ?></td>
                            <td class="py-3 pr-4"><?= e((string)($row['category'] ?? '')) ?></td>
                            <td class="py-3"><?= ((int)$row['is_active'] === 1) ? 'Yes' : 'No' ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<?php
$content = ob_get_clean();
require __DIR__ . '/../includes/layout.php';
