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

// Cek login
cek_login();

// Cek ID
if (!isset($_GET['id']) || empty($_GET['id'])) {
    header("Location: " . BASE_URL . "/modules/penggajian/index.php?error=" . urlencode("ID penggajian tidak valid!"));
    exit();
}

$id = (int)$_GET['id'];

// Cek akses (karyawan hanya bisa lihat slip gajinya sendiri)
if ($_SESSION['role'] == 'karyawan') {
    $user_id = $_SESSION['user_id'];
    $karyawan = get_karyawan_by_user($user_id);
    
    if ($karyawan) {
        $karyawan_id = $karyawan['id'];
        
        // Cek apakah slip gaji milik karyawan ini
        $check_query = "SELECT * FROM penggajian WHERE id = $id AND karyawan_id = $karyawan_id";
        $check_result = mysqli_query($conn, $check_query);
        
        if (mysqli_num_rows($check_result) == 0) {
            header("Location: " . BASE_URL . "/index.php?access=denied");
            exit();
        }
    } else {
        header("Location: " . BASE_URL . "/index.php?access=denied");
        exit();
    }
}

// Ambil data penggajian
$query = "SELECT p.*, k.nik, k.nama, k.jabatan, k.departemen, k.tanggal_masuk
          FROM penggajian p
          JOIN karyawan k ON p.karyawan_id = k.id
          WHERE p.id = $id";
$result = mysqli_query($conn, $query);

if (mysqli_num_rows($result) == 0) {
    header("Location: " . BASE_URL . "/modules/penggajian/index.php?error=" . urlencode("Data penggajian tidak ditemukan!"));
    exit();
}

$gaji = mysqli_fetch_assoc($result);

// Ambil detail potongan
$query_potongan = "SELECT * FROM detail_potongan WHERE penggajian_id = $id";
$result_potongan = mysqli_query($conn, $query_potongan);

// Ambil data perusahaan
$pengaturan = get_pengaturan();
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Slip Gaji - <?php echo $gaji['nama']; ?></title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css">
    <style>
        @media print {
            body {
                margin: 0;
                padding: 0;
            }
            .no-print {
                display: none !important;
            }
            .container {
                max-width: 100% !important;
                padding: 0 !important;
            }
            .slip {
                break-inside: avoid;
                page-break-inside: avoid;
                border: 1px solid #e2e8f0 !important;
            }
        }
    </style>
