<?php
// Initialize message variables
$msg '';
$msgType 'success';

// Handle event deletion
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'delete_event') {
    if (isset(
$_POST['event_id']) && is_numeric($_POST['event_id'])) {
        
$eventId intval($_POST['event_id']);
        
        
// Check if there are any registrations for this plage
        
$checkStmt $dbc->prepare("SELECT COUNT(*) as count FROM CINE_Registers WHERE fk_Plage = ?");
        
$checkStmt->bind_param('i'$eventId);
        
$checkStmt->execute();
        
$checkResult $checkStmt->get_result();
        
$registrationCount $checkResult->fetch_assoc()['count'];
        
$checkStmt->close();
        
        if (
$registrationCount 0) {
            
$msg 'Cannot delete event: There are ' $registrationCount ' registrations associated with this event.';
            
$msgType 'error';
        } else {
            
// Delete the event
            
$deleteStmt $dbc->prepare("DELETE FROM CINE_Plage WHERE pk_Plage = ?");
            
$deleteStmt->bind_param('i'$eventId);
            
            if (
$deleteStmt->execute()) {
                
$msg 'Event deleted successfully!';
                
$msgType 'success';
            } else {
                
$msg 'Error deleting event: ' $deleteStmt->error;
                
$msgType 'error';
            }
            
$deleteStmt->close();
        }
    } else {
        
$msg 'Invalid event ID.';
        
$msgType 'error';
    }
    
    
// Don't process other POST data if we're deleting
    
if (isset($_POST['date']) && isset($_POST['start_time']) && isset($_POST['end_time'])) {
        
// This is a regular form submission, not just a delete action
    
} else {
        
// This is just a delete action, so we've already processed it
        
$deleteOnly true;
    }
}

// Handle movie assignment to plage
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'assign_movie') {
    if (isset(
$_POST['plage_id']) && is_numeric($_POST['plage_id']) && isset($_POST['movie_id'])) {
        
$plageId intval($_POST['plage_id']);
        
$movieId $_POST['movie_id'] === '' null intval($_POST['movie_id']);
        
        
$updateStmt $dbc->prepare("UPDATE CINE_Plage SET fk_Film = ? WHERE pk_Plage = ?");
        
$updateStmt->bind_param('ii'$movieId$plageId);
        
        if (
$updateStmt->execute()) {
            
$msg 'Movie assigned successfully!';
            
$msgType 'success';
        } else {
            
$msg 'Error assigning movie: ' $updateStmt->error;
            
$msgType 'error';
        }
        
$updateStmt->close();
    } else {
        
$msg 'Invalid plage or movie ID.';
        
$msgType 'error';
    }
}

// Handle entry creation - only if not a delete-only request AND not a movie assignment
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !isset($deleteOnly) && (!isset($_POST['action']) || $_POST['action'] !== 'assign_movie')) {
    
// Only process entry creation if we have the required fields
    
if (isset($_POST['date']) && isset($_POST['start_time']) && isset($_POST['end_time'])) {
        
$date      $_POST['date'];
        
$start     $_POST['start_time'];
        
$end       $_POST['end_time'];
        
$extraTime = isset($_POST['extra_time']) ? intval($_POST['extra_time']) : 0;
        
$extraId   = isset($_POST['extra_id']) && is_numeric($_POST['extra_id']) ? intval($_POST['extra_id']) : null;

        
$stmt $dbc->prepare(
            
"INSERT INTO CINE_Plage (date, fromTime, toTime, extraTime, fk_Film, fk_Person_supervises, fk_Extra)
             VALUES (?, ?, ?, ?, NULL, NULL, ?)"
        
);
        
$stmt->bind_param('sssii'$date$start$end$extraTime$extraId);
        if (
$stmt->execute()) {
            
$msg 'Entry saved successfully!';
            
$msgType 'success';
        } else {
            
$msg 'Error: ' $stmt->error;
            
$msgType 'error';
        }
        
$stmt->close();
    } else if (
$_SERVER['REQUEST_METHOD'] === 'POST') {
        
// If we're here, it's a POST request without the required fields and not a delete or assign action
        
$msg 'Missing required fields for creating a new entry.';
        
$msgType 'error';
    }
}
?>