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

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

// Filter by destination
$destFilter = (int)($_GET['dest'] ?? 0);
$where = $destFilter ? "WHERE destination_id=$destFilter" : "";
$universities = db()->query("SELECT u.*, d.country_name, d.flag_emoji FROM universities u LEFT JOIN destinations d ON u.destination_id=d.id $where ORDER BY u.sort_order, u.name")->fetch_all(MYSQLI_ASSOC);
$destinations = db()->query("SELECT id,country_name,flag_emoji FROM destinations ORDER BY country_name")->fetch_all(MYSQLI_ASSOC);
?>
<div class="page-header">
    <h1>Universities</h1>
    <div class="page-header-actions" style="display:flex;gap:10px;align-items:center">
        <select onchange="location='universities.php'+(this.value?'?dest='+this.value:'')" class="form-select" style="width:180px;padding:7px 12px">
            <option value="">All Destinations</option>
            <?php foreach($destinations as $dest): ?>
            <option value="<?=$dest['id']?>" <?=$destFilter==$dest['id']?'selected':''?>><?=e($dest['flag_emoji'].' '.$dest['country_name'])?></option>
            <?php endforeach; ?>
        </select>
        <a href="uni-add.php" class="btn btn-primary"><i class="fas fa-plus"></i> Add University</a>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <i class="fas fa-university" style="color:var(--brand)"></i>
        <h2>All Universities</h2>
        <span style="color:var(--muted);font-size:13px;margin-left:auto"><?= count($universities) ?> universities</span>
    </div>
    <div class="table-wrap">
        <?php if (empty($universities)): ?>
        <div style="padding:48px;text-align:center;color:var(--muted)">
            <i class="fas fa-university" style="font-size:36px;opacity:.3;display:block;margin-bottom:12px"></i>
            No universities yet. <a href="uni-add.php" style="color:var(--brand)">Add first university</a>
        </div>
        <?php else: ?>
        <table>
            <thead>
                <tr><th>Logo</th><th>University</th><th>Destination</th><th>Website</th><th>Status</th><th>Actions</th></tr>
            </thead>
            <tbody>
            <?php foreach ($universities as $u): ?>
            <tr>
                <td><?php if($u['logo']): ?><img src="<?=e(SITE_URL.'/'.$u['logo'])?>" class="img-preview" style="width:40px;height:40px;object-fit:contain"><?php else: ?><div style="width:40px;height:40px;background:#f3f4f6;border-radius:8px;display:grid;place-items:center;color:#9ca3af"><i class="fas fa-university"></i></div><?php endif; ?></td>
                <td style="font-weight:600"><?= e($u['name']) ?></td>
                <td><?php if($u['country_name']): ?><span class="badge badge-blue"><?=e(($u['flag_emoji']??'').' '.$u['country_name'])?></span><?php else: ?><span style="color:var(--muted);font-size:13px"><?=e($u['country'])?></span><?php endif; ?></td>
                <td><?php if($u['website']): ?><a href="<?=e($u['website'])?>" target="_blank" style="color:var(--brand);font-size:13px"><i class="fas fa-external-link-alt"></i> Visit</a><?php else: ?><span style="color:var(--muted)">—</span><?php endif; ?></td>
                <td><span class="badge <?= $u['status']==='active'?'badge-green':'badge-yellow' ?>"><?= ucfirst($u['status']) ?></span></td>
                <td>
                    <div style="display:flex;gap:6px">
                        <a href="uni-edit.php?id=<?= $u['id'] ?>" class="btn btn-secondary btn-sm btn-icon" title="Edit"><i class="fas fa-edit"></i></a>
                        <button onclick="confirmDelete('universities.php?delete=<?= $u['id'] ?>','<?= e(addslashes($u['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'; ?>
