<?php
// Menggunakan file-file layout yang sudah didesain ulang
require_once $_SERVER['DOCUMENT_ROOT'] . '/yoga/includes/header.php';

// Cek login (biasanya sudah ada di header, tapi untuk keamanan ganda)
// cek_login(); // Asumsi sudah dipanggil di header.php

// Ambil data untuk dashboard
$today = date('Y-m-d');
$month = date('m');
$year = date('Y');

// Total karyawan aktif
$query_karyawan = "SELECT COUNT(*) as total FROM karyawan WHERE status = 'aktif'";
$result_karyawan = mysqli_query($conn, $query_karyawan);
$total_karyawan = mysqli_fetch_assoc($result_karyawan)['total'];

// Jumlah absensi hari ini
$query_absensi = "SELECT COUNT(*) as total FROM absensi WHERE tanggal = '$today'";
$result_absensi = mysqli_query($conn, $query_absensi);
$total_absensi = mysqli_fetch_assoc($result_absensi)['total'];

// Jumlah karyawan telat hari ini
// $pengaturan sudah di-load di header.php
$query_telat = "SELECT COUNT(*) as total FROM absensi WHERE tanggal = '$today' AND jam_masuk > '{$pengaturan['jam_masuk']}'";
$result_telat = mysqli_query($conn, $query_telat);
$total_telat = mysqli_fetch_assoc($result_telat)['total'];

// Total penggajian bulan ini
$query_gaji = "SELECT SUM(total_gaji) as total FROM penggajian WHERE periode_bulan = $month AND periode_tahun = $year";
$result_gaji = mysqli_query($conn, $query_gaji);
$total_gaji = mysqli_fetch_assoc($result_gaji)['total'] ?? 0;

// Daftar karyawan telat hari ini
$query_list_telat = "SELECT k.nama, a.jam_masuk FROM absensi a JOIN karyawan k ON a.karyawan_id = k.id WHERE a.tanggal = '$today' AND a.jam_masuk > '{$pengaturan['jam_masuk']}' ORDER BY a.jam_masuk DESC LIMIT 5";
$result_list_telat = mysqli_query($conn, $query_list_telat);

// Daftar karyawan tidak hadir hari ini
$query_alpha = "SELECT k.nama FROM karyawan k WHERE k.status = 'aktif' AND k.id NOT IN (SELECT karyawan_id FROM absensi WHERE tanggal = '$today') LIMIT 5";
$result_alpha = mysqli_query($conn, $query_alpha);

// Ambil data khusus untuk karyawan
if ($_SESSION['role'] == 'karyawan') {
    $user_id = $_SESSION['user_id'];
    $karyawan = get_karyawan_by_user($user_id);
    
    if ($karyawan) {
        $karyawan_id = $karyawan['id'];
        
        // Cek absensi hari ini
        $query_absen_hari_ini = "SELECT * FROM absensi WHERE karyawan_id = $karyawan_id AND tanggal = '$today'";
        $result_absen_hari_ini = mysqli_query($conn, $query_absen_hari_ini);
        $absen_hari_ini = mysqli_fetch_assoc($result_absen_hari_ini);
        
        // Total kehadiran bulan ini
        $query_kehadiran = "SELECT COUNT(*) as total FROM absensi WHERE karyawan_id = $karyawan_id AND MONTH(tanggal) = $month AND YEAR(tanggal) = $year";
        $result_kehadiran = mysqli_query($conn, $query_kehadiran);
        $total_kehadiran = mysqli_fetch_assoc($result_kehadiran)['total'];
        
        // Gaji bulan ini
        $query_gaji_karyawan = "SELECT * FROM penggajian WHERE karyawan_id = $karyawan_id AND periode_bulan = $month AND periode_tahun = $year";
        $result_gaji_karyawan = mysqli_query($conn, $query_gaji_karyawan);
        $gaji_bulan_ini = mysqli_fetch_assoc($result_gaji_karyawan);
    }
}
?>

<!-- Konten dimulai setelah <main> di header.php -->

