<?php
/** Printable payslip for a single payroll_items row. */
if (session_status() === PHP_SESSION_NONE) session_start();
if (!isset($_SESSION['user_id'])) { header('Location: ../index.php'); exit; }

require_once '../config.php';
requireModule('payroll');
require_once __DIR__ . '/../includes/payroll_lib.php';

$role = $_SESSION['role'] ?? '';
if (!payroll_can_manage($role)) { header('Location: ../dashboard.php?error=no_access'); exit; }

$conn = getDBConnection();
payroll_ensure_tables($conn);
$itemId = (int) ($_GET['item_id'] ?? 0);

$stmt = $conn->prepare("SELECT i.*, r.period, r.status FROM payroll_items i JOIN payroll_runs r ON r.id=i.run_id WHERE i.id=?");
$stmt->bind_param('i', $itemId); $stmt->execute();
$it = $stmt->get_result()->fetch_assoc(); $stmt->close();
if (!$it) { http_response_code(404); echo 'Payslip not found.'; exit; }

$schoolName = getSettingValue('school_name') ?: 'School';
$schoolAddr = getSettingValue('school_address') ?: '';
$logo       = getSettingValue('school_logo');
$m = fn($v) => 'GHS ' . number_format((float) $v, 2);
$periodLabel = date('F Y', strtotime($it['period'] . '-01'));
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"><title>Payslip — <?php echo htmlspecialchars($it['staff_name']); ?> — <?php echo $periodLabel; ?></title>
    <style>
        body{font-family:Arial,Helvetica,sans-serif;color:#111;margin:0;padding:24px;background:#f3f4f6;}
        .slip{max-width:720px;margin:0 auto;background:#fff;border:1px solid #e5e7eb;border-radius:10px;padding:28px;}
        .head{display:flex;align-items:center;gap:14px;border-bottom:2px solid #0f766e;padding-bottom:14px;margin-bottom:16px;}
        .head img{height:56px;}
        .head h1{font-size:20px;margin:0;}
        .head .sub{color:#555;font-size:13px;}
        .meta{display:flex;justify-content:space-between;font-size:13px;margin-bottom:16px;flex-wrap:wrap;gap:8px;}
        table{width:100%;border-collapse:collapse;font-size:14px;}
        th,td{padding:7px 10px;text-align:left;}
        .sect th{background:#f0fdfa;color:#0f766e;border-top:1px solid #ccc;border-bottom:1px solid #ccc;}
        .amt{text-align:right;font-variant-numeric:tabular-nums;}
        .net{background:#0f766e;color:#fff;font-size:16px;font-weight:700;}
        .foot{margin-top:20px;font-size:11px;color:#777;text-align:center;}
        @media print{body{background:#fff;padding:0;}.slip{border:none;}.noprint{display:none;}}
        .noprint{text-align:center;margin:16px;}
        .btn{background:#0f766e;color:#fff;border:none;padding:9px 18px;border-radius:6px;cursor:pointer;font-size:14px;}
    </style>
</head>
<body>
<div class="noprint">
    <button class="btn" onclick="window.print()">🖨 Print / Save PDF</button>
</div>
<div class="slip">
    <div class="head">
        <?php if ($logo): ?><img src="<?php echo htmlspecialchars('../' . ltrim($logo, '/')); ?>" alt=""><?php endif; ?>
        <div>
            <h1><?php echo htmlspecialchars($schoolName); ?></h1>
            <div class="sub"><?php echo htmlspecialchars($schoolAddr); ?></div>
            <div class="sub"><strong>PAYSLIP</strong> — <?php echo $periodLabel; ?><?php echo $it['status'] === 'draft' ? ' (DRAFT)' : ''; ?></div>
        </div>
    </div>

    <div class="meta">
        <div><strong>Employee:</strong> <?php echo htmlspecialchars($it['staff_name']); ?><br>
             <strong>SSNIT No:</strong> <?php echo htmlspecialchars($it['ssnit_number'] ?: '—'); ?></div>
        <div><strong>Bank:</strong> <?php echo htmlspecialchars($it['bank_name'] ?: '—'); ?><br>
             <strong>Account:</strong> <?php echo htmlspecialchars($it['bank_account'] ?: '—'); ?></div>
    </div>

    <table>
        <tr class="sect"><th>Earnings</th><th class="amt">Amount</th></tr>
        <tr><td>Basic Salary</td><td class="amt"><?php echo $m($it['basic']); ?></td></tr>
        <tr><td>Allowances</td><td class="amt"><?php echo $m($it['allowances']); ?></td></tr>
        <tr><td><strong>Gross Pay</strong></td><td class="amt"><strong><?php echo $m($it['gross']); ?></strong></td></tr>

        <tr class="sect"><th>Deductions</th><th class="amt">Amount</th></tr>
        <tr><td>SSNIT (employee 5.5%)</td><td class="amt"><?php echo $m($it['ssnit_employee']); ?></td></tr>
        <tr><td>PAYE (income tax)</td><td class="amt"><?php echo $m($it['paye']); ?></td></tr>
        <tr><td>Other deductions</td><td class="amt"><?php echo $m($it['other_deductions']); ?></td></tr>
        <tr><td><strong>Total Deductions</strong></td><td class="amt"><strong><?php echo $m($it['ssnit_employee'] + $it['paye'] + $it['other_deductions']); ?></strong></td></tr>

        <tr class="net"><td>NET PAY</td><td class="amt"><?php echo $m($it['net']); ?></td></tr>
    </table>

    <div class="foot">
        Taxable income: <?php echo $m($it['taxable']); ?> · Employer SSNIT (13%): <?php echo $m($it['ssnit_employer']); ?><br>
        Computer-generated payslip — <?php echo htmlspecialchars($schoolName); ?> · <?php echo date('d M Y'); ?>
    </div>
</div>
</body>
</html>
