<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="Styles/styles.css">
</head>
<body>
    <?php
        session_name
('cms');
        
session_start();

            
// echo "<pre>";
            // print_r($_SESSION);
            // echo "</pre>";
        
            //checks for wrong inputs missing

        
$LINK mysqli_connect('127.0.0.1','pisjo950','vjPRjFTxDVIoG7)t','pisjo950');

        
//get all the films
        
$statement $LINK->prepare("SELECT * FROM CineFilms");
        
$statement->execute();
        
$result $statement->get_result();

        
//get all the extratimes
        
$statementExtra $LINK->prepare("SELECT * FROM CineExtra");
        
$statementExtra->execute();
        
$resultExtra $statementExtra->get_result();


        
$date = isset($_GET['date']) ? $_GET['date'] : ''// Get date safely
    
?>

    <form id="form" method="POST">

             <select name="film">
                <option>--- Select film ---</option>
                <?php
                    
while($row=mysqli_fetch_assoc($result))
                    {
                        echo 
'<option value="' htmlspecialchars($row['pk_film']) . '">' htmlspecialchars($row['title']) . '</option>';
                        
$filmTitle=$row['title'];
                    }
                    
var_dump($filmTitle);

                
?>
            </select>
            <br>
            <br>
        <label for="date">Date: </label>
        <input type="date" name="date" value="<?php echo htmlspecialchars($date); ?>">
        <br>
        <!-- startTime, endTime missing for now -->
        <label for="fromLabel">From:</label>
        <input type="time" name="fromLabel">
        <br>
        <label for="toLabel">To:</label>
        <input type="time" name="toLabel">
        <br>
        <br>
            <select name="extra">
                <option>--- Select extra time ---</option>
                <?php
                    
while($row=mysqli_fetch_assoc($resultExtra))
                    {
                        echo 
'<option value="' htmlspecialchars($row['pk_extra']) . '" data-time="' htmlspecialchars($row['defaultTime']) . '">' htmlspecialchars($row['name']) . '</option>';
                        
$pkExtraTime=$row['pk_extra'];
                        
$extraLabel=$row['defaultTime'];
                    }

                    
                
?>
            </select> 
        <br>
        <label for="title">Notes: </label>
        <input type="text" name="notes">
        <br>
        <br>  
        <button type="submit" name="submit">Submit</button>
    </form>

    <form action="index.php">
        <button type="submit">Back</button>
    </form>

    <?php

        
if(isset($_POST['submit']))
        {
            
$film $_POST['film'];
            
$from $_POST['fromLabel'];
            
$to $_POST['toLabel'];
            
$user=$_SESSION['username'];

           
            
            
//get foreign primarykey for the user
            
$statement1 $LINK->prepare("SELECT pk_person FROM CinePerson WHERE email = ?");
            
$statement1->bind_param('s'$user);
            
$statement1->execute();
            
$result1 $statement1->get_result();
            
$row1 $result1->fetch_assoc();
            
//$pkPerson = $row1['pk_person'];//retrieve the id for the film
            
$pkPerson=null;

            
//insert into CineEntry
            
$statement $LINK->prepare("INSERT INTO CinePlages(date, fromTime, toTime, extraTime, fk_extra, fk_film, fk_respo) VALUES (?, ?, ?,?,?,?,?)");
            
$statement->bind_param('ssssiii'$date$from$to$extraLabel$pkExtraTime$film$pkPerson);
            
$statement->execute();
            
$statement->close();


            echo 
"New Register created successfully";
            
header("Location: index.php"); //put this onto the index page instead of loading a new page
        
}
                    
    
?>

</body>
</html>