<?php
session_start();
require_once '../config/database.php';

if (!isset($_SESSION["user_id"]) || !in_array($_SESSION["role"], ["customer","company_admin"])) {
    header("Location: ../auth/login.php");
    exit;
}

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

/* Customer wallet */
$stmt = $pdo->prepare("SELECT * FROM customers WHERE user_id = ? LIMIT 1");
$stmt->execute([$userId]);
$customer = $stmt->fetch(PDO::FETCH_ASSOC);

$unreadPayments = 0;
if ($customer) {
    $stmt = $pdo->prepare("SELECT COUNT(*) FROM transactions WHERE to_type='customer' AND to_id=? AND customer_read_at IS NULL");
    $stmt->execute([$customer["id"]]);
    $unreadPayments = (int)$stmt->fetchColumn();
}

/* Recent transactions */
$recentTx = [];
if ($customer) {
    $stmt = $pdo->prepare("
        SELECT * FROM transactions
        WHERE to_type='customer' AND to_id=?
        ORDER BY created_at DESC LIMIT 5
    ");
    $stmt->execute([$customer["id"]]);
    $recentTx = $stmt->fetchAll(PDO::FETCH_ASSOC);
}

/* Company / HR info */
$companyInfo = null;
$hrStats     = null;

try {
    $cs = $pdo->prepare("
        SELECT cs.company_id, cs.staff_role, cs.position, cs.department,
               c.name AS company_name
        FROM company_staff cs
        JOIN companies c ON c.id = cs.company_id
        WHERE cs.user_id = ? AND c.status = 'active'
        LIMIT 1
    ");
    $cs->execute([$userId]);
    $companyInfo = $cs->fetch(PDO::FETCH_ASSOC);
} catch (Throwable $e) {}

if ($companyInfo) {
    $cid = (int)$companyInfo["company_id"];
    $m   = (int)date('n');
    $y   = (int)date('Y');
    try {
        $sal = $pdo->prepare("SELECT net_pay, paid_at, base_salary, total_ot, total_allowances, deductions FROM hr_salary WHERE staff_id=? AND company_id=? AND month=? AND year=? LIMIT 1");
        $sal->execute([$userId, $cid, $m, $y]);
        $salRow = $sal->fetch(PDO::FETCH_ASSOC);

        $pl = $pdo->prepare("SELECT COUNT(*) FROM hr_leave WHERE staff_id=? AND status='pending'");
        $pl->execute([$userId]);

        $att = $pdo->prepare("SELECT COUNT(*) FROM hr_attendance WHERE staff_id=? AND company_id=? AND MONTH(work_date)=? AND YEAR(work_date)=? AND status='present'");
        $att->execute([$userId, $cid, $m, $y]);

        $leaves = $pdo->prepare("SELECT * FROM hr_leave WHERE staff_id=? AND company_id=? ORDER BY created_at DESC LIMIT 5");
        $leaves->execute([$userId, $cid]);

        $hrStats = [
            "net_pay"       => $salRow ? (float)$salRow["net_pay"]         : null,
            "base_salary"   => $salRow ? (float)$salRow["base_salary"]     : null,
            "total_ot"      => $salRow ? (float)$salRow["total_ot"]        : null,
            "allowances"    => $salRow ? (float)$salRow["total_allowances"]: null,
            "deductions"    => $salRow ? (float)$salRow["deductions"]      : null,
            "paid_at"       => $salRow ? $salRow["paid_at"]                : null,
            "pending_leave" => (int)$pl->fetchColumn(),
            "att_days"      => (int)$att->fetchColumn(),
            "leaves"        => $leaves->fetchAll(PDO::FETCH_ASSOC),
        ];
    } catch (Throwable $e) {}
}

$mn = ["","January","February","March","April","May","June","July","August","September","October","November","December"];
$m  = (int)date('n');
$y  = (int)date('Y');
?>
<!DOCTYPE html>
<html lang="lo">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Seven Digital — <?= htmlspecialchars($_SESSION["name"]) ?></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">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
<style>
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
:root{
    --red:#b91c1c;--red-lt:#fee2e2;
    --primary:#2563eb;--primary-dk:#1e3a5f;
    --teal:#0f766e;--teal-lt:#ccfbf1;
    --green:#16a34a;--green-lt:#dcfce7;
    --amber:#d97706;--amber-lt:#fef3c7;
    --purple:#7c3aed;--purple-lt:#ede9fe;
    --surface:#fff;--bg:#f0f4f8;--border:#e2e8f0;
    --text:#0f172a;--muted:#64748b;
    --radius:16px;--shadow:0 2px 14px rgba(15,23,42,.09)
}
body{font-family:"Noto Sans Lao","Inter",Arial,sans-serif;background:var(--bg);color:var(--text);min-height:100vh}

/* ── Top bar ── */
.top-bar{background:var(--red);color:#fff;padding:0 32px;display:flex;align-items:center;justify-content:space-between;height:60px;position:sticky;top:0;z-index:100;box-shadow:0 2px 12px rgba(0,0,0,.18)}
.top-brand{display:flex;align-items:center;gap:12px}
.top-logo{width:36px;height:36px;background:rgba(255,255,255,.2);border-radius:10px;display:flex;align-items:center;justify-content:center;font-weight:800;font-size:16px}
.top-brand-text{font-size:16px;font-weight:800}
.top-right{display:flex;align-items:center;gap:12px}
.top-user{display:flex;align-items:center;gap:8px;font-size:13px;color:rgba(255,255,255,.9)}
.avatar{width:34px;height:34px;border-radius:50%;background:rgba(255,255,255,.25);display:flex;align-items:center;justify-content:center;font-weight:800;font-size:14px;color:#fff}
.top-btn{display:inline-flex;align-items:center;gap:6px;padding:7px 14px;border-radius:8px;font-size:12px;font-weight:700;text-decoration:none;border:1.5px solid rgba(255,255,255,.35);color:#fff;transition:all .15s}
.top-btn:hover{background:rgba(255,255,255,.15)}
.top-btn.mobile{background:rgba(255,255,255,.15)}
.logout-btn{background:rgba(255,255,255,.1);border-color:rgba(255,255,255,.3)}

/* ── Page ── */
.page{padding:28px 32px;max-width:1280px;margin:auto}

/* ── Hero row ── */
.hero-row{display:grid;grid-template-columns:1fr 1fr;gap:20px;margin-bottom:24px}

/* Wallet card */
.wallet-card{background:linear-gradient(135deg,#7f1d1d,#b91c1c);border-radius:20px;padding:28px 30px;color:#fff;box-shadow:0 8px 28px rgba(185,28,28,.3);position:relative;overflow:hidden}
.wallet-card::before{content:"";position:absolute;width:220px;height:220px;right:-40px;top:-60px;border-radius:50%;background:rgba(255,255,255,.07)}
.wallet-card::after{content:"";position:absolute;width:140px;height:140px;left:-30px;bottom:-50px;border-radius:50%;background:rgba(255,255,255,.05)}
.wallet-lbl{font-size:12px;opacity:.75;text-transform:uppercase;letter-spacing:.5px;font-weight:700}
.wallet-code{font-size:18px;font-weight:800;margin:6px 0 20px;opacity:.9}
.wallet-balance-lbl{font-size:12px;opacity:.7;margin-bottom:6px}
.wallet-balance{font-size:42px;font-weight:800;line-height:1;position:relative;z-index:1}
.wallet-name{font-size:16px;font-weight:700;margin-top:16px;opacity:.9;position:relative;z-index:1}
.wallet-actions{display:flex;gap:10px;margin-top:20px;position:relative;z-index:1;flex-wrap:wrap}
.wallet-action{display:inline-flex;align-items:center;gap:7px;padding:9px 16px;border-radius:10px;background:rgba(255,255,255,.15);color:#fff;text-decoration:none;font-size:13px;font-weight:700;border:1.5px solid rgba(255,255,255,.25);transition:background .15s}
.wallet-action:hover{background:rgba(255,255,255,.25)}

/* Notification in hero */
.notif-card{background:var(--green-lt);border:1.5px solid #86efac;border-radius:16px;padding:16px 20px;display:flex;align-items:center;justify-content:space-between;gap:12px;margin-bottom:14px;text-decoration:none}
.notif-title{font-size:14px;font-weight:700;color:#166534}
.notif-sub{font-size:12px;color:#15803d;margin-top:3px}
.notif-btn{background:#16a34a;color:#fff;padding:8px 16px;border-radius:8px;font-weight:700;font-size:13px;white-space:nowrap}

/* Quick links */
.quick-card{background:var(--surface);border-radius:20px;padding:24px;box-shadow:var(--shadow);border:1px solid var(--border)}
.quick-title{font-size:13px;font-weight:700;color:var(--muted);text-transform:uppercase;letter-spacing:.5px;margin-bottom:18px}
.quick-grid{display:grid;grid-template-columns:repeat(2,1fr);gap:12px}
.quick-item{display:flex;flex-direction:column;align-items:center;gap:8px;padding:16px 10px;border-radius:14px;border:1.5px solid var(--border);text-decoration:none;color:var(--text);transition:all .15s;background:#fafafa}
.quick-item:hover{border-color:var(--red);background:#fff7f7;transform:translateY(-2px)}
.quick-item .qi-icon{width:48px;height:48px;border-radius:14px;display:flex;align-items:center;justify-content:center;font-size:22px}
.qi-red{background:#fee2e2} .qi-blue{background:#dbeafe} .qi-green{background:#dcfce7} .qi-purple{background:#ede9fe}
.quick-item span{font-size:12px;font-weight:700;color:var(--muted);text-align:center}

/* ── Main content ── */
.main-grid{display:grid;grid-template-columns:1fr 1fr;gap:20px;margin-bottom:24px}

/* Section card */
.sec-card{background:var(--surface);border-radius:var(--radius);box-shadow:var(--shadow);border:1px solid var(--border);overflow:hidden}
.sec-head{padding:16px 20px;border-bottom:1px solid var(--border);display:flex;align-items:center;justify-content:space-between}
.sec-head h2{font-size:15px;font-weight:800}
.sec-head a{font-size:12px;color:var(--primary);font-weight:700;text-decoration:none}

/* Transactions */
.tx-list{padding:8px 0}
.tx-row{display:flex;align-items:center;gap:12px;padding:12px 20px;border-bottom:1px solid #f8fafc;transition:background .1s}
.tx-row:last-child{border-bottom:none}
.tx-row:hover{background:#fafafa}
.tx-icon{width:38px;height:38px;border-radius:10px;background:#dcfce7;display:flex;align-items:center;justify-content:center;font-size:18px;flex-shrink:0}
.tx-info{flex:1;min-width:0}
.tx-name{font-size:13px;font-weight:700;color:var(--text);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.tx-date{font-size:11px;color:var(--muted);margin-top:2px}
.tx-amount{font-size:15px;font-weight:800;color:var(--green);white-space:nowrap}
.tx-empty{padding:32px;text-align:center;color:var(--muted);font-size:13px}

/* ── HR Section ── */
.hr-full{background:var(--surface);border-radius:var(--radius);box-shadow:var(--shadow);border:1px solid var(--border);margin-bottom:24px;overflow:hidden}
.hr-head{background:linear-gradient(135deg,#042f2e,#0f766e);padding:20px 24px;color:#fff;display:flex;align-items:center;justify-content:space-between}
.hr-head h2{font-size:16px;font-weight:800}
.hr-head .co-sub{font-size:12px;opacity:.75;margin-top:3px}
.hr-head .salary-info{text-align:right}
.hr-head .salary-val{font-size:24px;font-weight:800}
.hr-head .salary-lbl{font-size:11px;opacity:.75;margin-top:2px}

.hr-stats-row{display:grid;grid-template-columns:repeat(4,1fr);gap:0;border-bottom:1px solid var(--border)}
.hr-stat{padding:16px 20px;border-right:1px solid var(--border);text-align:center}
.hr-stat:last-child{border-right:none}
.hr-stat .sv{font-size:24px;font-weight:800;color:var(--text)}
.hr-stat .sl{font-size:11px;color:var(--muted);font-weight:600;margin-top:3px;text-transform:uppercase;letter-spacing:.3px}

.hr-menu-row{display:grid;grid-template-columns:repeat(6,1fr);gap:0;padding:16px 12px}
.hr-link{display:flex;flex-direction:column;align-items:center;gap:7px;padding:12px 8px;border-radius:12px;text-decoration:none;color:var(--text);transition:background .15s;position:relative}
.hr-link:hover{background:#f8fafc}
.hr-link .hl-icon{width:46px;height:46px;border-radius:14px;display:flex;align-items:center;justify-content:center;font-size:22px}
.hl-teal{background:var(--teal-lt)} .hl-blue{background:#dbeafe} .hl-amber{background:var(--amber-lt)} .hl-purple{background:var(--purple-lt)} .hl-pink{background:#fce7f3} .hl-green{background:var(--green-lt)}
.hr-link span{font-size:12px;font-weight:700;color:var(--muted);text-align:center}
.hr-badge{position:absolute;top:8px;right:14px;background:var(--red);color:#fff;font-size:10px;font-weight:800;padding:2px 6px;border-radius:999px}

/* HR salary breakdown */
.salary-breakdown{display:grid;grid-template-columns:1fr 1fr;gap:0;border-top:1px solid var(--border)}
.sb-item{padding:14px 20px;border-right:1px solid var(--border);border-bottom:1px solid var(--border)}
.sb-item:nth-child(even){border-right:none}
.sb-item:nth-last-child(-n+2){border-bottom:none}
.sb-lbl{font-size:11px;color:var(--muted);font-weight:600;text-transform:uppercase;letter-spacing:.3px}
.sb-val{font-size:18px;font-weight:800;color:var(--text);margin-top:4px}

/* Leave table */
.leave-table{width:100%;border-collapse:collapse}
.leave-table th{background:#f8fafc;padding:10px 16px;text-align:left;font-size:11px;font-weight:700;color:var(--muted);text-transform:uppercase;letter-spacing:.4px;border-bottom:1px solid var(--border)}
.leave-table td{padding:11px 16px;font-size:13px;border-bottom:1px solid var(--border);vertical-align:middle}
.leave-table tr:last-child td{border-bottom:none}
.badge{display:inline-flex;padding:3px 9px;border-radius:999px;font-size:11px;font-weight:700}
.bp{background:var(--amber-lt);color:var(--amber)}
.ba{background:var(--green-lt);color:var(--green)}
.br{background:var(--red-lt);color:var(--red)}

/* Admin portal banner */
.admin-banner{background:linear-gradient(135deg,#0f2444,#1d4ed8);border-radius:var(--radius);padding:20px 24px;margin-bottom:24px;display:flex;align-items:center;justify-content:space-between;text-decoration:none;box-shadow:0 4px 14px rgba(15,23,42,.2)}
.admin-banner .ab-left{display:flex;align-items:center;gap:14px;color:#fff}
.admin-banner .ab-icon{width:48px;height:48px;border-radius:14px;background:rgba(255,255,255,.15);display:flex;align-items:center;justify-content:center;font-size:24px}
.admin-banner .ab-title{font-size:16px;font-weight:800;color:#fff}
.admin-banner .ab-sub{font-size:12px;color:rgba(255,255,255,.75);margin-top:3px}
.admin-banner .ab-arrow{font-size:28px;color:rgba(255,255,255,.6)}

/* Footer */
.footer{text-align:center;padding:20px;font-size:12px;color:var(--muted);border-top:1px solid var(--border)}

@media(max-width:900px){
    .hero-row,.main-grid{grid-template-columns:1fr}
    .hr-stats-row{grid-template-columns:repeat(2,1fr)}
    .hr-menu-row{grid-template-columns:repeat(3,1fr)}
    .quick-grid{grid-template-columns:repeat(4,1fr)}
}
</style>
</head>
<body>

<!-- ── Top bar ── -->
<div class="top-bar">
    <div class="top-brand">
        <div class="top-logo">7D</div>
        <div class="top-brand-text">Seven Digital Wallet</div>
    </div>
    <div class="top-right">
        <a class="top-btn mobile" href="dashboard.php">📱 Mobile View</a>
        <?php if ($_SESSION["role"] === "company_admin"): ?>
        <a class="top-btn" href="../company/dashboard.php">🏢 Admin Portal</a>
        <?php endif; ?>
        <div class="top-user">
            <div class="avatar"><?= strtoupper(mb_substr($_SESSION["name"], 0, 1)) ?></div>
            <span><?= htmlspecialchars($_SESSION["name"]) ?></span>
        </div>
        <a class="top-btn logout-btn" href="../auth/logout.php">⏻ Logout</a>
    </div>
</div>

<div class="page">

    <?php if ($_SESSION["role"] === "company_admin"): ?>
    <!-- Admin portal banner -->
    <a class="admin-banner" href="../company/dashboard.php">
        <div class="ab-left">
            <div class="ab-icon">🏢</div>
            <div>
                <div class="ab-title">Company Admin Portal</div>
                <div class="ab-sub">Manage staff, HR, salary, attendance and company credit</div>
            </div>
        </div>
        <div class="ab-arrow">›</div>
    </a>
    <?php endif; ?>

    <!-- ── Hero row ── -->
    <div class="hero-row">

        <!-- Wallet card -->
        <?php if ($customer): ?>
        <div class="wallet-card">
            <div class="wallet-lbl">Account Number</div>
            <div class="wallet-code"><?= htmlspecialchars($customer["customer_code"]) ?></div>
            <div class="wallet-balance-lbl">Current Balance</div>
            <div class="wallet-balance" id="desktopBalance">₭ <?= number_format($customer["balance"], 2) ?></div>
            <div class="wallet-name"><?= htmlspecialchars($customer["full_name"]) ?></div>
            <div class="wallet-actions">
                <a class="wallet-action" href="wallet_history.php">📄 Statement</a>
                <a class="wallet-action" href="qr_code.php">📷 My QR</a>
                <a class="wallet-action" href="transfer.php">💸 Transfer</a>
                <a class="wallet-action" href="scan_qr.php">📲 Scan Pay</a>
            </div>
        </div>
        <?php else: ?>
        <div style="background:var(--surface);border-radius:20px;padding:28px;display:flex;align-items:center;justify-content:center;color:var(--muted);font-size:14px;box-shadow:var(--shadow)">
            No wallet connected to this account.
        </div>
        <?php endif; ?>

        <!-- Quick links + notification -->
        <div class="quick-card">
            <?php if ($unreadPayments > 0): ?>
            <a class="notif-card" href="wallet_history.php" id="notifBox">
                <div>
                    <div class="notif-title">💰 New Money Received</div>
                    <div class="notif-sub" id="notifText">You have <?= $unreadPayments ?> unread payment(s).</div>
                </div>
                <div class="notif-btn">View</div>
            </a>
            <?php endif; ?>

            <div class="quick-title">Quick Access</div>
            <div class="quick-grid">
                <a class="quick-item" href="wallet_history.php">
                    <div class="qi-icon qi-red">📄</div>
                    <span>Statement</span>
                </a>
                <a class="quick-item" href="qr_code.php">
                    <div class="qi-icon qi-blue">📷</div>
                    <span>My QR Code</span>
                </a>
                <a class="quick-item" href="transfer.php">
                    <div class="qi-icon qi-green">💸</div>
                    <span>Transfer</span>
                </a>
                <a class="quick-item" href="scan_qr.php">
                    <div class="qi-icon qi-purple">📲</div>
                    <span>Scan & Pay</span>
                </a>
            </div>
        </div>
    </div>

    <!-- ── Recent transactions + Leave ── -->
    <div class="main-grid">

        <!-- Recent transactions -->
        <div class="sec-card">
            <div class="sec-head">
                <h2>💳 Recent Transactions</h2>
                <a href="wallet_history.php">View all →</a>
            </div>
            <?php if (empty($recentTx)): ?>
                <div class="tx-empty">No transactions yet.</div>
            <?php else: ?>
            <div class="tx-list">
                <?php foreach ($recentTx as $tx): ?>
                <div class="tx-row">
                    <div class="tx-icon">💰</div>
                    <div class="tx-info">
                        <div class="tx-name"><?= htmlspecialchars($tx["note"] ?? "Payment received") ?></div>
                        <div class="tx-date"><?= date("d M Y H:i", strtotime($tx["created_at"])) ?></div>
                    </div>
                    <div class="tx-amount">+ ₭ <?= number_format((float)($tx["amount"] ?? 0), 0) ?></div>
                </div>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </div>

        <!-- Leave requests -->
        <?php if ($companyInfo && $hrStats): ?>
        <div class="sec-card">
            <div class="sec-head">
                <h2>🏖️ Leave Requests</h2>
                <a href="../staff/my_leave.php">Request leave →</a>
            </div>
            <?php if (empty($hrStats["leaves"])): ?>
                <div class="tx-empty">No leave requests yet.</div>
            <?php else: ?>
            <table class="leave-table">
                <thead><tr><th>Type</th><th>Dates</th><th>Days</th><th>Status</th></tr></thead>
                <tbody>
                <?php
                $typeLabels = ["annual"=>"🏖️ Annual","sick"=>"🤒 Sick","unpaid"=>"💸 Unpaid","maternity"=>"🤱 Maternity","other"=>"📋 Other"];
                foreach ($hrStats["leaves"] as $l):
                    $bc = ["pending"=>"bp","approved"=>"ba","rejected"=>"br"][$l["status"]] ?? "bp";
                ?>
                <tr>
                    <td><?= $typeLabels[$l["leave_type"]] ?? $l["leave_type"] ?></td>
                    <td style="font-size:12px;color:var(--muted)">
                        <?= date("d M", strtotime($l["start_date"])) ?> – <?= date("d M Y", strtotime($l["end_date"])) ?>
                    </td>
                    <td style="font-weight:700"><?= $l["days"] ?></td>
                    <td><span class="badge <?= $bc ?>"><?= ucfirst($l["status"]) ?></span></td>
                </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
        <?php else: ?>
        <div class="sec-card">
            <div class="sec-head"><h2>📋 Account Info</h2></div>
            <div style="padding:20px">
                <div style="font-size:13px;color:var(--muted);line-height:1.8">
                    <div><strong>Name:</strong> <?= htmlspecialchars($_SESSION["name"]) ?></div>
                    <div><strong>Role:</strong> <?= ucfirst($_SESSION["role"]) ?></div>
                    <?php if ($customer): ?>
                    <div><strong>Account:</strong> <?= htmlspecialchars($customer["customer_code"]) ?></div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <?php endif; ?>

    </div>

    <!-- ── HR Full Section ── -->
    <?php if ($companyInfo && $hrStats): ?>
    <div class="hr-full">

        <!-- HR Header -->
        <div class="hr-head">
            <div>
                <h2>🏢 <?= htmlspecialchars($companyInfo["company_name"]) ?> — HR Portal</h2>
                <div class="co-sub">
                    <?= htmlspecialchars($companyInfo["position"] ?? ucfirst($companyInfo["staff_role"])) ?>
                    <?php if ($companyInfo["department"]): ?> · <?= htmlspecialchars($companyInfo["department"]) ?><?php endif; ?>
                    · <?= $mn[$m] ?> <?= $y ?>
                </div>
            </div>
            <?php if ($hrStats["net_pay"] !== null): ?>
            <div class="salary-info">
                <div class="salary-val">₭ <?= number_format($hrStats["net_pay"], 0) ?></div>
                <div class="salary-lbl"><?= $hrStats["paid_at"] ? "✅ Paid" : "⏳ Pending" ?> · Net Pay</div>
            </div>
            <?php endif; ?>
        </div>

        <!-- Stats row -->
        <div class="hr-stats-row">
            <div class="hr-stat">
                <div class="sv" style="color:var(--green)"><?= $hrStats["att_days"] ?></div>
                <div class="sl">Days Present</div>
            </div>
            <div class="hr-stat">
                <div class="sv" style="color:<?= $hrStats["pending_leave"] > 0 ? "var(--amber)" : "var(--muted)" ?>">
                    <?= $hrStats["pending_leave"] ?>
                </div>
                <div class="sl">Leave Pending</div>
            </div>
            <div class="hr-stat">
                <div class="sv">₭ <?= $hrStats["net_pay"] !== null ? number_format($hrStats["net_pay"], 0) : "—" ?></div>
                <div class="sl">Net Pay</div>
            </div>
            <div class="hr-stat">
                <div class="sv">₭ <?= $hrStats["total_ot"] !== null ? number_format($hrStats["total_ot"], 0) : "—" ?></div>
                <div class="sl">OT Pay</div>
            </div>
        </div>

        <!-- Salary breakdown -->
        <?php if ($hrStats["net_pay"] !== null): ?>
        <div class="salary-breakdown">
            <div class="sb-item">
                <div class="sb-lbl">Base Salary</div>
                <div class="sb-val">₭ <?= number_format($hrStats["base_salary"], 0) ?></div>
            </div>
            <div class="sb-item">
                <div class="sb-lbl">OT Pay</div>
                <div class="sb-val">₭ <?= number_format($hrStats["total_ot"], 0) ?></div>
            </div>
            <div class="sb-item">
                <div class="sb-lbl">Allowances</div>
                <div class="sb-val">₭ <?= number_format($hrStats["allowances"], 0) ?></div>
            </div>
            <div class="sb-item">
                <div class="sb-lbl">Deductions</div>
                <div class="sb-val" style="color:var(--red)">- ₭ <?= number_format($hrStats["deductions"], 0) ?></div>
            </div>
        </div>
        <?php endif; ?>

        <!-- HR quick links -->
        <div class="hr-menu-row">
            <a class="hr-link" href="../staff/my_salary.php">
                <div class="hl-icon hl-teal">💵</div>
                <span>My Salary</span>
            </a>
            <a class="hr-link" href="../staff/my_attendance.php">
                <div class="hl-icon hl-blue">🕐</div>
                <span>Attendance</span>
            </a>
            <a class="hr-link" href="../staff/my_leave.php">
                <?php if ($hrStats["pending_leave"] > 0): ?>
                    <span class="hr-badge"><?= $hrStats["pending_leave"] ?></span>
                <?php endif; ?>
                <div class="hl-icon hl-amber">🏖️</div>
                <span>My Leave</span>
            </a>
            <a class="hr-link" href="../staff/my_allowances.php">
                <div class="hl-icon hl-purple">🎁</div>
                <span>Allowances</span>
            </a>
            <a class="hr-link" href="../staff/my_payslips.php">
                <div class="hl-icon hl-pink">🧾</div>
                <span>Payslips</span>
            </a>
            <a class="hr-link" href="../staff/change_password.php">
                <div class="hl-icon hl-green">🔑</div>
                <span>Password</span>
            </a>
        </div>

    </div>
    <?php endif; ?>

    <div class="footer">
        Seven Digital Credit System &copy; <?= date("Y") ?> · Powered by Seven Digital
    </div>

</div>

<script>
function checkNewPayments() {
    fetch("check_new_payments.php")
        .then(r => r.json())
        .then(data => {
            if (!data.success) return;
            const bal = document.getElementById("desktopBalance");
            if (bal && data.balance !== undefined) bal.textContent = "₭ " + data.balance;
        })
        .catch(() => {});
}
setInterval(checkNewPayments, 10000);
</script>
</body>
</html>