</head>
<body class="bg-gray-100 p-6">
    <div class="container mx-auto">
        <div class="no-print mb-6 flex justify-between items-center">
            <h1 class="text-2xl font-bold">Slip Gaji Karyawan</h1>
            <div class="flex space-x-2">
                <button onclick="window.print()" class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
                    <i class="fas fa-print mr-1"></i> Cetak
                </button>
                <a href="<?php echo BASE_URL; ?>/modules/penggajian/index.php" class="bg-gray-500 hover:bg-gray-700 text-white font-bold py-2 px-4 rounded">
                    <i class="fas fa-arrow-left mr-1"></i> Kembali
                </a>
            </div>
        </div>
        
        <div class="bg-white rounded-lg shadow-md slip">
            <div class="p-6 border-b">
                <div class="flex justify-between items-center">
                    <div>
                        <h2 class="text-xl font-bold"><?php echo $pengaturan['nama_perusahaan']; ?></h2>
                        <p class="text-sm text-gray-600"><?php echo $pengaturan['alamat']; ?></p>
                    </div>
                    <div class="text-right">
                        <h3 class="text-lg font-bold">SLIP GAJI KARYAWAN</h3>
                        <p class="text-sm">Periode: <?php echo date('F Y', mktime(0, 0, 0, $gaji['periode_bulan'], 1, $gaji['periode_tahun'])); ?></p>
                    </div>
                </div>
            </div>
            
            <div class="p-6">
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
                    <div>
                        <table class="w-full text-sm">
                            <tr>
                                <td class="py-1 font-medium">NIK</td>
                                <td class="py-1">: <?php echo $gaji['nik']; ?></td>
                            </tr>
                            <tr>
                                <td class="py-1 font-medium">Nama</td>
                                <td class="py-1">: <?php echo $gaji['nama']; ?></td>
                            </tr>
                            <tr>
                                <td class="py-1 font-medium">Jabatan</td>
                                <td class="py-1">: <?php echo $gaji['jabatan']; ?></td>
                            </tr>
                            <tr>
                                <td class="py-1 font-medium">Departemen</td>
                                <td class="py-1">: <?php echo $gaji['departemen']; ?></td>
                            </tr>
                        </table>
                    </div>
                    
                    <div>
                        <table class="w-full text-sm">
                            <tr>
                                <td class="py-1 font-medium">Tanggal Masuk</td>
                                <td class="py-1">: <?php echo tanggal_indo($gaji['tanggal_masuk']); ?></td>
                            </tr>
                            <tr>
                                <td class="py-1 font-medium">Periode Gaji</td>
                                <td class="py-1">: <?php echo date('F Y', mktime(0, 0, 0, $gaji['periode_bulan'], 1, $gaji['periode_tahun'])); ?></td>
                            </tr>
                            <tr>
                                <td class="py-1 font-medium">Tanggal Bayar</td>
                                <td class="py-1">: <?php echo $gaji['tanggal_bayar'] ? tanggal_indo($gaji['tanggal_bayar']) : '-'; ?></td>
                            </tr>
                            <tr>
                                <td class="py-1 font-medium">Status</td>
                                <td class="py-1">: <?php echo ucfirst($gaji['status']); ?></td>
                            </tr>
                        </table>
                    </div>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <h4 class="font-semibold mb-3 border-b pb-2">Pendapatan</h4>
                        <table class="w-full">
                            <tr>
                                <td class="py-2">Gaji Pokok</td>
                                <td class="py-2 text-right"><?php echo format_rupiah($gaji['gaji_pokok']); ?></td>
                            </tr>
                            <tr>
                                <td class="py-2">Tunjangan</td>
                                <td class="py-2 text-right"><?php echo format_rupiah($gaji['tunjangan']); ?></td>
                            </tr>
                            <tr>
                                <td class="py-2">Bonus</td>
                                <td class="py-2 text-right"><?php echo format_rupiah($gaji['bonus']); ?></td>
                            </tr>
                            <tr class="border-t">
                                <td class="py-2 font-bold">Total Pendapatan</td>
                                <td class="py-2 text-right font-bold"><?php echo format_rupiah($gaji['gaji_pokok'] + $gaji['tunjangan'] + $gaji['bonus']); ?></td>
                            </tr>
                        </table>
                    </div>
                    
                    <div>
                        <h4 class="font-semibold mb-3 border-b pb-2">Potongan</h4>
                        <table class="w-full">
                            <?php if (mysqli_num_rows($result_potongan) > 0): ?>
                                <?php while ($potongan = mysqli_fetch_assoc($result_potongan)): ?>
                                <tr>
                                    <td class="py-2">
                                        <?php echo ucfirst($potongan['jenis_potongan']); ?>
                                        <?php if (!empty($potongan['keterangan'])): ?>
                                        <span class="text-xs text-gray-500">(<?php echo $potongan['keterangan']; ?>)</span>
                                        <?php endif; ?>
                                    </td>
                                    <td class="py-2 text-right text-red-600"><?php echo format_rupiah($potongan['nominal']); ?></td>
                                </tr>
                                <?php endwhile; ?>
                            <?php else: ?>
                                <tr>
                                    <td class="py-2">Tidak ada potongan</td>
                                    <td class="py-2 text-right">Rp 0</td>
                                </tr>
                            <?php endif; ?>
                            <tr class="border-t">
                                <td class="py-2 font-bold">Total Potongan</td>
                                <td class="py-2 text-right font-bold text-red-600"><?php echo format_rupiah($gaji['potongan']); ?></td>
                            </tr>
                        </table>
                    </div>
                </div>
                
                <div class="mt-6 pt-4 border-t-2 border-gray-200">
                    <div class="flex justify-between items-center">
                        <h3 class="text-lg font-bold">TOTAL GAJI DITERIMA</h3>
                        <p class="text-xl font-bold"><?php echo format_rupiah($gaji['total_gaji']); ?></p>
                    </div>
                    
                    <p class="mt-1 text-sm text-gray-600">Terbilang: <em><?php echo terbilang($gaji['total_gaji']); ?> rupiah</em></p>
                </div>
                
                <div class="mt-6 pt-4 border-t text-sm">
                    <p>Catatan:</p>
                    <ul class="list-disc list-inside space-y-1 text-gray-600">
                        <li>Slip gaji ini diterbitkan secara elektronik dan sah tanpa tanda tangan.</li>
                        <li>Jika ada pertanyaan terkait penggajian, silahkan hubungi bagian HRD.</li>
                    </ul>
                </div>
                
                <div class="mt-6 flex justify-end">
                    <div class="text-center">
                        <p><?php echo date('d F Y'); ?></p>
                        <div class="h-20"></div>
                        <p class="font-bold">HRD & Keuangan</p>
                        <p><?php echo $pengaturan['nama_perusahaan']; ?></p>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="no-print mt-6 text-center">
            <p class="text-sm text-gray-600">© <?php echo date('Y'); ?> <?php echo $pengaturan['nama_perusahaan']; ?> - Sistem Informasi Penggajian</p>
        </div>
    </div>
    
    <script>
        function terbilang(angka) {
            var bilangan = ['', 'Satu', 'Dua', 'Tiga', 'Empat', 'Lima', 'Enam', 'Tujuh', 'Delapan', 'Sembilan', 'Sepuluh', 'Sebelas'];
            
            if (angka < 12) {
                return bilangan[angka];
            } else if (angka < 20) {
                return bilangan[angka - 10] + ' Belas';
            } else if (angka < 100) {
                return bilangan[Math.floor(angka / 10)] + ' Puluh ' + bilangan[angka % 10];
            } else if (angka < 200) {
                return 'Seratus ' + terbilang(angka - 100);
            } else if (angka < 1000) {
                return bilangan[Math.floor(angka / 100)] + ' Ratus ' + terbilang(angka % 100);
            } else if (angka < 2000) {
                return 'Seribu ' + terbilang(angka - 1000);
            } else if (angka < 1000000) {
                return terbilang(Math.floor(angka / 1000)) + ' Ribu ' + terbilang(angka % 1000);
            } else if (angka < 1000000000) {
                return terbilang(Math.floor(angka / 1000000)) + ' Juta ' + terbilang(angka % 1000000);
            } else if (angka < 1000000000000) {
                return terbilang(Math.floor(angka / 1000000000)) + ' Milyar ' + terbilang(angka % 1000000000);
            } else if (angka < 1000000000000000) {
                return terbilang(Math.floor(angka / 1000000000000)) + ' Trilyun ' + terbilang(angka % 1000000000000);
            }
            
            return '';
        }
        
        // Auto print when page loads
        window.onload = function() {
            setTimeout(function() {
                // window.print();
            }, 1000);
        };
    </script>
</body>
</html>