<?php
// header.php ko shaamil karein
require_once 'header.php';

if (!userHasRole(['Admin', 'Manager'])) {
    header("Location: dashboard.php");
    exit();
}

// --- UPDATE ATTENDANCE LOGIC ---
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['update_attendance'])) {
    $user_id = $_POST['user_id'];
    $att_date = $_POST['att_date'];
    $new_status = $_POST['new_status'];
    $admin_id = $_SESSION['user_id'];

    $stmt = $conn->prepare("INSERT INTO attendance (user_id, attendance_date, status, marked_by) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE status = VALUES(status), marked_by = VALUES(marked_by)");
    $stmt->bind_param("issi", $user_id, $att_date, $new_status, $admin_id);
    $stmt->execute();
    $stmt->close();
}

// --- DATA FETCHING ---
$filter_date = $_GET['date'] ?? date('Y-m-d');
$sql = "SELECT u.id as user_id, u.name, a.status 
        FROM users u 
        LEFT JOIN attendance a ON u.id = a.user_id AND a.attendance_date = ?
        ORDER BY u.name ASC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $filter_date);
$stmt->execute();
$result = $stmt->get_result();
$attendance_data = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
?>

<title>Attendance Management - Digify OS</title>
<h2 class="text-xl font-bold text-gray-800">Attendance Management</h2>

<main class="p-6 md:p-10">
    <div class="bg-white p-6 rounded-lg shadow-lg">
        <form method="GET" class="flex items-center gap-4 mb-6">
            <label for="date" class="font-medium">Select Date:</label>
            <input type="date" name="date" value="<?php echo htmlspecialchars($filter_date); ?>" class="border-gray-300 rounded-md shadow-sm">
            <button type="submit" class="px-4 py-2 bg-blue-600 text-white rounded-md">View</button>
        </form>

        <table class="min-w-full divide-y divide-gray-200">
            <thead class="bg-gray-50">
                <tr>
                    <th class="px-4 py-3 text-left font-semibold">Employee</th>
                    <th class="px-4 py-3 text-left font-semibold">Status</th>
                    <th class="px-4 py-3 text-left font-semibold">Mark As</th>
                </tr>
            </thead>
            <tbody class="bg-white divide-y divide-gray-200">
                <?php foreach($attendance_data as $att): ?>
                    <tr>
                        <td class="px-4 py-3 font-medium"><?php echo htmlspecialchars($att['name']); ?></td>
                        <td class="px-4 py-3">
                            <span class="px-2 py-1 text-xs font-semibold rounded-full 
                                <?php 
                                if ($att['status'] == 'Present') echo 'bg-green-100 text-green-800';
                                elseif ($att['status'] == 'CL' || $att['status'] == 'Half Day') echo 'bg-blue-100 text-blue-800';
                                elseif ($att['status'] == 'Absent') echo 'bg-red-100 text-red-800';
                                else echo 'bg-gray-100 text-gray-800';
                                ?>
                            ">
                                <?php echo htmlspecialchars($att['status'] ?? 'Not Marked'); ?>
                            </span>
                        </td>
                        <td class="px-4 py-3">
                            <form method="POST" class="flex gap-2">
                                <input type="hidden" name="user_id" value="<?php echo $att['user_id']; ?>">
                                <input type="hidden" name="att_date" value="<?php echo $filter_date; ?>">
                                <select name="new_status" class="border-gray-300 rounded-md text-sm">
                                    <option value="Present">Present</option>
                                    <option value="Absent">Absent</option>
                                </select>
                                <button type="submit" name="update_attendance" class="px-3 py-1 bg-gray-200 text-xs font-semibold rounded-md">Update</button>
                            </form>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</main>

<?php require_once 'footer.php'; ?>