<?php
$pageTitle = 'Edit Place';
require_once 'includes/header.php';

$id = intval($_GET['id'] ?? 0);
$stmt = $db->prepare("SELECT * FROM places WHERE id = ?");
$stmt->execute([$id]);
$place = $stmt->fetch();

if (!$place) {
    setFlash('danger', 'Place not found.');
    redirect(ADMIN_URL . 'places.php');
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = sanitize($_POST['name'] ?? '');
    $description = sanitize($_POST['description'] ?? '');
    $isFeatured = isset($_POST['is_featured']) ? 1 : 0;
    $status = sanitize($_POST['status'] ?? 'active');
    $slug = createSlug($name);
    
    $imageName = $place['image'];
    if (!empty($_FILES['image']['name'])) {
        $upload = uploadFile($_FILES['image'], 'places');
        if ($upload['success']) {
            $imageName = $upload['filename'];
        }
    }
    
    $stmt = $db->prepare("UPDATE places SET name = ?, slug = ?, description = ?, image = ?, is_featured = ?, status = ? WHERE id = ?");
    $stmt->execute([$name, $slug, $description, $imageName, $isFeatured, $status, $id]);
    
    logActivity($_SESSION['user_id'], 'place_updated', "Updated place: $name");
    setFlash('success', 'Place updated successfully!');
    redirect(ADMIN_URL . 'places.php');
}
?>

<div class="page-header">
    <h1><i class="fas fa-edit text-primary"></i> Edit Place</h1>
    <a href="<?= ADMIN_URL ?>places.php" class="btn btn-outline"><i class="fas fa-arrow-left"></i> Back to Places</a>
</div>

<div class="admin-card">
    <div class="card-body">
        <form method="POST" enctype="multipart/form-data" class="admin-form">
            <div class="form-group">
                <label>Place Name <span class="required">*</span></label>
                <input type="text" name="name" class="form-control" value="<?= sanitize($place['name']) ?>" required>
            </div>
            
            <div class="form-group">
                <label>Description</label>
                <textarea name="description" class="form-control" rows="4"><?= sanitize($place['description']) ?></textarea>
            </div>
            
            <div class="form-group">
                <label>Image</label>
                <input type="file" name="image" class="form-control" accept="image/*" data-preview="image-preview">
                <div class="image-preview" id="image-preview">
                    <img src="<?= getImageUrl($place['image'], 'places') ?>" alt="Current Image">
                </div>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label>Status</label>
                    <select name="status" class="form-control">
                        <option value="active" <?= $place['status'] === 'active' ? 'selected' : '' ?>>Active</option>
                        <option value="inactive" <?= $place['status'] === 'inactive' ? 'selected' : '' ?>>Inactive</option>
                    </select>
                </div>
                <div class="form-group">
                    <label>&nbsp;</label>
                    <div class="form-check" style="padding-top:10px;">
                        <input type="checkbox" name="is_featured" id="is_featured" value="1" <?= $place['is_featured'] ? 'checked' : '' ?>>
                        <label for="is_featured">Featured Place</label>
                    </div>
                </div>
            </div>
            
            <div style="display:flex;gap:10px;margin-top:20px;">
                <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Update Place</button>
                <a href="<?= ADMIN_URL ?>places.php" class="btn btn-outline">Cancel</a>
            </div>
        </form>
    </div>
</div>

<?php require_once 'includes/footer.php'; ?>
