<?php
require_once "auth.php";
checkPermission(['admin','manager']);
require_once "db.php";

$bonus_id = (int)($_GET['bonus_id'] ?? 0);

if (!$bonus_id) {
    die("Invalid Bonus ID");
}

/* FETCH BONUS DATA */
$sql = "
    SELECT 
        b.*,
        e.employee_name,
        e.phone,
        d.title AS designation
    FROM bonus_pay b
    JOIN employees e ON b.employee_id = e.employee_id
    LEFT JOIN designation d ON e.designation_id = d.designation_id
    WHERE b.bonus_id = $bonus_id
";

$result = $conn->query($sql);
if (!$result || $result->num_rows === 0) {
    die("Bonus record not found");
}

$row = $result->fetch_assoc();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bonus Pay Slip</title>

<style>
body{
    font-family: Arial, Helvetica, sans-serif;
    background:#fff;
    margin:0;
    padding:0;
}
.print-container{
    max-width:700px;
    margin:auto;
    padding:20px;
    margin-top: 30px;
}
h2{
    text-align:center;
    margin-bottom:5px;
}
.company{
    text-align:center;
    font-size:14px;
    margin-bottom:15px;
}
table{
    width:100%;
    border-collapse:collapse;
    margin-top:15px;
}
th,td{
    border:1px solid #000;
    padding:8px;
    text-align:left;
}
th{
    width:35%;

}
.footer{
    margin-top:40px;
    display:flex;
    justify-content:space-between;
}
.signature{
    text-align:center;
    width:40%;
}
.signature span{
    display:block;
    border-top:1px solid #000;
    margin-top:40px;
}
@media print{
    button{display:none;}
}
</style>
</head>

<body class="global-print">
    <?php
    include("global/global-print-heading.php");
?>

<div class="print-container">

    <h2>Bonus Payment Slip</h2>
    <div class="company">
        Employee Bonus Payment
    </div>

    <table>
        <tr>
            <th>Employee Name</th>
            <td><?= htmlspecialchars($row['employee_name']) ?></td>
        </tr>
        <tr>
            <th>Designation</th>
            <td><?= htmlspecialchars($row['designation'] ?? '-') ?></td>
        </tr>
        <tr>
            <th>Phone</th>
            <td><?= htmlspecialchars($row['phone'] ?? '-') ?></td>
        </tr>
        <tr>
            <th>Bonus Type</th>
            <td><?= htmlspecialchars($row['bonus_type']) ?></td>
        </tr>
        <tr>
            <th>Bonus Amount</th>
            <td><?= number_format($row['bonus_amount'], 2) ?></td>
        </tr>
        <tr>
            <th>Bonus Date</th>
            <td><?= htmlspecialchars($row['bonus_date']) ?></td>
        </tr>
        <tr>
            <th>Note</th>
            <td><?= nl2br(htmlspecialchars($row['note'] ?? '')) ?></td>
        </tr>
    </table>

    <div class="footer">
        <div class="signature">
            <span>Authorized Signature</span>
        </div>
        <div class="signature">
            <span>Employee Signature</span>
        </div>
    </div>

</div>
<?php
    include("global/global-print-footer.php");
?>
<script>
window.onload = function(){
    window.print();
};
</script>

</body>
</html>
