<?php
    
/* session_start(); */
    
require_once("Functions/Error/errorHandler.php");

    
$userID $_SESSION['userID'];

    if (
$_SERVER["REQUEST_METHOD"] == "POST") {
        
$fk_responsible $_POST['fk_responsible'];
        
$date $_POST['date']; 
        
$fromTime $_POST['fromTime']; 
        
$toTime $_POST['toTime']; 
        
$extraTime $_POST['extraTime']; 
        
$fk_film $_POST['fk_film']; 
        
$fk_extra $_POST['fk_extra'] ?? null;

        
$fk_responsible $fk_responsible === '' null $fk_responsible;
        
$fk_film $fk_film === '' null $fk_film;
        
$fk_extra $fk_extra === '' null $fk_extra;
        
$extraTime $extraTime === '' null $extraTime;

        if (empty(
$date) || empty($fromTime) || empty($toTime)) {
            
setError("Please fill in the date, from time, and to time.");
        } else {
            
$query "INSERT INTO citeLeParis_shift (fk_responsible, date, fromTime, toTime, extraTime, fk_film, fk_extra)
                    VALUES (?, ?, ?, ?, ?, ?, ?)"
;

            
$stmt mysqli_prepare($dbc$query);
            if (
$stmt) {
                
mysqli_stmt_bind_param($stmt"issssss"$fk_responsible$date$fromTime$toTime$extraTime$fk_film$fk_extra);
                if (!
mysqli_stmt_execute($stmt)) {
                    
setError("Database error: " mysqli_error($dbc));
                }
                
mysqli_stmt_close($stmt);
            } else {
                
setError("Unable to prepare the statement.");
            }
        }
    }

    include(
"Functions/Error/errorDisplay.php");
?>

<style>main{padding:0 !important;}</style>

<section id="shift-form-container">
    <h2>Add Shift</h2>
    <form method="post" action="" class="shift-form">
        <label for="fk_responsible">Responsible Employee:</label>
        <select name="fk_responsible" id="fk_responsible">
            <option value="">Select Employee</option>
            <?php
                $employeeQuery 
"SELECT pk_userID, firstName, lastName FROM citeLeParis_user WHERE isActive = 1";
                
$employeeResult mysqli_query($dbc$employeeQuery);
                
                while (
$employee mysqli_fetch_assoc($employeeResult)) {
                    
$fullName htmlspecialchars($employee['firstName'] . " " $employee['lastName']);
                    echo 
"<option value='" $employee['pk_userID'] . "'>$fullName</option>";
                }
            
?>
        </select>

        <label for="date">Shift Date:</label>
        <input type="date" name="date" id="date" required>

        <label for="fromTime">From Time:</label>
        <input type="time" name="fromTime" id="fromTime" required>

        <label for="toTime">To Time:</label>
        <input type="time" name="toTime" id="toTime" required>

        <label for="extraTime">Extra Time:</label>
        <input type="time" name="extraTime" id="extraTime">

        <label for="fk_film">Film:</label>
        <select name="fk_film" id="fk_film">
            <option value="">Select Film</option>
            <?php
                $filmQuery 
"SELECT pk_filmID, title FROM citeLeParis_film";
                
$filmResult mysqli_query($dbc$filmQuery);

                while (
$film mysqli_fetch_assoc($filmResult)) {
                    echo 
"<option value='" $film['pk_filmID'] . "'>" htmlspecialchars($film['title']) . "</option>";
                }
            
?>
        </select>

        <label for="fk_extra">Extra (Optional):</label>
        <select name="fk_extra" id="fk_extra">
            <option value="">Select Extra</option>
            <?php
                $extraQuery 
"SELECT pk_extra, name FROM citeLeParis_extra";
                
$extraResult mysqli_query($dbc$extraQuery);

                while (
$extra mysqli_fetch_assoc($extraResult)) {
                    echo 
"<option value='" $extra['pk_extra'] . "'>" htmlspecialchars($extra['name']) . "</option>";
                }
            
?>
        </select>

        <input type="submit" value="Add Shift">
    </form>
</section>