<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once __DIR__ . '/../config.php'; // DB connection
require_once __DIR__ . '/layout.php';    // layout, auth, sidebar
require_once __DIR__ . '/includes/header.php';

try {
    // Fetch parks
    $stmt = $pdo->query("
        SELECT id, name, slug
        FROM parks
        ORDER BY name ASC
    ");
    $parks = $stmt->fetchAll(PDO::FETCH_ASSOC);

} catch (PDOException $e) {
    die("Database error: " . htmlspecialchars($e->getMessage()));
}
?>

<h1>All Parks</h1>

<div class="d-flex justify-content-end mb-3">
  <a href="create-park.php" class="btn btn-sm btn-success" style="width: 120px;">Create Park</a>
</div>

<table class="table table-striped">
    <thead>
        <tr>
            <th>Park Name</th>
            <th>Edit</th>
            <th>View</th>
        </tr>
    </thead>
    <tbody>
        <?php if (empty($parks)): ?>
            <tr><td colspan="3" class="text-center">No parks found.</td></tr>
        <?php else: ?>
            <?php foreach ($parks as $park): ?>
                <tr>
                    <td><?= htmlspecialchars($park['name']) ?></td>
                    <td>
                        <a href="edit-park.php?id=<?= (int)$park['id'] ?>" class="btn btn-sm btn-primary">Edit</a>
                    </td>
                    <td>
                        <a href="/park/<?= urlencode($park['slug']) ?>" class="btn btn-sm btn-secondary" target="_blank">View</a>
                    </td>
                </tr>
            <?php endforeach; ?>
        <?php endif; ?>
    </tbody>
</table>
