<?php

require_once "db.php";
date_default_timezone_set("Asia/Kuala_Lumpur");

$currentTime = date("H:i:s");
$today = date("Y-m-d");

// configurable absent time
$absentTime = "08:30:00";

if ($currentTime >= $absentTime) {

    // get employees who have NO attendance today
    $q = $conn->query("
        SELECT emp_id FROM employee
        WHERE emp_id NOT IN (
            SELECT emp_id FROM attendance_log WHERE att_date = '$today'
        )
    ");

    if ($q) {
        while ($row = $q->fetch_assoc()) {

            $emp_id = $row['emp_id'];

            // double check (avoid duplicate insert)
            $check = $conn->query("
                SELECT 1 FROM attendance_log 
                WHERE emp_id = '$emp_id' 
                AND att_date = '$today'
            ");

            if ($check && $check->num_rows == 0) {

                $conn->query("
                    INSERT INTO attendance_log 
                    (emp_id, att_date, status, remark)
                    VALUES ('$emp_id', '$today', 'Absent', 'Auto marked absent (8:30 AM)')
                ");
            }
        }
    }
}



$today = date("Y-m-d");



// Total employees

$totalEmployees = 0;

$q = $conn->query("SELECT COUNT(*) AS c FROM employee");

if ($q) {

    $row = $q->fetch_assoc();

    $totalEmployees = (int)$row["c"];

}



// Present today = any row except Absent

$present = 0;

$q = $conn->query("

    SELECT COUNT(*) AS c

    FROM attendance_log

    WHERE att_date = '$today'

      AND status <> 'Absent'

");

if ($q) {

    $row = $q->fetch_assoc();

    $present = (int)$row["c"];

}



// On Time

$onTime = 0;

$q = $conn->query("

    SELECT COUNT(*) AS c

    FROM attendance_log

    WHERE att_date = '$today'

      AND status = 'On Time'

");

if ($q) {

    $row = $q->fetch_assoc();

    $onTime = (int)$row["c"];

}



// Late

$late = 0;

$q = $conn->query("

    SELECT COUNT(*) AS c

    FROM attendance_log

    WHERE att_date = '$today'

      AND status = 'Late'

");

if ($q) {

    $row = $q->fetch_assoc();

    $late = (int)$row["c"];

}



// Manual

$manual = 0;

$q = $conn->query("

    SELECT COUNT(*) AS c

    FROM attendance_log

    WHERE att_date = '$today'

      AND status = 'Manual'

");

if ($q) {

    $row = $q->fetch_assoc();

    $manual = (int)$row["c"];

}



// Absent

$absent = 0;

$q = $conn->query("

    SELECT COUNT(*) AS c

    FROM attendance_log

    WHERE att_date = '$today'

      AND status = 'Absent'

");

if ($q) {

    $row = $q->fetch_assoc();

    $absent = (int)$row["c"];

}



// Not checked out yet

$notCheckedOut = 0;

$q = $conn->query("

    SELECT COUNT(*) AS c

    FROM attendance_log

    WHERE att_date = '$today'

      AND status <> 'Absent'

      AND (check_out_time IS NULL OR check_out_time = '')

");

if ($q) {

    $row = $q->fetch_assoc();

    $notCheckedOut = (int)$row["c"];

}

?>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>HRMS Dashboard</title>

    <style>

        body {

            font-family: Arial, sans-serif;

            background: #f5f7fb;

            margin: 0;

            padding: 24px;

            color: #111827;

        }



        .topbar {

            display: flex;

            justify-content: space-between;

            align-items: center;

            flex-wrap: wrap;

            gap: 12px;

            margin-bottom: 24px;

        }



        h1 {

            margin: 0;

            font-size: 28px;

        }



        .sub {

            color: #6b7280;

            margin-top: 6px;

        }



        .actions {

            display: flex;

            gap: 10px;

            flex-wrap: wrap;

        }



        .btn {

            display: inline-block;

            padding: 10px 16px;

            border-radius: 10px;

            text-decoration: none;

            color: white;

            font-weight: bold;

        }



        .btn-blue {

            background: #2563eb;

        }



        .btn-green {

            background: #16a34a;

        }



        .grid {

            display: grid;

            grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));

            gap: 16px;

            margin-bottom: 28px;

        }



        .card {

            background: white;

            border-radius: 16px;

            padding: 20px;

            box-shadow: 0 2px 12px rgba(0,0,0,0.06);

        }



        .card-title {

            font-size: 14px;

            color: #6b7280;

            margin-bottom: 10px;

        }



        .card-value {

            font-size: 32px;

            font-weight: bold;

            color: #111827;

        }



        .section {

            background: white;

            border-radius: 16px;

            padding: 20px;

            box-shadow: 0 2px 12px rgba(0,0,0,0.06);

        }



        .section h2 {

            margin-top: 0;

            margin-bottom: 14px;

            font-size: 20px;

        }



        .note {

            color: #6b7280;

            font-size: 14px;

        }



        .highlight {

            font-weight: bold;

            color: #111827;

        }

    </style>

</head>

<body>



    <div class="topbar">

        <div>

            <h1>HRMS Dashboard</h1>

            <div class="sub">Attendance summary for <span class="highlight"><?php echo htmlspecialchars($today); ?></span></div>

        </div>



        <div class="actions">

            <a class="btn btn-blue" href="admin_logs.php">View Attendance Logs</a>

            <a class="btn btn-green" href="auto_absent.php" onclick="return confirm('Run automatic absent marking for today?')">Run Absent Check</a>

        </div>

    </div>



    <div class="grid">

        <div class="card">

            <div class="card-title">Total Employees</div>

            <div class="card-value"><?php echo $totalEmployees; ?></div>

        </div>



        <div class="card">

            <div class="card-title">Present Today</div>

            <div class="card-value"><?php echo $present; ?></div>

        </div>



        <div class="card">

            <div class="card-title">On Time</div>

            <div class="card-value"><?php echo $onTime; ?></div>

        </div>



        <div class="card">

            <div class="card-title">Late</div>

            <div class="card-value"><?php echo $late; ?></div>

        </div>



        <div class="card">

            <div class="card-title">Manual</div>

            <div class="card-value"><?php echo $manual; ?></div>

        </div>



        <div class="card">

            <div class="card-title">Absent</div>

            <div class="card-value"><?php echo $absent; ?></div>

        </div>



        <div class="card">

            <div class="card-title">Not Checked Out Yet</div>

            <div class="card-value"><?php echo $notCheckedOut; ?></div>

        </div>

    </div>



    <div class="section">

        <h2>How this works</h2>

        <p class="note">

            <span class="highlight">Present Today</span> counts employees who have an attendance row today and are not marked absent.

        </p>

        <p class="note">

            <span class="highlight">Absent</span> counts employees who were marked by the automatic absent system.

        </p>

        <p class="note">

            <span class="highlight">Not Checked Out Yet</span> counts employees who checked in but still have no checkout time recorded.

        </p>

    </div>



</body>

</html>