
<?php
ob_start();
include("connection.php");

// Add new room
if(isset($_POST['add_room'])) {
    $room_name = mysqli_real_escape_string($conn, $_POST['room_name']);
    
    // Handle file upload - check if file is uploaded
    $fileName = '';
    if(isset($_FILES["image"]) && $_FILES["image"]["error"] == 0 && !empty($_FILES["image"]["name"])) {
        $targetDir = "room_images/";
        $fileName = time() . "_" . basename($_FILES["image"]["name"]);
        $targetFile = $targetDir . $fileName;
        $imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
        $allowedTypes = ['jpg','jpeg','png','gif'];
        
        if(in_array($imageFileType,$allowedTypes)){
            if(!move_uploaded_file($_FILES["image"]["tmp_name"], $targetFile)){
                $fileName = ''; // Reset if upload fails
            }
        } else {
            $fileName = ''; // Reset if file type not allowed
        }
    }
    
    $created_at = date('Y-m-d H:i:s');

    // Insert room
    $sql = mysqli_query($conn, "
        INSERT INTO `rooms`(`room_name`, `image`, `created_at`)
        VALUES ('$room_name','$fileName','$created_at')
    ");

    if($sql){
        echo "Room added successfully!";
    } else {
        echo "Error: " . mysqli_error($conn);
    }
}

// Fetch room data for edit
if(isset($_POST['fetch_room'])) {
    $room_id = mysqli_real_escape_string($conn, $_POST['room_id']);
    $q = mysqli_query($conn,"
        SELECT * FROM rooms 
        WHERE id='$room_id'
        ORDER BY id ASC limit 1
    ");
    $data = mysqli_fetch_assoc($q);
    
    echo json_encode([
        "id"            => $data['id'],
        "room_name"     => $data['room_name'],
        "image"         => $data['image']
    ]);
    exit;
}

// Edit room details
if(isset($_POST['edit_room'])) {
    $room_id = mysqli_real_escape_string($conn, $_POST['id']);
    $room_name = mysqli_real_escape_string($conn, $_POST['room_name']);
    
    // Handle file upload - check if file is uploaded
    $fileName = '';
    if(isset($_FILES["image"]) && $_FILES["image"]["error"] == 0 && !empty($_FILES["image"]["name"])) {
        $targetDir = "room_images/";
        $fileName = time() . "_" . basename($_FILES["image"]["name"]);
        $targetFile = $targetDir . $fileName;
        $imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
        $allowedTypes = ['jpg','jpeg','png','gif'];
        
        if(in_array($imageFileType,$allowedTypes)){
            if(!move_uploaded_file($_FILES["image"]["tmp_name"], $targetFile)){
                $fileName = ''; // Reset if upload fails
            }
        } else {
            $fileName = ''; // Reset if file type not allowed
        }
    }

    // Update room
    $sql = mysqli_query($conn, "
        UPDATE `rooms` SET 
        `room_name`='$room_name'
        WHERE `id`='$room_id'
    ");
    
    if($fileName!='') {
        $sql = mysqli_query($conn, "
            UPDATE `rooms` SET `image`='$fileName' WHERE `id`='$room_id'
        ");
    }

    if ($sql) {
        if (mysqli_affected_rows($conn) > 0) {
            echo "Room updated successfully!";
        } else {
            echo "No changes made to room data.";
        }
    } else {
        echo "Error updating room: " . mysqli_error($conn);
    }
}

// Delete room
if(isset($_POST['delete_room'])) {
    $room_id = mysqli_real_escape_string($conn, $_POST['room_id']);
    $q = mysqli_query($conn,"
        DELETE FROM `rooms` WHERE `id`='$room_id'
    ");
    if($q) {
        echo "Room deleted successfully!";
    } else {
        echo "Error deleting room: " . mysqli_error($conn);
    }
    exit;
}
?>
