<?php
require_once "db.php";

if (!isset($_GET['employee_id'], $_GET['month'])) {
    die("Invalid request");
}

$employee_id  = (int)$_GET['employee_id'];
$salary_month = $_GET['month'];

/* EMPLOYEE INFO */
$emp = $conn->query("
    SELECT employee_name, base_salary
    FROM employees
    WHERE employee_id = $employee_id
")->fetch_assoc();

if (!$emp) {
    die("Employee not found");
}

/* ADVANCE */
$adv = $conn->query("
    SELECT IFNULL(SUM(amount),0) total
    FROM advance_pay
    WHERE employee_id = $employee_id
")->fetch_assoc();

$advance = (float)$adv['total'];

/* SALARY PAYMENTS */
$pay = $conn->query("
    SELECT 
        IFNULL(SUM(paid_amount),0) AS total_paid,
        MAX(pay_date) AS last_pay_date
    FROM salary_pay
    WHERE employee_id = $employee_id
    AND salary_month = '$salary_month'
")->fetch_assoc();

$total_paid = (float)$pay['total_paid'];
$pay_date   = $pay['last_pay_date'];

/* CALCULATIONS */
$base_salary = (float)$emp['base_salary'];
$due_salary  = $base_salary - $advance - $total_paid;
if ($due_salary < 0) $due_salary = 0;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Salary Slip</title>
<style>
body {
    font-family: Arial, sans-serif;
    background:#f4f4f4;
}
.print-area {
    width: 700px;
    margin: 30px auto;
}
.header {
    text-align: center;
    margin-bottom: 20px;
}
.header h2 {
    margin: 0;
}
.table {
    width: 100%;
    border-collapse: collapse;
}
.table th, .table td {
    border: 1px solid #ccc;
    padding: 10px;
    text-align: left;
}
.table th {
    
}
.footer-sign {
    margin-top: 30px;
    display: flex;
    justify-content: space-between;
}
.signature {
    margin-top: 40px;
    text-align: center;
}
@media print {
    body {
        background: #fff;
    }
    .print-btn {
        display: none;
    }
}
</style>
</head>

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

<div class="print-area">

    <div class="header">
        <h2>Salary Slip</h2>
        <p><strong>Month:</strong> <?= htmlspecialchars($salary_month) ?></p>
    </div>

    <table class="table">
        <tr>
            <th>Employee Name</th>
            <td><?= htmlspecialchars($emp['employee_name']) ?></td>
        </tr>
        <tr>
            <th>Base Salary</th>
            <td><?= number_format($base_salary,2) ?></td>
        </tr>
        <tr>
            <th>Advance Deduction</th>
            <td><?= number_format($advance,2) ?></td>
        </tr>
        <tr>
            <th>Total Salary Paid</th>
            <td><?= number_format($total_paid,2) ?></td>
        </tr>
        <tr>
            <th>Due Salary</th>
            <td><strong><?= number_format($due_salary,2) ?></strong></td>
        </tr>
        <tr>
            <th>Last Payment Date</th>
            <td><?= htmlspecialchars($pay_date) ?></td>
        </tr>
    </table>

    <div class="footer-sign">
        <div class="signature">
            _______________________<br>
            Authorized Signature
        </div>
        <div class="signature">
            _______________________<br>
            Employee Signature
        </div>
    </div>

</div>
<?php
    include("global/global-print-footer.php");
?>
</body>
</html>
