<?php
$page_title = 'Slip Gaji Karyawan';
require_once('includes/load.php');
page_require_level(4);

// Cek apakah ID gaji diberikan melalui URL
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    redirect('gaji.php');
}

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

// Kueri untuk mengambil data slip gaji
$sql = "SELECT 
            s.*, 
            e.id AS employee_id,
            e.first_name, 
            e.last_name, 
            e.job_title, 
            e.hire_date,
            d.name AS department
        FROM salaries s
        LEFT JOIN employees e ON s.employee_id = e.id
        LEFT JOIN departments d ON e.department_id = d.department_id
        WHERE s.id = '{$salary_id}' LIMIT 1";

$slip_data = find_by_sql($sql);

if (!$slip_data) {
    // Data tidak ditemukan, arahkan kembali ke halaman daftar gaji
    redirect('gaji.php');
}

$slip = $slip_data[0];

// Pastikan tipe data numerik
$slip['basic_salary'] = (int)$slip['basic_salary'];
$slip['tunjangan_jabatan'] = (int)$slip['tunjangan_jabatan'];
$slip['adjustment'] = (int)$slip['adjustment'];
$slip['tunjangan_kehadiran'] = (int)$slip['tunjangan_kehadiran'];
$slip['tunjangan_transport'] = (int)$slip['tunjangan_transport'];
$slip['tunjangan_makan'] = (int)$slip['tunjangan_makan'];
$slip['work_visit'] = (int)$slip['work_visit'];
$slip['lembur'] = (int)$slip['lembur'];
$slip['bpjs_kesehatan'] = (int)$slip['bpjs_kesehatan'];
$slip['bpjs_ketenagakerjaan'] = (int)$slip['bpjs_ketenagakerjaan'];
$slip['net_salary'] = (int)$slip['net_salary'];
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Slip Gaji Karyawan</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"/>
    <style>
        /* Styling untuk tampilan umum di layar */
        body {
            background-color: #f5f5f5;
            padding: 20px;
        }
        .slip-container {
            max-width: 800px;
            margin: 20px auto;
            padding: 30px;
            border: 1px solid #ddd;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            background-color: #fff;
        }
        .slip-header {
            display: flex;
            align-items: center;
            justify-content: center;
            border-bottom: 2px solid #333;
            padding-bottom: 15px;
            margin-bottom: 20px;
        }
        .header-logo {
            width: 120px;
            margin-right: 20px;
        }
        .header-text {
            text-align: left;
        }
        .slip-header h2 {
            margin: 0;
            font-weight: bold;
        }
        .slip-header h4 {
            margin: 5px 0 0;
            color: #555;
        }
        .info-section, .details-section {
            margin-bottom: 20px;
        }
        .info-table th, .info-table td, .details-table th, .details-table td {
            border: none;
            padding: 4px;
        }
        .details-table th {
            width: 60%;
        }
        .details-table td {
            text-align: right;
        }
        .total-row {
            background-color: #f9f9f9;
            font-weight: bold;
        }
        .total-row td, .total-row th {
            border-top: 1px solid #ddd;
        }
        .confidential-text {
            font-size: 12px;
            color: #888;
            text-align: center;
            margin-top: 20px;
        }
        
        /* Styling khusus untuk tampilan cetak */
        @media print {
            body {
                margin: 0;
                padding: 0;
            }
            .non-printable {
                display: none;
            }
            .slip-container {
                width: 100%;
                max-width: none;
                margin: 0;
                padding: 0;
                border: none;
                box-shadow: none;
            }
            .slip-header, .info-section, .details-section {
                padding: 10px;
            }
        }
    </style>
