<?php
$pageTitle = 'Edit University';
require_once dirname(__DIR__) . '/includes/header.php';
$id = (int)($_GET['id'] ?? 0);
if (!$id) redirect(ADMIN_URL.'/pages/universities.php');
$u = db()->query("SELECT * FROM universities WHERE id=$id")->fetch_assoc();
if (!$u) { flash('Not found.','error'); redirect(ADMIN_URL.'/pages/universities.php'); }
$destinations = db()->query("SELECT id,country_name,flag_emoji FROM destinations WHERE status='active' ORDER BY country_name")->fetch_all(MYSQLI_ASSOC);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name   = trim($_POST['name'] ?? '');
    $country= trim($_POST['country'] ?? '');
    $dest_id= (int)($_POST['destination_id'] ?? 0);
    $desc   = trim($_POST['description'] ?? '');
    $web    = trim($_POST['website'] ?? '');
    $status = $_POST['status'] ?? 'active';
    $sort   = (int)($_POST['sort_order'] ?? 0);
    $logo   = handleUpload('logo','universities') ?? trim($_POST['logo_url'] ?? $u['logo']);
    $st = db()->prepare("UPDATE universities SET name=?,country=?,destination_id=?,description=?,logo=?,website=?,sort_order=?,status=? WHERE id=?");
    $st->bind_param('ssissssii', $name,$country,$dest_id,$desc,$logo,$web,$sort,$status,$id);
    $st->execute();
    flash('University updated!','success');
    redirect(ADMIN_URL.'/pages/universities.php');
}
?>
<div class="page-header">
    <a href="universities.php" class="btn btn-secondary btn-sm"><i class="fas fa-arrow-left"></i></a>
    <h1>Edit University</h1>
</div>
<div class="card">
    <div class="card-header"><i class="fas fa-edit" style="color:var(--brand)"></i><h2><?=e($u['name'])?></h2></div>
    <div class="card-body">
        <form method="POST" enctype="multipart/form-data">
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label">University Name *</label>
                    <input type="text" name="name" class="form-control" value="<?=e($u['name'])?>" required>
                </div>
                <div class="form-group">
                    <label class="form-label">Country</label>
                    <input type="text" name="country" class="form-control" value="<?=e($u['country'])?>">
                </div>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label">Destination</label>
                    <select name="destination_id" class="form-select">
                        <option value="0">— None —</option>
                        <?php foreach($destinations as $dest): ?>
                        <option value="<?=$dest['id']?>" <?=$u['destination_id']==$dest['id']?'selected':''?>>
                            <?=e($dest['flag_emoji'].' '.$dest['country_name'])?>
                        </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">Website</label>
                    <input type="url" name="website" class="form-control" value="<?=e($u['website'])?>">
                </div>
            </div>
            <div class="form-group">
                <label class="form-label">Description</label>
                <textarea name="description" class="form-control" rows="4"><?=e($u['description'])?></textarea>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label">Replace Logo</label>
                    <input type="file" name="logo" class="form-control" accept="image/*" onchange="previewImg(this)">
                    <?php if($u['logo']): ?><img src="<?=e(SITE_URL.'/'.$u['logo'])?>" class="img-preview" style="margin-top:8px" id="imgPreview">
                    <?php else: ?><img id="imgPreview" class="img-preview" style="margin-top:8px;display:none"><?php endif; ?>
                </div>
                <div class="form-group">
                    <label class="form-label">Or Logo URL</label>
                    <input type="text" name="logo_url" class="form-control" value="<?=e($u['logo'])?>">
                </div>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label">Status</label>
                    <select name="status" class="form-select">
                        <option value="active" <?=$u['status']==='active'?'selected':''?>>Active</option>
                        <option value="inactive" <?=$u['status']==='inactive'?'selected':''?>>Inactive</option>
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">Sort Order</label>
                    <input type="number" name="sort_order" class="form-control" value="<?=(int)$u['sort_order']?>">
                </div>
            </div>
            <div style="display:flex;gap:10px">
                <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Update</button>
                <a href="universities.php" class="btn btn-secondary">Cancel</a>
            </div>
        </form>
    </div>
</div>
<script>function previewImg(i){const r=new FileReader();r.onload=e=>{const p=document.getElementById('imgPreview');p.src=e.target.result;p.style.display='block'};r.readAsDataURL(i.files[0]);}</script>
<?php require_once dirname(__DIR__) . '/includes/footer.php'; ?>
