<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/yoga/includes/config.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/yoga/includes/functions.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/yoga/includes/header.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/yoga/includes/sidebar.php';

// Cek login
cek_login();
if (!cek_akses('penggajian', 'report')) {
    header("Location: " . BASE_URL . "/index.php?access=denied");
    exit();
}

// Filter
$bulan = isset($_GET['bulan']) ? (int)$_GET['bulan'] : date('m');
$tahun = isset($_GET['tahun']) ? (int)$_GET['tahun'] : date('Y');
$pengaturan = get_pengaturan();

// Query data
$query = "SELECT p.*, k.nik, k.nama, k.jabatan, k.departemen 
          FROM penggajian p JOIN karyawan k ON p.karyawan_id = k.id 
          WHERE p.periode_bulan = $bulan AND p.periode_tahun = $tahun
          ORDER BY k.nama ASC";
$result = mysqli_query($conn, $query);

// Query total
$query_total = "SELECT SUM(total_gaji) as total FROM penggajian WHERE periode_bulan = $bulan AND periode_tahun = $tahun";
$result_total = mysqli_query($conn, $query_total);
$total_gaji = mysqli_fetch_assoc($result_total)['total'] ?: 0;

$print_mode = isset($_GET['print']) && $_GET['print'] == 'true';

if (!$print_mode) {
    require_once $_SERVER['DOCUMENT_ROOT'] . '/yoga/includes/header.php';
    require_once $_SERVER['DOCUMENT_ROOT'] . '/yoga/includes/sidebar.php';
}
?>

<?php if ($print_mode): ?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Laporan Penggajian - <?php echo date('F Y', mktime(0, 0, 0, $bulan, 1, $tahun)); ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
    <style>@media print { body {-webkit-print-color-adjust: exact; print-color-adjust: exact;} .no-print {display: none !important;}}</style>
</head>
<body class="bg-white p-8" onload="window.print()">
    <div class="container mx-auto">
        <header class="text-center mb-8">
            <h1 class="text-3xl font-bold"><?php echo htmlspecialchars($pengaturan['nama_perusahaan']); ?></h1>
            <p class="text-gray-600"><?php echo htmlspecialchars($pengaturan['alamat']); ?></p>
            <div class="h-px bg-gray-300 my-4"></div>
            <h2 class="text-2xl font-semibold">LAPORAN PENGGAJIAN KARYAWAN</h2>
            <p class="text-lg">Periode: <?php echo date('F Y', mktime(0, 0, 0, $bulan, 1, $tahun)); ?></p>
        </header>
<?php else: ?>
<!-- Main Content -->
<div class="main-content flex-1 p-8">
    <div class="flex flex-col md:flex-row justify-between items-start md:items-center mb-6 gap-4">
        <div>
            <h2 class="text-3xl font-bold text-white">Laporan Penggajian</h2>
            <p class="text-slate-400">Menampilkan rekapitulasi penggajian per periode.</p>
        </div>
        <a href="<?php echo $_SERVER['PHP_SELF']; ?>?print=true&bulan=<?php echo $bulan; ?>&tahun=<?php echo $tahun; ?>" target="_blank" class="bg-teal-600 hover:bg-teal-700 text-white font-bold py-2.5 px-5 rounded-lg transition-colors flex items-center gap-2"><i class="fas fa-print"></i> Cetak Laporan</a>
    </div>
    
    <div class="glass-effect p-6 rounded-xl shadow-lg">
        <form action="" method="GET" class="flex flex-wrap items-end gap-4 mb-6 pb-6 border-b border-slate-700/50">
            <div>
                <label for="bulan" class="block text-slate-400 text-sm font-medium mb-2">Bulan</label>
                <select name="bulan" id="bulan" class="w-full border border-slate-600 bg-slate-700/50 text-slate-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-sky-500 px-3 py-2.5">
                    <?php for ($i = 1; $i <= 12; $i++): ?><option value="<?php echo $i; ?>" <?php echo ($bulan == $i) ? 'selected' : ''; ?>><?php echo date('F', mktime(0, 0, 0, $i, 1)); ?></option><?php endfor; ?>
                </select>
            </div>
            <div>
                <label for="tahun" class="block text-slate-400 text-sm font-medium mb-2">Tahun</label>
                <select name="tahun" id="tahun" class="w-full border border-slate-600 bg-slate-700/50 text-slate-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-sky-500 px-3 py-2.5">
                    <?php for ($i = date('Y') - 5; $i <= date('Y'); $i++): ?><option value="<?php echo $i; ?>" <?php echo ($tahun == $i) ? 'selected' : ''; ?>><?php echo $i; ?></option><?php endfor; ?>
                </select>
            </div>
            <button type="submit" class="bg-slate-700 hover:bg-slate-600 text-white font-bold py-2.5 px-6 rounded-lg transition-colors flex items-center gap-2"><i class="fas fa-filter"></i> Filter</button>
        </form>