<?php if ($_SESSION['role'] != 'karyawan'): ?>
<!-- =============================================================== -->
<!-- =================== DASHBOARD ADMIN & HRD ===================== -->
<!-- =============================================================== -->
<div class="space-y-6">
    <!-- Kartu Statistik -->
    <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6">
        <!-- Karyawan Aktif -->
        <div class="glass-effect p-5 rounded-lg">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm font-medium text-slate-400">Karyawan Aktif</p>
                    <p class="text-3xl font-bold text-white"><?php echo htmlspecialchars($total_karyawan); ?></p>
                </div>
                <div class="bg-sky-500/20 text-sky-400 p-3 rounded-full">
                    <i class="fas fa-users fa-lg"></i>
                </div>
            </div>
        </div>
        <!-- Absensi Hari Ini -->
        <div class="glass-effect p-5 rounded-lg">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm font-medium text-slate-400">Absensi Hari Ini</p>
                    <p class="text-3xl font-bold text-white"><?php echo htmlspecialchars($total_absensi); ?></p>
                </div>
                <div class="bg-emerald-500/20 text-emerald-400 p-3 rounded-full">
                    <i class="fas fa-calendar-check fa-lg"></i>
                </div>
            </div>
        </div>
        <!-- Terlambat Hari Ini -->
        <div class="glass-effect p-5 rounded-lg">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm font-medium text-slate-400">Terlambat Hari Ini</p>
                    <p class="text-3xl font-bold text-white"><?php echo htmlspecialchars($total_telat); ?></p>
                </div>
                <div class="bg-amber-500/20 text-amber-400 p-3 rounded-full">
                    <i class="fas fa-clock fa-lg"></i>
                </div>
            </div>
        </div>
        <!-- Total Gaji -->
        <div class="glass-effect p-5 rounded-lg">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm font-medium text-slate-400">Total Gaji Bulan Ini</p>
                    <p class="text-2xl font-bold text-white"><?php echo format_rupiah($total_gaji); ?></p>
                </div>
                <div class="bg-indigo-500/20 text-indigo-400 p-3 rounded-full">
                    <i class="fas fa-money-bill-wave fa-lg"></i>
                </div>
            </div>
        </div>
    </div>

    <!-- Tabel Informasi -->
    <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
        <!-- Karyawan Terlambat -->
        <div class="glass-effect p-5 rounded-lg">
            <h3 class="text-lg font-semibold text-slate-200 mb-4">Karyawan Terlambat Hari Ini</h3>
            <div class="overflow-x-auto">
                <table class="w-full text-sm text-left">
                    <thead class="border-b border-slate-700 text-slate-400">
                        <tr>
                            <th class="py-2 px-2">Nama Karyawan</th>
                            <th class="py-2 px-2 text-right">Jam Masuk</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (mysqli_num_rows($result_list_telat) > 0): ?>
                            <?php while ($row = mysqli_fetch_assoc($result_list_telat)): ?>
                            <tr class="border-b border-slate-700/50">
                                <td class="py-3 px-2"><?php echo htmlspecialchars($row['nama']); ?></td>
                                <td class="py-3 px-2 text-right font-medium text-amber-400"><?php echo htmlspecialchars($row['jam_masuk']); ?></td>
                            </tr>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <tr><td colspan="2" class="py-4 text-center text-slate-400">Tidak ada karyawan yang terlambat.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
        <!-- Karyawan Tidak Hadir -->
        <div class="glass-effect p-5 rounded-lg">
            <h3 class="text-lg font-semibold text-slate-200 mb-4">Karyawan Tidak Hadir Hari Ini</h3>
            <div class="overflow-x-auto">
                <table class="w-full text-sm text-left">
                     <thead class="border-b border-slate-700 text-slate-400">
                        <tr>
                            <th class="py-2 px-2">Nama Karyawan</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (mysqli_num_rows($result_alpha) > 0): ?>
                            <?php while ($row = mysqli_fetch_assoc($result_alpha)): ?>
                            <tr class="border-b border-slate-700/50">
                                <td class="py-3 px-2"><?php echo htmlspecialchars($row['nama']); ?></td>
                            </tr>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <tr><td class="py-4 text-center text-slate-400">Semua karyawan hadir hari ini.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<?php else: ?>
