<?php
session_start();
include('../config/dbcon.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $userID = $_SESSION['userID'];

    // Extract leave days data
    $lentryDate = $_POST['lentryDate'];
    $leaveDays = $_POST['leaveDays'];

    // Hard-coded values for leave day entry
    $clientID = 27;
    $projectID = 995;
    $milestoneID = 1664;
    $hrs = 0;
    $descp = "LEAVE";

    // Convert the date using strtotime
    $convertedDate = date('Y-m-d', strtotime($lentryDate));

    // Construct the SQL query with values directly
    $insertLeaveDaysQuery = "INSERT INTO timesheet (userID, clientID, projectID, milestoneID, tentryDate, hrs, descp, leaveDays) 
                            VALUES ($userID, $clientID, $projectID, $milestoneID, '$convertedDate', $hrs, '$descp', $leaveDays)";

    // Execute the SQL query
    $result = mysqli_query($conn, $insertLeaveDaysQuery);

    if (!$result) {
        // Error occurred, log the error and display a user-friendly message
        error_log("Error executing query: " . mysqli_error($conn));
        exit("An error occurred. Please try again later.");
    }

    // Close the database connection
    mysqli_close($conn);

    // Redirect to the timesheet page
    header('Location: timesheet.php');
    exit(); // Ensure no further code is executed
}
?>