</head>
<body>
    <div class="container non-printable" style="text-align: right; margin-bottom: 20px;">
        <button onclick="window.print()" class="btn btn-info">
            <span class="glyphicon glyphicon-print"></span> Cetak Slip
        </button>
    </div>

    <div id="printableArea" class="slip-container">
        <div class="slip-header">
            <img src="images/logo.png" alt="Company Logo" class="header-logo">
            <div class="header-text">
                <h2>SLIP GAJI KARYAWAN</h2>
                <h4>PT. KANAKA HOLDING COMPANY</h4>
                <p>Periode: <?php echo date('F Y', strtotime($slip['pay_date'])); ?></p>
                <p style="font-style: italic; color: #777; margin-top: 10px;">DOKUMEN INI BERSIFAT RAHASIA (PRIVATE & CONFIDENTIAL)</p>
            </div>
        </div>

        <div class="info-section">
            <table class="table table-condensed info-table">
                <tr>
                    <td><strong>Nama</strong></td>
                    <td>: <?php echo remove_junk($slip['first_name'] . ' ' . $slip['last_name']); ?></td>
                    <td><strong>NIK</strong></td>
                    <td>: <?php echo remove_junk($slip['employee_id']); ?></td>
                </tr>
                <tr>
                    <td><strong>Jabatan</strong></td>
                    <td>: <?php echo remove_junk($slip['job_title']); ?></td>
                    <td><strong>Departemen</strong></td>
                    <td>: <?php echo remove_junk($slip['department']); ?></td>
                </tr>
                <tr>
                    <td><strong>Tanggal Bergabung</strong></td>
                    <td>: <?php echo date('d-m-Y', strtotime(remove_junk($slip['hire_date']))); ?></td>
                </tr>
            </table>
        </div>

        <div class="details-section">
            <table class="table table-bordered table-condensed details-table">
                <thead>
                    <tr>
                        <th class="text-center">Keterangan</th>
                        <th class="text-center">Jumlah (Rp)</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Gaji Pokok</td>
                        <td><?php echo number_format($slip['basic_salary'], 0, ',', '.'); ?></td>
                    </tr>
                    <tr>
                        <td>Tunjangan Jabatan</td>
                        <td><?php echo number_format($slip['tunjangan_jabatan'], 0, ',', '.'); ?></td>
                    </tr>
                    <tr>
                        <td>Tunjangan Kehadiran</td>
                        <td><?php echo number_format($slip['tunjangan_kehadiran'], 0, ',', '.'); ?></td>
                    </tr>
                    <tr>
                        <td>Tunjangan Transportasi</td>
                        <td><?php echo number_format($slip['tunjangan_transport'], 0, ',', '.'); ?></td>
                    </tr>
                    <tr>
                        <td>Tunjangan Makan</td>
                        <td><?php echo number_format($slip['tunjangan_makan'], 0, ',', '.'); ?></td>
                    </tr>
                    <tr>
                        <td>Work Visit</td>
                        <td><?php echo number_format($slip['work_visit'], 0, ',', '.'); ?></td>
                    </tr>
                    <tr>
                        <td>Lembur</td>
                        <td><?php echo number_format($slip['lembur'], 0, ',', '.'); ?></td>
                    </tr>
                    <tr>
                        <td>Adjustment</td>
                        <td><?php echo number_format($slip['adjustment'], 0, ',', '.'); ?></td>
                    </tr>
                    <tr class="total-row">
                        <th>Total Pendapatan</th>
                        <td><?php echo number_format($slip['basic_salary'] + $slip['tunjangan_jabatan'] + $slip['adjustment'] + $slip['tunjangan_kehadiran'] + $slip['tunjangan_transport'] + $slip['tunjangan_makan'] + $slip['work_visit'] + $slip['lembur'], 0, ',', '.'); ?></td>
                    </tr>
                    <tr>
                        <td>BPJS Kesehatan</td>
                        <td>- <?php echo number_format($slip['bpjs_kesehatan'], 0, ',', '.'); ?></td>
                    </tr>
                    <tr>
                        <td>BPJS Ketenagakerjaan</td>
                        <td>- <?php echo number_format($slip['bpjs_ketenagakerjaan'], 0, ',', '.'); ?></td>
                    </tr>
                    <tr class="total-row">
                        <th>Total Potongan</th>
                        <td>- <?php echo number_format($slip['bpjs_kesehatan'] + $slip['bpjs_ketenagakerjaan'], 0, ',', '.'); ?></td>
                    </tr>
                    <tr class="total-row">
                        <th>Total Gaji Bersih (THP)</th>
                        <td><strong>Rp<?php echo number_format($slip['net_salary'], 0, ',', '.'); ?></strong></td>
                    </tr>
                </tbody>
            </table>
        </div>
        
        <p class="confidential-text">Dokumen ini adalah informasi pribadi dan rahasia. Dilarang menyebarluaskan atau menggunakan informasi di dalamnya tanpa izin tertulis dari perusahaan.</p>
    </div>
</body>
</html>