<?php 
    header
('Content-Type: application/json');
    require_once 
'../DB/db_credentials.php';
    require_once 
'../DB/db_connection.php';
    
// global $dbc; // needs to be set globally so $dbc is accessible in this file too

    
if ($_SERVER["REQUEST_METHOD"] === "POST") {
        
$scheduleID $_POST["scheduleID"];
        
$filmID $_POST["filmID"];
        
$start $_POST["start"];    
        
$end $_POST["end"];   
    
        if (empty(
$scheduleID) || empty($start) || empty($end)) {
            echo 
json_encode(["status" => "error""message" => "Missing data"]);
            exit;
        }
    
        
// get title from the film table using the film ID
        
$query "SELECT title FROM citeLeParis_film WHERE pk_filmID = ?";
        
$stmt mysqli_prepare($dbc$query);
        
mysqli_stmt_bind_param($stmt"i"$filmID);
        
mysqli_stmt_execute($stmt);
        
mysqli_stmt_bind_result($stmt$filmTitle);
        
mysqli_stmt_fetch($stmt);
        
mysqli_stmt_close($stmt);
    
        if (!
$filmTitle) {
            echo 
json_encode(["status" => "error""message" => "Film not found"]);
            exit;
        }
    
        
// update event in schedule table
        
$query "UPDATE citeLeParis_schedule SET fk_filmID = ?, startTime = ?, endTime = ? WHERE pk_scheduleID = ?";
        
$stmt mysqli_prepare($dbc$query);
        
mysqli_stmt_bind_param($stmt"issi"$filmID$start$end$scheduleID);
        
$result mysqli_stmt_execute($stmt);

        
// check if update was successful
        
if ($result) {
            
$affectedRows mysqli_affected_rows($dbc);

            if (
$affectedRows 0) {
                echo 
json_encode([
                    
"status" => "success"
                    
"filmTitle" => $filmTitle
                    
"start" => $start
                    
"end" => $end,
                    
"scheduleID" => $scheduleID 
                
]);
            } else {
                
// no rows were updated, schedule ID was not found
                
echo json_encode([
                    
"status" => "error"
                    
"message" => "No rows updated, scheduleID doesn't exist"
                    
"scheduleID" => $scheduleID
                
]);
            }
        } else {
            echo 
json_encode([
                
"status" => "error"
                
"message" => "Failed to execute update",
                
"scheduleID" => $scheduleID
            
]);
        }

        
mysqli_stmt_close($stmt);
    }
?>