<?php
include('db_connection.php');

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

$stmt = $conn->prepare("
SELECT p.*, t.name 
FROM payrolls p
JOIN teachers t ON t.teacher_id = p.teacher_id
WHERE p.id = ?
");
$stmt->bind_param("i", $id);
$stmt->execute();
$data = $stmt->get_result()->fetch_assoc();

$gross = $data['basic_salary'] + $data['allowances'];
?>

<!DOCTYPE html>
<html>
<head>
<title>Payslip</title>

<style>
body { font-family: Arial; background:#f4f6f9; }

.card {
    width:650px;
    margin:30px auto;
    background:white;
    padding:25px;
    border-radius:10px;
    box-shadow:0 5px 20px rgba(0,0,0,0.1);
}

h2 { text-align:center; }

.row {
    display:flex;
    justify-content:space-between;
    margin:8px 0;
}

.line {
    border-top:1px solid #ddd;
    margin:10px 0;
}
</style>
</head>

<body>

<div class="card">

<h2>PAYSLIP</h2>
<p><b>Teacher:</b> <?= htmlspecialchars($data['name']) ?></p>
<p><b>Period:</b> <?= $data['pay_month'] ?> <?= $data['pay_year'] ?></p>

<div class="line"></div>

<div class="row">
    <span>Basic Salary</span>
    <span>₦<?= number_format($data['basic_salary'],2) ?></span>
</div>

<div class="row">
    <span>Allowances</span>
    <span>₦<?= number_format($data['allowances'],2) ?></span>
</div>

<div class="row">
    <span><b>Gross Salary</b></span>
    <span><b>₦<?= number_format($gross,2) ?></b></span>
</div>

<div class="line"></div>

<div class="row">
    <span>Absent Deduction</span>
    <span style="color:red;">- ₦<?= number_format($data['deduction'],2) ?></span>
</div>

<div class="row">
    <span>Late Deduction</span>
    <span style="color:red;">- ₦<?= number_format($data['late_deduction'],2) ?></span>
</div>

<div class="row">
    <span>Tax</span>
    <span>- ₦<?= number_format($data['tax'],2) ?></span>
</div>

<div class="row">
    <span>Pension</span>
    <span>- ₦<?= number_format($data['pension'],2) ?></span>
</div>

<div class="line"></div>

<div class="row">
    <h3>Net Salary</h3>
    <h3>₦<?= number_format($data['net_salary'],2) ?></h3>
</div>

<?php if ($data['warning_flag'] == 1): ?>
<p style="color:red;text-align:center;">
⚠ HR WARNING: Excessive Lateness Detected
</p>
<?php endif; ?>

</div>

</body>
</html>