<!-- =============================================================== -->
<!-- =================== DASHBOARD KARYAWAN ======================== -->
<!-- =============================================================== -->
<?php if (isset($karyawan)): ?>
<div class="space-y-6">
    <!-- Informasi Absensi Hari Ini -->
    <div class="glass-effect p-6 rounded-lg">
        <h3 class="text-lg font-semibold text-slate-200 mb-4">Halo, <?php echo htmlspecialchars($karyawan['nama']); ?>!</h3>
        <?php if (isset($absen_hari_ini) && $absen_hari_ini): ?>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div class="bg-slate-700/50 rounded-lg p-4">
                    <h4 class="text-sm font-medium text-slate-400 mb-2">Jam Masuk</h4>
                    <p class="text-3xl font-bold <?php echo ($absen_hari_ini['jam_masuk'] > $pengaturan['jam_masuk']) ? 'text-amber-400' : 'text-emerald-400'; ?>">
                        <?php echo htmlspecialchars($absen_hari_ini['jam_masuk']); ?>
                    </p>
                    <?php if ($absen_hari_ini['jam_masuk'] > $pengaturan['jam_masuk']): ?>
                    <p class="text-xs text-amber-500">Terlambat <?php echo hitung_keterlambatan($absen_hari_ini['jam_masuk'], $pengaturan['jam_masuk']); ?> menit</p>
                    <?php endif; ?>
                </div>
                <div class="bg-slate-700/50 rounded-lg p-4">
                    <h4 class="text-sm font-medium text-slate-400 mb-2">Jam Pulang</h4>
                    <p class="text-3xl font-bold <?php echo ($absen_hari_ini['jam_keluar'] && $absen_hari_ini['jam_keluar'] < $pengaturan['jam_keluar']) ? 'text-amber-400' : 'text-emerald-400'; ?>">
                        <?php echo $absen_hari_ini['jam_keluar'] ?: '-'; ?>
                    </p>
                </div>
            </div>
            <?php if (empty($absen_hari_ini['jam_masuk']) || empty($absen_hari_ini['jam_keluar'])): ?>
            <div class="mt-5">
                <a href="<?php echo BASE_URL; ?>/modules/absensi/scan.php" class="bg-sky-600 hover:bg-sky-700 text-white font-bold py-3 px-5 rounded-lg transition-colors duration-200 inline-flex items-center gap-2">
                    <i class="fas fa-qrcode"></i>
                    <?php echo empty($absen_hari_ini['jam_masuk']) ? 'Absen Masuk Sekarang' : 'Absen Pulang Sekarang'; ?>
                </a>
            </div>
            <?php endif; ?>
        <?php else: ?>
            <p class="text-slate-400 mb-4">Anda belum melakukan absensi hari ini. Silakan lakukan absen masuk.</p>
            <a href="<?php echo BASE_URL; ?>/modules/absensi/scan.php" class="bg-sky-600 hover:bg-sky-700 text-white font-bold py-3 px-5 rounded-lg transition-colors duration-200 inline-flex items-center gap-2">
                <i class="fas fa-qrcode"></i>
                Absen Masuk Sekarang
            </a>
        <?php endif; ?>
    </div>
    
    <!-- Ringkasan & Gaji -->
    <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
        <!-- Ringkasan Kehadiran -->
        <div class="glass-effect p-5 rounded-lg">
             <h3 class="text-lg font-semibold text-slate-200 mb-4">Ringkasan Bulan Ini</h3>
             <div class="grid grid-cols-2 gap-4">
                <div class="bg-slate-700/50 rounded-lg p-4 text-center">
                    <h4 class="text-sm font-medium text-slate-400 mb-2">Total Kehadiran</h4>
                    <p class="text-3xl font-bold text-sky-400"><?php echo htmlspecialchars($total_kehadiran); ?></p>
                </div>
                <div class="bg-slate-700/50 rounded-lg p-4 text-center">
                    <h4 class="text-sm font-medium text-slate-400 mb-2">Hari Kerja</h4>
                    <p class="text-3xl font-bold text-slate-300"><?php echo date('t'); ?></p>
                </div>
            </div>
        </div>

        <!-- Informasi Gaji -->
        <div class="glass-effect p-5 rounded-lg">
            <h3 class="text-lg font-semibold text-slate-200 mb-4">Gaji Bulan Ini</h3>
            <?php if (isset($gaji_bulan_ini)): ?>
            <div class="space-y-2 text-sm">
                <div class="flex justify-between"><span class="text-slate-400">Gaji Pokok:</span><span class="font-medium text-slate-200"><?php echo format_rupiah($gaji_bulan_ini['gaji_pokok']); ?></span></div>
                <div class="flex justify-between"><span class="text-slate-400">Tunjangan:</span><span class="font-medium text-slate-200"><?php echo format_rupiah($gaji_bulan_ini['tunjangan']); ?></span></div>
                <div class="flex justify-between"><span class="text-slate-400">Bonus:</span><span class="font-medium text-slate-200"><?php echo format_rupiah($gaji_bulan_ini['bonus']); ?></span></div>
                <div class="flex justify-between"><span class="text-slate-400">Potongan:</span><span class="font-medium text-red-400"><?php echo format_rupiah($gaji_bulan_ini['potongan']); ?></span></div>
                <div class="border-t border-slate-700 my-2"></div>
                <div class="flex justify-between text-base"><span class="font-bold text-slate-200">Total Gaji:</span><span class="font-bold text-emerald-400"><?php echo format_rupiah($gaji_bulan_ini['total_gaji']); ?></span></div>
            </div>
            <div class="mt-4">
                <a href="<?php echo BASE_URL; ?>/modules/penggajian/slip.php?id=<?php echo $gaji_bulan_ini['id']; ?>" class="text-sky-500 hover:text-sky-400 text-sm font-medium">
                    <i class="fas fa-print mr-1"></i> Cetak Slip Gaji
                </a>
            </div>
            <?php else: ?>
            <p class="text-slate-400 text-center py-8">Data penggajian bulan ini belum tersedia.</p>
            <?php endif; ?>
        </div>
    </div>
</div>
<?php else: ?>
    <div class="glass-effect rounded-lg p-6 text-center">
        <i class="fas fa-exclamation-triangle text-amber-400 fa-3x mb-4"></i>
        <h3 class="text-xl font-bold text-amber-400">Perhatian!</h3>
        <p class="text-slate-300 mt-2">Data karyawan Anda tidak ditemukan atau belum terdaftar. <br>Silakan hubungi HRD atau admin untuk verifikasi.</p>
    </div>
<?php endif; ?>

<?php endif; ?>

<!-- Konten berakhir sebelum footer.php -->
<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/yoga/includes/footer.php';
?>