<?php endif; ?>
        
        <div class="overflow-x-auto">
            <table class="w-full text-left table-auto">
                <thead>
                    <tr class="<?php echo $print_mode ? 'bg-gray-100' : 'border-b border-slate-700'; ?>">
                        <th class="px-4 py-3 <?php echo $print_mode ? 'text-gray-800' : 'text-slate-300'; ?> font-semibold">No</th>
                        <th class="px-4 py-3 <?php echo $print_mode ? 'text-gray-800' : 'text-slate-300'; ?> font-semibold">Nama Karyawan</th>
                        <th class="px-4 py-3 <?php echo $print_mode ? 'text-gray-800' : 'text-slate-300'; ?> font-semibold text-right">Gaji Pokok</th>
                        <th class="px-4 py-3 <?php echo $print_mode ? 'text-gray-800' : 'text-slate-300'; ?> font-semibold text-right">Tunjangan</th>
                        <th class="px-4 py-3 <?php echo $print_mode ? 'text-gray-800' : 'text-slate-300'; ?> font-semibold text-right">Bonus</th>
                        <th class="px-4 py-3 <?php echo $print_mode ? 'text-gray-800' : 'text-slate-300'; ?> font-semibold text-right">Potongan</th>
                        <th class="px-4 py-3 <?php echo $print_mode ? 'text-gray-800' : 'text-slate-300'; ?> font-semibold text-right">Total Gaji</th>
                    </tr>
                </thead>
                <tbody class="<?php echo $print_mode ? 'text-gray-700' : 'text-slate-400'; ?>">
                    <?php if (mysqli_num_rows($result) > 0): $no = 1; while ($row = mysqli_fetch_assoc($result)): ?>
                    <tr class="<?php echo !$print_mode ? 'hover:bg-slate-800/50' : ''; ?>">
                        <td class="px-4 py-3 border-b <?php echo $print_mode ? 'border-gray-200' : 'border-slate-800'; ?>"><?php echo $no++; ?></td>
                        <td class="px-4 py-3 border-b <?php echo $print_mode ? 'border-gray-200' : 'border-slate-800'; ?>">
                            <p class="<?php echo !$print_mode ? 'font-medium text-slate-200' : ''; ?>"><?php echo htmlspecialchars($row['nama']); ?></p>
                            <p class="<?php echo $print_mode ? 'text-xs' : 'text-xs text-slate-500'; ?>"><?php echo htmlspecialchars($row['nik']); ?></p>
                        </td>
                        <td class="px-4 py-3 border-b <?php echo $print_mode ? 'border-gray-200' : 'border-slate-800'; ?> text-right"><?php echo format_rupiah($row['gaji_pokok']); ?></td>
                        <td class="px-4 py-3 border-b <?php echo $print_mode ? 'border-gray-200' : 'border-slate-800'; ?> text-right"><?php echo format_rupiah($row['tunjangan']); ?></td>
                        <td class="px-4 py-3 border-b <?php echo $print_mode ? 'border-gray-200' : 'border-slate-800'; ?> text-right"><?php echo format_rupiah($row['bonus']); ?></td>
                        <td class="px-4 py-3 border-b <?php echo $print_mode ? 'border-gray-200 text-red-600' : 'border-slate-800 text-red-400'; ?> text-right"><?php echo format_rupiah($row['potongan']); ?></td>
                        <td class="px-4 py-3 border-b <?php echo $print_mode ? 'border-gray-200' : 'border-slate-800'; ?> text-right font-bold <?php echo !$print_mode ? 'text-white' : ''; ?>"><?php echo format_rupiah($row['total_gaji']); ?></td>
                    </tr>
                    <?php endwhile; else: ?>
                    <tr><td colspan="7" class="px-4 py-10 text-center <?php echo $print_mode ? 'text-gray-500' : 'text-slate-500'; ?>">Tidak ada data penggajian untuk periode ini.</td></tr>
                    <?php endif; ?>
                </tbody>
                <tfoot>
                    <tr class="<?php echo $print_mode ? 'bg-gray-100' : 'border-t-2 border-slate-700'; ?>">
                        <th colspan="6" class="px-4 py-3 text-right <?php echo $print_mode ? 'text-gray-800' : 'text-slate-300'; ?> font-bold text-lg">TOTAL PENGGAJIAN</th>
                        <th class="px-4 py-3 text-right <?php echo $print_mode ? 'text-gray-800' : 'text-white'; ?> font-bold text-lg"><?php echo format_rupiah($total_gaji); ?></th>
                    </tr>
                </tfoot>
            </table>
        </div>
        
<?php if ($print_mode): ?>
        <footer class="mt-12">
            <div class="flex justify-end">
                <div class="text-center">
                    <p>Tangerang, <?php echo tanggal_indo(date('Y-m-d')); ?></p>
                    <p class="mt-2">Disetujui oleh,</p>
                    <div class="h-24"></div>
                    <p class="font-bold underline">Manajer Keuangan</p>
                    <p><?php echo htmlspecialchars($pengaturan['nama_perusahaan']); ?></p>
                </div>
            </div>
        </footer>
    </div>
</body>
</html>
<?php else: ?>
    </div>
</div>
<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/yoga/includes/footer.php'; ?>
<?php endif; ?>