<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
session_write_close();
require 'config.php';

if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit;
}

$username = $_SESSION['username'];
$role = $_SESSION['role'];

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

// ---------------------------
// LOAD SESSIONS (SHIFT BASED)
// ---------------------------
$sql = "
SELECT 
    a.id,
    a.employee_id,
    a.check_in,
    a.check_out,
    a.shift_start,
    a.shift_end,
    e.name,
    s.name AS shift_name,
    s.checkin AS expected_in,
    s.checkout AS expected_out,
    s.crosses_midnight
FROM attendance_sessions a
JOIN employees e ON e.id = a.employee_id
LEFT JOIN shifts s ON s.id = a.shift_id
WHERE DATE(a.shift_start) = ?
";

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

$sql .= " ORDER BY a.shift_start DESC";

$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;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Attendance Sessions</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>

<body class="flex h-screen">

<?php include 'sidebar.php'; ?>

<div class="flex-1 p-6 bg-gray-100 overflow-y-auto" id="main-content">

<h1 class="text-2xl font-bold mb-6">Shift-Based Attendance Sessions</h1>

<!-- FILTER -->
<form method="get" class="flex gap-4 mb-4 flex-wrap">

    <input type="date" name="date"
        value="<?php echo $date; ?>"
        class="border rounded-lg p-2">

    <input type="text" name="search"
        placeholder="Search Employee..."
        value="<?php echo htmlspecialchars($search); ?>"
        class="border rounded-lg p-2 flex-1">

    <button class="bg-blue-600 text-white px-6 rounded-lg">
        Filter
    </button>

</form>

<!-- TABLE -->
<div class="overflow-x-auto">
<table class="w-full bg-white shadow rounded-lg">

<thead>
<tr class="bg-blue-600 text-white">
    <th class="p-3 text-left">Employee</th>
    <th class="p-3 text-center">Check In</th>
    <th class="p-3 text-center">Check Out</th>
    <th class="p-3 text-center">Shift</th>
    <th class="p-3 text-center">Status</th>
</tr>
</thead>

<tbody>

<?php if (empty($sessions)): ?>
<tr>
    <td colspan="5" class="text-center p-6 text-gray-500">
        No sessions found for this date.
    </td>
</tr>
<?php endif; ?>

<?php foreach ($sessions as $row):

    $status = "Absent";
    $color = "text-red-600";

    $checkIn = $row['check_in'];
    $checkOut = $row['check_out'];

    $expectedIn = $row['expected_in'];
    $expectedOut = $row['expected_out'];
    $cross = $row['crosses_midnight'];

    // ---------------------------
    // STATUS LOGIC (SHIFT BASED)
    // ---------------------------
    if ($checkIn) {

        if ($checkIn > $row['shift_start']) {
            $status = "Late";
            $color = "text-yellow-600";
        } else {
            $status = "On Time";
            $color = "text-green-600";
        }

        if ($checkOut) {

            if ($checkOut < $row['shift_end']) {
                $status = "Left Early";
                $color = "text-orange-600";
            }
        }
    }
?>

<tr class="border-b hover:bg-gray-50">

    <td class="p-3">
        <?php echo htmlspecialchars($row['name']); ?>
    </td>

    <td class="p-3 text-center">
        <?php echo $checkIn ?? "-"; ?>
    </td>

    <td class="p-3 text-center">
        <?php echo $checkOut ?? "-"; ?>
    </td>

    <td class="p-3 text-center">
        <span class="inline-block w-3 h-3 rounded-full mr-2 bg-blue-500"></span>
        <?php echo htmlspecialchars($row['shift_name'] ?? 'Shift'); ?>
        <div class="text-xs text-gray-500">
            <?php echo $row['shift_start'] . " → " . $row['shift_end']; ?>
        </div>
    </td>

    <td class="p-3 text-center font-semibold <?php echo $color; ?>">
        <?php echo $status; ?>
    </td>

</tr>

<?php endforeach; ?>

</tbody>
</table>
</div>

</div>

</body>
</html>