<?php
/* ============================================================================
 *  staff/my_payslips.php — list of monthly payslips. Tick months and download
 *  professional payslips (Save as PDF) for the bank.
 *  Bilingual EN/Lao.
 * ==========================================================================*/
session_start();
if (!isset($_SESSION["user_id"]) || empty($_SESSION["company_id"])) {
    header("Location: ../auth/login.php"); exit;
}
require_once '../config/database.php';

$userId    = (int)$_SESSION["user_id"];
$companyId = (int)$_SESSION["company_id"];

$rows = $pdo->prepare("SELECT id, month, year, net_pay, paid_at FROM hr_salary
                       WHERE staff_id=? AND company_id=? ORDER BY year DESC, month DESC LIMIT 36");
$rows->execute([$userId, $companyId]);
$rows = $rows->fetchAll(PDO::FETCH_ASSOC);

$mn   = ["","January","February","March","April","May","June","July","August","September","October","November","December"];
$mnLo = ["","ມັງກອນ","ກຸມພາ","ມີນາ","ເມສາ","ພຶດສະພາ","ມິຖຸນາ","ກໍລະກົດ","ສິງຫາ","ກັນຍາ","ຕຸລາ","ພະຈິກ","ທັນວາ"];
function kip($n){ return number_format((float)$n,0); }
?>
<!DOCTYPE html>
<html lang="lo">
<head>
    <?php $pwaTheme='#0f766e'; include __DIR__.'/../config/pwa_head.php'; ?>
    <title>My Payslips</title>
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Lao:wght@400;500;600;700;800&family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <script src="../assets/lang.js" defer></script>
    <style>
        *,*::before,*::after{box-sizing:border-box;margin:0;padding:0;}
        :root{--primary:#0f766e;--surface:#fff;--bg:#f0f4f8;--border:#e2e8f0;--text:#0f172a;--muted:#64748b;--green:#16a34a;--green-lt:#dcfce7;--amber:#d97706;--amber-lt:#fef3c7;}
        body{font-family:"Noto Sans Lao","Inter",Arial,sans-serif;background:var(--bg);color:var(--text);min-height:100vh;}
        .top-bar{background:var(--surface);border-bottom:1px solid var(--border);
            padding:calc(12px + env(safe-area-inset-top,0px)) 16px 12px;display:flex;justify-content:space-between;
            align-items:center;gap:10px;position:sticky;top:0;z-index:100;box-shadow:0 1px 6px rgba(15,23,42,.06);}
        .back-link{display:inline-flex;align-items:center;gap:6px;background:#f1f5f9;color:var(--text);border:1.5px solid var(--border);padding:7px 13px;border-radius:8px;text-decoration:none;font-size:13px;font-weight:700;}
        .lang-bar{display:flex;gap:7px;}
        .lang-btn{padding:4px 11px;border-radius:999px;border:1.5px solid var(--border);background:var(--surface);font-size:11px;font-weight:700;color:var(--muted);cursor:pointer;font-family:inherit;}
        .lang-btn.active{background:var(--primary);border-color:var(--primary);color:#fff;}
        .page{max-width:560px;margin:auto;padding:16px;}
        h1{font-size:19px;font-weight:800;margin-bottom:4px;}
        .sub{font-size:13px;color:var(--muted);margin-bottom:14px;}
        .list{background:var(--surface);border:1px solid var(--border);border-radius:14px;overflow:hidden;box-shadow:0 2px 14px rgba(15,23,42,.06);margin-bottom:14px;}
        .ps{display:flex;align-items:center;gap:12px;padding:13px 14px;border-bottom:1px solid var(--border);cursor:pointer;}
        .ps:last-child{border-bottom:none;}
        .ps input{width:20px;height:20px;flex-shrink:0;}
        .ps .mo{font-weight:800;font-size:15px;}
        .ps .info{flex:1;}
        .ps .np{font-weight:800;}
        .tag{display:inline-block;padding:2px 9px;border-radius:999px;font-size:10px;font-weight:800;margin-top:3px;}
        .tag.green{background:var(--green-lt);color:#15803d;}.tag.amber{background:var(--amber-lt);color:#92400e;}
        .bar{display:flex;gap:10px;align-items:center;flex-wrap:wrap;position:sticky;bottom:0;background:var(--bg);padding:10px 0;}
        .btn{flex:1;min-width:180px;padding:14px;border:none;border-radius:12px;background:var(--primary);color:#fff;font-size:15px;font-weight:800;cursor:pointer;font-family:inherit;}
        .btn:disabled{opacity:.5;}
        .selall{font-size:13px;font-weight:700;color:var(--primary);background:none;border:none;cursor:pointer;font-family:inherit;}
        .empty{text-align:center;color:var(--muted);padding:36px 16px;}
    </style>
</head>
<body>

<div class="top-bar">
    <a class="back-link" href="../customer/company_home.php">← <span data-lo="ກັບຄືນ" data-en="Back">Back</span></a>
    <div class="lang-bar">
        <button class="lang-btn" id="btn-lo" onclick="setLang('lo')">🇱🇦 ລາວ</button>
        <button class="lang-btn" id="btn-en" onclick="setLang('en')">🇬🇧 EN</button>
    </div>
</div>

<div class="page">
    <h1 data-lo="ໃບແຈ້ງເງິນເດືອນ" data-en="My Payslips">My Payslips</h1>
    <p class="sub" data-lo="ຕິກເດືອນທີ່ຕ້ອງການ ແລ້ວດາວໂຫລດ (ບັນທຶກເປັນ PDF) ສຳລັບທະນາຄານ."
       data-en="Tick the months you want, then download (Save as PDF) for the bank.">Tick months, then download as PDF for the bank.</p>

    <?php if (!$rows): ?>
        <div class="empty" data-lo="ຍັງບໍ່ມີໃບແຈ້ງເງິນເດືອນ. ລໍຖ້າ HR ສ້າງເງິນເດືອນ." data-en="No payslips yet. They appear after HR generates payroll.">No payslips yet — they appear after HR generates payroll.</div>
    <?php else: ?>
        <form method="GET" action="payslip_print.php" target="_blank" id="psForm">
            <div style="text-align:right;margin-bottom:8px;"><button type="button" class="selall" onclick="toggleAll()" data-lo="ເລືອກທັງໝົດ" data-en="Select all">Select all</button></div>
            <div class="list">
                <?php foreach ($rows as $r): ?>
                    <label class="ps">
                        <input type="checkbox" name="ids[]" value="<?= (int)$r['id'] ?>" onchange="upd()">
                        <div class="info">
                            <div class="mo" data-lo="<?= $mnLo[$r['month']] ?> <?= $r['year'] ?>" data-en="<?= $mn[$r['month']] ?> <?= $r['year'] ?>"><?= $mn[$r['month']] ?> <?= $r['year'] ?></div>
                            <?php if ($r['paid_at']): ?><span class="tag green" data-lo="ຈ່າຍແລ້ວ" data-en="Paid">Paid</span>
                            <?php else: ?><span class="tag amber" data-lo="ຮ່າງ" data-en="Draft">Draft</span><?php endif; ?>
                        </div>
                        <div class="np">₭ <?= kip($r['net_pay']) ?></div>
                    </label>
                <?php endforeach; ?>
            </div>
            <div class="bar">
                <button class="btn" type="submit" id="dlBtn" disabled data-lo="⬇ ດາວໂຫລດທີ່ເລືອກ" data-en="⬇ Download selected">⬇ Download selected</button>
            </div>
        </form>
    <?php endif; ?>
</div>

<script>
function upd(){
    var n=document.querySelectorAll('input[name="ids[]"]:checked').length;
    var b=document.getElementById('dlBtn'); if(b) b.disabled = n===0;
}
function toggleAll(){
    var boxes=document.querySelectorAll('input[name="ids[]"]');
    var anyUnchecked=[...boxes].some(b=>!b.checked);
    boxes.forEach(b=>b.checked=anyUnchecked);
    upd();
}
</script>

<?php require __DIR__ . '/../config/auto_update.php'; ?>
</body>
</html>