<?php
$pageTitle = 'Edit Destination';
require_once dirname(__DIR__) . '/includes/header.php';
$id = (int)($_GET['id'] ?? 0);
if (!$id) redirect(ADMIN_URL.'/pages/destinations.php');
$d = db()->query("SELECT * FROM destinations WHERE id=$id")->fetch_assoc();
if (!$d) { flash('Not found.','error'); redirect(ADMIN_URL.'/pages/destinations.php'); }

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name   = trim($_POST['country_name'] ?? '');
    $flag   = trim($_POST['flag_emoji'] ?? '');
    $desc   = trim($_POST['description'] ?? '');
    $content= $_POST['content'] ?? '';
    $unis   = (int)($_POST['universities_count'] ?? 0);
    $status = $_POST['status'] ?? 'active';
    $sort   = (int)($_POST['sort_order'] ?? 0);
    $img    = handleUpload('image','destinations') ?? trim($_POST['image_url'] ?? $d['image']);
    $st = db()->prepare("UPDATE destinations SET country_name=?,flag_emoji=?,description=?,content=?,image=?,universities_count=?,sort_order=?,status=? WHERE id=?");
    $st->bind_param('sssssiiii', $name,$flag,$desc,$content,$img,$unis,$sort,$status,$id);
    $st->execute();
    flash('Destination updated!','success');
    redirect(ADMIN_URL.'/pages/destinations.php');
}
?>
<div class="page-header">
    <a href="destinations.php" class="btn btn-secondary btn-sm"><i class="fas fa-arrow-left"></i></a>
    <h1>Edit Destination</h1>
</div>
<div class="card">
    <div class="card-header"><i class="fas fa-edit" style="color:var(--brand)"></i><h2><?=e($d['country_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">Country Name *</label>
                    <input type="text" name="country_name" class="form-control" value="<?=e($d['country_name'])?>" required>
                </div>
                <div class="form-group">
                    <label class="form-label">Flag Emoji</label>
                    <input type="text" name="flag_emoji" class="form-control" value="<?=e($d['flag_emoji'])?>">
                </div>
            </div>
            <div class="form-group">
                <label class="form-label">Short Description</label>
                <textarea name="description" class="form-control" rows="3"><?=e($d['description'])?></textarea>
            </div>
            <div class="form-group">
                <label class="form-label">Full Content</label>
                <textarea name="content" class="form-control" rows="8"><?=e($d['content'])?></textarea>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label">Replace Image</label>
                    <input type="file" name="image" class="form-control" accept="image/*" onchange="previewImg(this)">
                    <?php if($d['image']): ?><img src="<?=e(SITE_URL.'/'.$d['image'])?>" 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 Image URL</label>
                    <input type="text" name="image_url" class="form-control" value="<?=e($d['image'])?>">
                </div>
            </div>
            <div class="form-row-3">
                <div class="form-group">
                    <label class="form-label">Universities Count</label>
                    <input type="number" name="universities_count" class="form-control" value="<?=(int)$d['universities_count']?>">
                </div>
                <div class="form-group">
                    <label class="form-label">Status</label>
                    <select name="status" class="form-select">
                        <option value="active" <?=$d['status']==='active'?'selected':''?>>Active</option>
                        <option value="inactive" <?=$d['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)$d['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="destinations.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'; ?>
