<?php
session_start();
include 'koneksi.php';

date_default_timezone_set('Asia/Jakarta');

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

// Total karyawan
$totalKaryawanQuery = "SELECT COUNT(*) AS total_karyawan FROM karyawan";
$totalKaryawan = mysqli_fetch_assoc(mysqli_query($conn, $totalKaryawanQuery))['total_karyawan'];

// Karyawan yang sudah absen masuk hari ini (berdasarkan kolom 'tanggal')
$sudahMasukQuery = "SELECT DISTINCT rfid FROM absensi WHERE tanggal = CURDATE() AND jam_masuk IS NOT NULL";
$sudahMasukResult = mysqli_query($conn, $sudahMasukQuery);
$sudahMasukCount = mysqli_num_rows($sudahMasukResult);

// Karyawan yang belum masuk = total - yang sudah masuk
$belumMasuk = $totalKaryawan - $sudahMasukCount;

// Log terakhir presensi masuk hari ini (berdasarkan kolom 'tanggal')
$logQuery = "SELECT k.nama, a.jam_masuk 
             FROM absensi a 
             JOIN karyawan k ON a.rfid = k.rfid 
             WHERE a.tanggal = CURDATE() 
             ORDER BY a.jam_masuk DESC 
             LIMIT 5";
$logResult = mysqli_query($conn, $logQuery);
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Dashboard Absensi RFID</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
    <style>
        body {
            min-height: 100vh;
            display: flex;
            font-family: 'Poppins', sans-serif;
            background-color: #f4f6f9;
        }
        .sidebar {
            width: 250px;
            background-color: #1f2d3d;
            color: white;
            display: flex;
            flex-direction: column;
            padding-top: 20px;
        }
        .sidebar a {
            color: #cfd8dc;
            text-decoration: none;
            display: flex;
            align-items: center;
            padding: 12px 20px;
        }
        .sidebar a i {
            margin-right: 12px;
        }
        .sidebar a:hover, .sidebar a.active {
            background-color: #273746;
            color: #ffffff;
        }
        .content {
            flex-grow: 1;
            padding: 30px;
            width: 100%;
        }
        .card {
            border: none;
            border-radius: 10px;
            box-shadow: 0 3px 10px rgba(0,0,0,0.05);
            margin-bottom: 20px;
        }
        .card h5 {
            font-weight: 600;
        }
    </style>
</head>
<body>
<div class="sidebar">
    <h4 class="text-center text-white mb-4">ABSENSI RFID</h4>
    <a href="index.php" class="active"><i class="bi bi-house-door-fill"></i> Beranda</a>
    <a href="karyawan.php"><i class="bi bi-people-fill"></i> Data Karyawan</a>
    <a href="absensi.php"><i class="bi bi-clock-fill"></i> Absensi</a>
    <a href="rekap.php"><i class="bi bi-card-checklist"></i> Rekap Absensi</a>
    <a href="atur_jam.php"><i class="bi bi-clock-history"></i> Set Jam Masuk / Pulang</a> <!-- Tambahan tab baru -->
    <div class="mt-auto">
        <hr class="bg-secondary">
        <a href="logout.php"><i class="bi bi-box-arrow-right"></i> Logout</a>
    </div>
</div>


    <div class="content">
        <div class="mb-4">
            <h3>Selamat Datang, <?php echo $_SESSION['username']; ?>!</h3>
            <p class="text-muted">Dashboard sistem absensi berbasis RFID</p>
        </div>

        <div class="row g-4 mb-4">
            <div class="col-md-6">
                <div class="card bg-white p-4">
                    <h5>Total Karyawan</h5>
                    <p class="fs-3 fw-semibold text-primary"><?php echo $totalKaryawan; ?></p>
                </div>
            </div>
            <div class="col-md-6">
                <div class="card bg-white p-4">
                    <h5>Belum Presensi Masuk Hari Ini</h5>
                    <p class="fs-3 fw-semibold text-danger"><?php echo $belumMasuk; ?></p>
                </div>
            </div>
        </div>

        <div class="card bg-white p-4">
            <h5 class="mb-3">Log Presensi Masuk Hari Ini</h5>
            <?php if (mysqli_num_rows($logResult) > 0): ?>
                <ul class="list-group">
                    <?php while ($row = mysqli_fetch_assoc($logResult)): ?>
                        <li class="list-group-item d-flex justify-content-between align-items-center">
                            <?php echo $row['nama']; ?>
                            <span class="badge bg-success"><?php echo date("H:i:s", strtotime($row['jam_masuk'])); ?></span>
                        </li>
                    <?php endwhile; ?>
                </ul>
            <?php else: ?>
                <p class="text-muted">Belum ada yang melakukan presensi hari ini.</p>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>
