<?php include 'includes/header.php'; ?>
<?php include 'includes/sidebar.php'; ?>
<?php require 'includes/db_connect.php'; ?>
<?php
date_default_timezone_set('Africa/Lagos');
$currentDate = date('Y-m-d');
$currentTime = date('H:i:s');

// Handle individual attendance marking
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['mark_individual'])) {
    $staff_id = (int)$_POST['staff_id'];
    $status = $_POST['status'];

    // Check if attendance already marked
    $check = $conn->prepare("SELECT * FROM attendance WHERE staff_id = ? AND attendance_date = ?");
    $check->bind_param("is", $staff_id, $currentDate);
    $check->execute();
    $result = $check->get_result();

    if ($result->num_rows == 0) {
        // Insert attendance with time
        $stmt = $conn->prepare("INSERT INTO attendance (staff_id, attendance_date, attendance_time, status) VALUES (?, ?, ?, ?)");
        $stmt->bind_param("isss", $staff_id, $currentDate, $currentTime, $status);
        $stmt->execute();

        // Fetch staff info
        $emailQuery = $conn->prepare("SELECT fullname, email FROM staff WHERE id = ?");
        $emailQuery->bind_param("i", $staff_id);
        $emailQuery->execute();
        $staffInfo = $emailQuery->get_result()->fetch_assoc();

        // Send admin notification email
        $adminEmail = "admin@example.com"; // Update this
        $subject = "Attendance Marked: {$staffInfo['fullname']}";
        $message = "Attendance for {$staffInfo['fullname']} was marked as {$status} on {$currentDate} at {$currentTime}.";
        $headers = "From: noreply@yourdomain.com";

        mail($adminEmail, $subject, $message, $headers);

        echo "<div class='alert alert-success'>Attendance marked for {$staffInfo['fullname']}.</div>";
    } else {
        echo "<div class='alert alert-warning'>Attendance already marked for this staff today.</div>";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Individual Attendance Management</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
    <div class="container py-5">
        <h2 class="mb-4">Staff Attendance - <?= $currentDate ?></h2>

        <table class="table table-bordered bg-white">
            <thead class="table-dark">
                <tr>
                    <th>Staff Name</th>
                    <th>Department</th>
                    <th>Status</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $staffs = $conn->query("SELECT * FROM staff ORDER BY fullname");
                while ($staff = $staffs->fetch_assoc()) {
                    $staff_id = $staff['id'];

                    // Check if attendance already marked
                    $check = $conn->prepare("SELECT * FROM attendance WHERE staff_id = ? AND attendance_date = ?");
                    $check->bind_param("is", $staff_id, $currentDate);
                    $check->execute();
                    $result = $check->get_result();

                    echo "<tr>";
                    echo "<td>{$staff['fullname']}</td>";
                    echo "<td>{$staff['department']}</td>";
                    if ($result->num_rows > 0) {
                        echo "<td colspan='2'><span class='text-success'>Already Marked</span></td>";
                    } else {
                        echo "<form method='POST'>";
                        echo "<td>
                                <select name='status' class='form-select'>
                                    <option value='Absent'>Absent</option>
                                    <option value='Present'>Present</option>
                                    
                                </select>
                              </td>";
                        echo "<td>
                                <input type='hidden' name='staff_id' value='{$staff_id}'>
                                <button type='submit' name='mark_individual' class='btn btn-primary btn-sm'>Mark</button>
                              </td>";
                        echo "</form>";
                    }
                    echo "</tr>";
                }
                ?>
            </tbody>
        </table>
    </div>
</body>
</html>
