<?php
require 'config.php';

$date = $_GET['date'] ?? date('Y-m-d');
$search = $_GET['search'] ?? "";

$sql = "
SELECT 
    ss.*,
    e.name,
    s.name AS shift_name,
    s.crosses_midnight
FROM shift_sessions ss
JOIN employees e ON e.id = ss.employee_id
JOIN shifts s ON s.id = ss.shift_id
WHERE ss.shift_date = ?
";

if ($search) {
    $sql .= " AND e.name LIKE ?";
}

$sql .= " ORDER BY e.name";

$stmt = $conn->prepare($sql);

if ($search) {
    $like = "%$search%";
    $stmt->bind_param("ss", $date, $like);
} else {
    $stmt->bind_param("s", $date);
}

$stmt->execute();
$result = $stmt->get_result();

$sessions = [];

while ($row = $result->fetch_assoc()) {
    $sessions[] = $row;
}

$stmt->close();
$conn->close();
?>