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

/* =========================
   AUTH CHECK
========================= */
if (
    !isset($_SESSION['teacher_id']) ||
    !isset($_SESSION['role']) ||
    $_SESSION['role'] !== 'teacher'
) {
    header("Location: teacher_login.php");
    exit();
}

$teacher_id = intval($_SESSION['teacher_id']);

/* =========================
   VALIDATE INPUT
========================= */
$payroll_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

if ($payroll_id <= 0) {
    die("Invalid payslip request.");
}

/* =========================
   FETCH PAYROLL (SECURE)
========================= */
$sql = "SELECT 
            p.id,
            p.teacher_id,
            p.salary,
            p.basic_salary,
            p.allowances,
            p.tax,
            p.pension,
            p.deduction,
            p.late_deduction,
            p.absent_days,
            p.net_salary,
            p.payment_date,
            p.pay_month,
            p.pay_year,
            t.name AS teacher_name
        FROM payrolls p
        INNER JOIN teachers t ON t.teacher_id = p.teacher_id
        WHERE p.id = ? AND p.teacher_id = ?";

$stmt = $conn->prepare($sql);

if (!$stmt) {
    die("Query Error: " . $conn->error);
}

$stmt->bind_param("ii", $payroll_id, $teacher_id);
$stmt->execute();

$result = $stmt->get_result();
$payroll = $result->fetch_assoc();
$stmt->close();

/* =========================
   VALIDATION CHECK
========================= */
if (!$payroll) {
    die("Payslip not found or access denied.");
}

/* =========================
   DOWNLOAD HEADER
========================= */
header("Content-Type: text/html");
header("Content-Disposition: attachment; filename=Payslip_" . $payroll['pay_month'] . "_" . $payroll['pay_year'] . ".html");
?>

<!DOCTYPE html>
<html>
<head>
    <title>Payslip</title>
    <style>
        body{
            font-family: Arial, sans-serif;
            background:#f4f6f9;
            padding:30px;
        }

        .container{
            max-width:700px;
            margin:auto;
            background:#fff;
            padding:20px;
            border-radius:10px;
            box-shadow:0 5px 20px rgba(0,0,0,0.1);
        }

        h2{
            text-align:center;
            margin-bottom:20px;
        }

        table{
            width:100%;
            border-collapse:collapse;
        }

        td{
            padding:10px;
            border-bottom:1px solid #ddd;
        }

        .title{
            font-weight:bold;
        }

        .total{
            font-size:18px;
            font-weight:bold;
            color:green;
        }

        .print-btn{
            text-align:center;
            margin-top:20px;
        }

        button{
            padding:10px 15px;
            border:none;
            background:#0f172a;
            color:#fff;
            border-radius:5px;
            cursor:pointer;
        }
    </style>
</head>

<body>

<div class="container">

    <h2>Teacher Payslip</h2>

    <p><b>Name:</b> <?= htmlspecialchars($payroll['teacher_name']) ?></p>
    <p><b>Month:</b> <?= $payroll['pay_month'] ?> <?= $payroll['pay_year'] ?></p>
    <p><b>Payment Date:</b> <?= $payroll['payment_date'] ?></p>

    <hr>

    <table>

        <tr>
            <td class="title">Basic Salary</td>
            <td>₦<?= number_format($payroll['basic_salary']) ?></td>
        </tr>

        <tr>
            <td class="title">Allowances</td>
            <td>₦<?= number_format($payroll['allowances']) ?></td>
        </tr>

        <tr>
            <td class="title">Tax</td>
            <td>₦<?= number_format($payroll['tax']) ?></td>
        </tr>

        <tr>
            <td class="title">Pension</td>
            <td>₦<?= number_format($payroll['pension']) ?></td>
        </tr>

        <tr>
            <td class="title">Deductions</td>
            <td>₦<?= number_format($payroll['deduction'] + $payroll['late_deduction']) ?></td>
        </tr>

        <tr>
            <td class="title">Absent Days</td>
            <td><?= $payroll['absent_days'] ?></td>
        </tr>

        <tr>
            <td class="title total">NET SALARY</td>
            <td class="total">₦<?= number_format($payroll['net_salary']) ?></td>
        </tr>

    </table>

    <div class="print-btn">
        <button onclick="window.print()">Print Payslip</button>
    </div>

</div>

</body>
</html>