<?php
$pageTitle = 'Destinations';
require_once dirname(__DIR__) . '/includes/header.php';

if (isset($_GET['delete'])) {
    db()->query("DELETE FROM destinations WHERE id=".(int)$_GET['delete']);
    flash('Destination deleted.','success');
    redirect(ADMIN_URL.'/pages/destinations.php');
}

$destinations = db()->query("SELECT * FROM destinations ORDER BY sort_order,country_name")->fetch_all(MYSQLI_ASSOC);
?>
<div class="page-header">
    <h1>Destinations</h1>
    <div class="page-header-actions">
        <a href="dest-add.php" class="btn btn-primary"><i class="fas fa-plus"></i> Add Destination</a>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <i class="fas fa-globe-asia" style="color:var(--brand)"></i>
        <h2>All Destinations</h2>
        <span style="color:var(--muted);font-size:13px;margin-left:auto"><?= count($destinations) ?> destinations</span>
    </div>
    <div class="table-wrap">
        <?php if (empty($destinations)): ?>
        <div style="padding:48px;text-align:center;color:var(--muted)">
            <i class="fas fa-globe" style="font-size:36px;opacity:.3;display:block;margin-bottom:12px"></i>
            No destinations yet. <a href="dest-add.php" style="color:var(--brand)">Add first destination</a>
        </div>
        <?php else: ?>
        <table>
            <thead>
                <tr><th>Flag</th><th>Country</th><th>Universities</th><th>Status</th><th>Actions</th></tr>
            </thead>
            <tbody>
            <?php foreach ($destinations as $d): ?>
            <tr>
                <td style="font-size:24px"><?= e($d['flag_emoji']) ?></td>
                <td style="font-weight:600"><?= e($d['country_name']) ?></td>
                <td style="color:var(--muted)"><?= (int)$d['universities_count'] ?> universities</td>
                <td><span class="badge <?= $d['status']==='active'?'badge-green':'badge-yellow' ?>"><?= ucfirst($d['status']) ?></span></td>
                <td>
                    <div style="display:flex;gap:6px">
                        <a href="dest-edit.php?id=<?= $d['id'] ?>" class="btn btn-secondary btn-sm btn-icon" title="Edit"><i class="fas fa-edit"></i></a>
                        <button onclick="confirmDelete('destinations.php?delete=<?= $d['id'] ?>','<?= e(addslashes($d['country_name'])) ?>')" class="btn btn-danger btn-sm btn-icon" title="Delete"><i class="fas fa-trash"></i></button>
                    </div>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>
    </div>
</div>

<?php require_once dirname(__DIR__) . '/includes/footer.php'; ?>
