<?php require "../includes/header.php"; ?>
<?php require "../../config/config.php"; ?>

<?php
if(!isset($_SESSION['admin_username'])){ header("location: ../login.php"); exit; }
if(!isset($_GET['id'])){ header("location: index.php"); exit; }

$id     = (int)$_GET['id'];
$hotel  = $conn->prepare("SELECT * FROM hotels WHERE id = :id");
$hotel->execute([':id' => $id]);
$hotel  = $hotel->fetch(PDO::FETCH_OBJ);
if(!$hotel){ header("location: index.php"); exit; }

$error = '';
if(isset($_POST['submit'])){
    $name        = $_POST['name'];
    $location    = $_POST['location'];
    $description = $_POST['description'];
    $status      = isset($_POST['status']) ? 1 : 0;
    $image_name  = $hotel->image;

    if(isset($_FILES['image']) && $_FILES['image']['error'] === 0){
        $allowed = ['jpg','jpeg','png','webp'];
        $ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
        if(!in_array($ext, $allowed)){
            $error = 'Invalid image format.';
        } else {
            $image_name = 'hotel_' . time() . '.' . $ext;
            move_uploaded_file($_FILES['image']['tmp_name'], '../../images/' . $image_name);
        }
    }

    if(!$error){
        $upd = $conn->prepare("UPDATE hotels SET name=:name, location=:location, description=:description, image=:image, status=:status WHERE id=:id");
        $upd->execute([':name'=>$name,':location'=>$location,':description'=>$description,':image'=>$image_name,':status'=>$status,':id'=>$id]);
        header("location: index.php?success=Hotel updated successfully");
        exit;
    }
}
?>

<div class="topbar">
    <h4><i class="fa fa-edit mr-2"></i> Edit Hotel</h4>
    <div class="admin-badge">
        <div class="avatar"><?php echo strtoupper(substr($_SESSION['admin_username'],0,1)); ?></div>
        <span><?php echo htmlspecialchars($_SESSION['admin_username']); ?></span>
        <a href="../logout.php"><i class="fa fa-sign-out"></i> Logout</a>
    </div>
</div>

<div class="main-content">
    <div class="row justify-content-center">
        <div class="col-md-7">
            <div class="admin-form-card">
                <h5 class="mb-4" style="color:#1a2d5a;font-weight:700;">Edit Hotel: <?php echo htmlspecialchars($hotel->name); ?></h5>
                <?php if($error): ?><div class="alert alert-danger"><?php echo $error; ?></div><?php endif; ?>

                <?php if($hotel->image): ?>
                <div class="mb-3">
                    <img src="<?php echo APPURL; ?>/images/<?php echo htmlspecialchars($hotel->image); ?>" style="height:120px;border-radius:8px;object-fit:cover;">
                </div>
                <?php endif; ?>

                <form method="POST" action="update.php?id=<?php echo $id; ?>" enctype="multipart/form-data">
                    <div class="form-group">
                        <label>Hotel Name</label>
                        <input type="text" name="name" class="form-control" value="<?php echo htmlspecialchars($hotel->name); ?>" required>
                    </div>
                    <div class="form-group">
                        <label>Location</label>
                        <input type="text" name="location" class="form-control" value="<?php echo htmlspecialchars($hotel->location); ?>" required>
                    </div>
                    <div class="form-group">
                        <label>Description</label>
                        <textarea name="description" class="form-control" rows="4"><?php echo htmlspecialchars($hotel->description); ?></textarea>
                    </div>
                    <div class="form-group">
                        <label>Replace Image (optional)</label>
                        <input type="file" name="image" class="form-control" accept="image/*">
                    </div>
                    <div class="form-group">
                        <div class="custom-control custom-switch">
                            <input type="checkbox" class="custom-control-input" id="statusSwitch" name="status" <?php echo $hotel->status ? 'checked' : ''; ?>>
                            <label class="custom-control-label" for="statusSwitch">Active</label>
                        </div>
                    </div>
                    <button type="submit" name="submit" class="btn btn-admin-primary"><i class="fa fa-save mr-1"></i> Update Hotel</button>
                    <a href="index.php" class="btn btn-secondary ml-2">Cancel</a>
                </form>
            </div>
        </div>
    </div>
</div>

<?php require "../includes/footer.php"; ?>
