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

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

$success = $error = '';
if(isset($_POST['submit'])){
    if(empty($_POST['name']) || empty($_POST['location']) || empty($_POST['description'])){
        $error = 'Name, location and description are required.';
    } else {
        $name        = $_POST['name'];
        $location    = $_POST['location'];
        $description = $_POST['description'];
        $status      = isset($_POST['status']) ? 1 : 0;
        $image_name  = '';

        // Handle image upload
        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 = 'Only JPG, PNG, WEBP images are allowed.';
            } else {
                $image_name = 'hotel_' . time() . '.' . $ext;
                $upload_dir = '../../images/';
                move_uploaded_file($_FILES['image']['tmp_name'], $upload_dir . $image_name);
            }
        }

        if(!$error){
            $insert = $conn->prepare("INSERT INTO hotels (name, location, description, image, status) VALUES (:name, :location, :description, :image, :status)");
            $insert->execute([':name' => $name, ':location' => $location, ':description' => $description, ':image' => $image_name, ':status' => $status]);
            header("location: index.php?success=Hotel created successfully");
            exit;
        }
    }
}
?>

<div class="topbar">
    <h4><i class="fa fa-plus mr-2"></i> Add 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;">Add New Hotel</h5>
                <?php if($error): ?><div class="alert alert-danger"><?php echo $error; ?></div><?php endif; ?>

                <form method="POST" action="create.php" enctype="multipart/form-data">
                    <div class="form-group">
                        <label>Hotel Name</label>
                        <input type="text" name="name" class="form-control" placeholder="e.g. Grand Palace Hotel" required>
                    </div>
                    <div class="form-group">
                        <label>Location</label>
                        <input type="text" name="location" class="form-control" placeholder="e.g. Lagos, Nigeria" required>
                    </div>
                    <div class="form-group">
                        <label>Description</label>
                        <textarea name="description" class="form-control" rows="4" placeholder="Describe the hotel..." required></textarea>
                    </div>
                    <div class="form-group">
                        <label>Hotel Image</label>
                        <input type="file" name="image" class="form-control" accept="image/*">
                        <small class="text-muted">JPG, PNG or WEBP. Will be stored in /images/</small>
                    </div>
                    <div class="form-group">
                        <div class="custom-control custom-switch">
                            <input type="checkbox" class="custom-control-input" id="statusSwitch" name="status" checked>
                            <label class="custom-control-label" for="statusSwitch">Active (visible to users)</label>
                        </div>
                    </div>
                    <button type="submit" name="submit" class="btn btn-admin-primary">
                        <i class="fa fa-save mr-1"></i> Create Hotel
                    </button>
                    <a href="index.php" class="btn btn-secondary ml-2">Cancel</a>
                </form>
            </div>
        </div>
    </div>
</div>

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