<?php
session_start();

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

require_once '../config/database.php';
require_once '../config/modules.php';

$companyId   = (int)$_SESSION["company_id"];
$companyName = $_SESSION["company_name"] ?? "My Company";
$enabledModules = company_enabled_modules($pdo, $companyId);

/* ── Live counts ── */
function safeCount($pdo, $sql, $params = []) {
    try {
        $st = $pdo->prepare($sql);
        $st->execute($params);
        return (int)$st->fetchColumn();
    } catch (Throwable $e) { return 0; }
}

function safeVal($pdo, $sql, $params = [], $default = 0) {
    try {
        $st = $pdo->prepare($sql);
        $st->execute($params);
        return $st->fetchColumn() ?? $default;
    } catch (Throwable $e) { return $default; }
}

$totalStaff    = safeCount($pdo, "SELECT COUNT(*) FROM company_staff cs JOIN users u ON u.id = cs.user_id WHERE cs.company_id = ?", [$companyId]);
$pendingLeave  = safeCount($pdo, "SELECT COUNT(*) FROM hr_leave WHERE company_id = ? AND status = 'pending'", [$companyId]);
$todayPresent  = safeCount($pdo, "SELECT COUNT(*) FROM hr_attendance WHERE company_id = ? AND work_date = CURDATE() AND status = 'present'", [$companyId]);
$creditBalance = safeVal($pdo,   "SELECT credit_balance FROM companies WHERE id = ?", [$companyId], 0);

/* Current month payroll total */
$currentMonth  = date('n');
$currentYear   = date('Y');
$monthPayroll  = safeVal($pdo,
    "SELECT COALESCE(SUM(net_pay),0) FROM hr_salary WHERE company_id = ? AND month = ? AND year = ?",
    [$companyId, $currentMonth, $currentYear], 0
);
?>
<!DOCTYPE html>
<html lang="lo">
<head>
    <?php $pwaTheme = '#0f766e'; include __DIR__ . '/../config/pwa_head.php'; ?>
    <title><?= htmlspecialchars($companyName) ?> — Company Dashboard</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>
    <script src="../assets/push.js" defer></script>
    <style>
        *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

        :root {
            --primary:    #0f766e;
            --primary-dk: #084740;
            --green:      #16a34a;
            --green-lt:   #dcfce7;
            --amber:      #d97706;
            --amber-lt:   #fef3c7;
            --red:        #dc2626;
            --red-lt:     #fee2e2;
            --blue:       #2563eb;
            --blue-lt:    #dbeafe;
            --purple:     #7c3aed;
            --purple-lt:  #ede9fe;
            --teal:       #0f766e;
            --teal-lt:    #ccfbf1;
            --surface:    #ffffff;
            --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(--surface);
            border-bottom: 1px solid var(--border);
            padding: calc(12px + env(safe-area-inset-top, 0px)) 32px 12px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            gap: 16px;
            position: sticky; top: 0; z-index: 100;
            box-shadow: 0 1px 6px rgba(15,23,42,.06);
            flex-wrap: wrap;
        }

        .top-bar-brand {
            display: flex; align-items: center; gap: 10px;
        }

        .brand-logo {
            width: 34px; height: 34px;
            background: linear-gradient(135deg, #084740, #0f766e);
            border-radius: 9px;
            display: flex; align-items: center; justify-content: center;
            color: #fff; font-weight: 800; font-size: 14px;
        }

        .brand-name {
            font-size: 14px; font-weight: 700; color: var(--text);
            line-height: 1.2;
        }

        .brand-sub {
            font-size: 11px; color: var(--muted);
        }

        .top-bar-right {
            display: flex; align-items: center; gap: 12px;
        }

        .lang-bar {
            display: flex; align-items: center; gap: 6px;
        }

        .lang-label { font-size: 12px; color: var(--muted); font-weight: 500; }

        .lang-btn {
            display: inline-flex; align-items: center; justify-content: center; gap: 4px;
            height: 36px; padding: 0 14px; border-radius: 999px;
            border: 1.5px solid var(--border);
            background: var(--surface);
            font-size: 12px; font-weight: 700; color: var(--muted);
            cursor: pointer; font-family: inherit; line-height: 1;
            transition: border-color .15s, color .15s, background .15s;
        }

        .lang-btn:hover  { border-color: var(--primary); color: var(--primary); }
        .lang-btn.active { background: var(--primary); border-color: var(--primary); color: #fff; }

        .avatar {
            width: 34px; height: 34px;
            background: linear-gradient(135deg, #084740, #0f766e);
            border-radius: 50%;
            display: flex; align-items: center; justify-content: center;
            color: #fff; font-weight: 800; font-size: 14px;
        }

        .user-info { font-size: 13px; color: var(--muted); line-height: 1.3; }
        .user-info strong { display: block; color: var(--text); font-size: 13px; }

        .logout-link {
            display: inline-flex; align-items: center; justify-content: center; gap: 5px;
            height: 36px; padding: 0 16px;
            background: var(--red-lt); color: var(--red);
            border: 1.5px solid #fca5a5;
            border-radius: 999px;
            text-decoration: none; font-size: 12px; font-weight: 700; line-height: 1;
            transition: background .15s, color .15s;
        }

        .logout-link:hover { background: var(--red); color: #fff; }

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

        /* ── Hero ── */
        .hero {
            background: linear-gradient(135deg, #042f2e 0%, #084740 50%, #0f766e 100%);
            border-radius: 20px;
            padding: 32px 36px;
            margin-bottom: 28px;
            color: #fff;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 20px;
            box-shadow: 0 8px 32px rgba(4,47,46,.22);
            position: relative;
            overflow: hidden;
        }

        .hero::before, .hero::after {
            content: "";
            position: absolute;
            border-radius: 50%;
            opacity: .07;
            background: #fff;
        }

        .hero::before { width: 300px; height: 300px; right: -70px; top: -90px; }
        .hero::after  { width: 180px; height: 180px; right: 130px; bottom: -70px; }

        .hero-left h1 {
            font-size: 28px; font-weight: 800; line-height: 1.2; margin-bottom: 6px;
        }

        .hero-left .subtitle {
            font-size: 13px; color: rgba(255,255,255,.7);
        }

        .hero-left .welcome {
            font-size: 14px; color: rgba(255,255,255,.88);
            font-weight: 600; margin-top: 8px;
        }

        .hero-badges {
            display: flex; gap: 12px; flex-wrap: wrap;
            position: relative; z-index: 1;
        }

        .hero-badge {
            background: rgba(255,255,255,.12);
            border: 1px solid rgba(255,255,255,.2);
            border-radius: 12px;
            padding: 14px 20px;
            text-align: center;
            min-width: 130px;
        }

        .hero-badge .val {
            font-size: 26px; font-weight: 800; color: #fff;
            display: block; margin-bottom: 3px;
        }

        .hero-badge .lbl {
            font-size: 12px; color: rgba(255,255,255,.75);
        }

        /* ── Stat row ── */
        .stat-row {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
            gap: 14px;
            margin-bottom: 28px;
        }

        .stat-card {
            background: var(--surface);
            border-radius: var(--radius);
            padding: 18px 20px;
            box-shadow: var(--shadow);
            border: 1px solid var(--border);
            display: flex;
            align-items: center;
            gap: 14px;
        }

        .stat-icon {
            width: 48px; height: 48px; border-radius: 12px;
            display: flex; align-items: center; justify-content: center;
            font-size: 22px; flex-shrink: 0;
        }

        .stat-icon.teal   { background: var(--teal-lt); }
        .stat-icon.green  { background: var(--green-lt); }
        .stat-icon.amber  { background: var(--amber-lt); }
        .stat-icon.blue   { background: var(--blue-lt); }
        .stat-icon.purple { background: var(--purple-lt); }

        .stat-info .val  { font-size: 26px; font-weight: 800; color: var(--text); line-height: 1; }
        .stat-info .lbl  { font-size: 12px; color: var(--muted); font-weight: 600; margin-top: 3px; text-transform: uppercase; letter-spacing: .4px; }

        /* ── Section title ── */
        .section-title {
            font-size: 12px; font-weight: 700; color: var(--muted);
            text-transform: uppercase; letter-spacing: .6px;
            margin-bottom: 14px; margin-top: 4px;
        }

        /* ── Menu grid ── */
        .menu-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
            gap: 14px;
            margin-bottom: 28px;
        }

        .menu-card {
            background: var(--surface);
            border-radius: var(--radius);
            padding: 22px;
            box-shadow: var(--shadow);
            text-decoration: none;
            color: var(--text);
            border: 1px solid var(--border);
            display: flex;
            flex-direction: column;
            gap: 10px;
            transition: transform .18s, box-shadow .18s, border-color .18s;
            position: relative;
            overflow: hidden;
        }

        .menu-card::before {
            content: "";
            position: absolute;
            top: 0; left: 0; right: 0; height: 3px;
            background: var(--accent, var(--teal));
            opacity: 0;
            transition: opacity .18s;
        }

        .menu-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 10px 30px rgba(15,23,42,.13);
            border-color: #99f6e4;
        }

        .menu-card:hover::before { opacity: 1; }

        .menu-card .card-icon {
            width: 44px; height: 44px; border-radius: 12px;
            display: flex; align-items: center; justify-content: center;
            font-size: 22px; background: var(--icon-bg, var(--teal-lt));
        }

        .menu-card h3 { font-size: 15px; font-weight: 700; color: var(--text); line-height: 1.3; }
        .menu-card p  { font-size: 12px; color: var(--muted); line-height: 1.55; }

        .card-teal   { --accent: #0f766e; --icon-bg: #ccfbf1; }
        .card-blue   { --accent: #2563eb; --icon-bg: #dbeafe; }
        .card-green  { --accent: #16a34a; --icon-bg: #dcfce7; }
        .card-amber  { --accent: #d97706; --icon-bg: #fef3c7; }
        .card-purple { --accent: #7c3aed; --icon-bg: #ede9fe; }
        .card-pink   { --accent: #db2777; --icon-bg: #fce7f3; }
        .card-gray   { --accent: #475569; --icon-bg: #f1f5f9; }

        /* Pending badge */
        .pending-dot {
            position: absolute; top: 12px; right: 12px;
            background: var(--red); color: #fff;
            font-size: 14px; font-weight: 900;
            min-width: 24px; height: 24px; line-height: 24px; text-align: center;
            padding: 0 7px; border-radius: 999px;
            box-shadow: 0 2px 6px rgba(220,38,38,.45);
        }
        /* notification highlight: whole card light red */
        .menu-card.notif { background:#fee2e2 !important; border-color:#fca5a5 !important; }

        /* ── Credit balance card ── */
        .credit-card {
            background: linear-gradient(135deg, #042f2e, #0f766e);
            border-radius: var(--radius);
            padding: 22px 26px;
            color: #fff;
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 20px;
            margin-bottom: 28px;
            box-shadow: 0 4px 20px rgba(4,47,46,.2);
        }

        .credit-left .credit-lbl {
            font-size: 12px; color: rgba(255,255,255,.7);
            text-transform: uppercase; letter-spacing: .5px; font-weight: 700;
        }

        .credit-left .credit-val {
            font-size: 32px; font-weight: 800; color: #fff;
            margin-top: 4px; letter-spacing: -.5px;
        }

        .credit-left .credit-sub {
            font-size: 12px; color: rgba(255,255,255,.6); margin-top: 4px;
        }

        .credit-icon { font-size: 42px; opacity: .6; }

        /* ── Footer ── */
        .footer-row {
            display: flex; align-items: center;
            justify-content: space-between;
            flex-wrap: wrap; gap: 10px;
            padding-top: 10px;
            border-top: 1px solid var(--border);
            font-size: 12px; color: var(--muted);
        }

        @media (max-width: 700px) {
            .page-body { padding: 16px; }
            .top-bar   { padding: calc(10px + env(safe-area-inset-top, 0px)) 16px 10px; }
            .hero      { padding: 22px 20px; }
            .hero-left h1 { font-size: 20px; }
            .credit-left .credit-val { font-size: 24px; }
        }
    </style>
</head>
<body>

<?php $isDesktopUA = !preg_match('/android|iphone|ipad|ipod|blackberry|windows phone|mobile/i', $_SERVER["HTTP_USER_AGENT"] ?? ""); ?>
<!-- ── Top bar ── -->
<div class="top-bar">
    <div class="top-bar-brand">
        <?php if (!$isDesktopUA): /* mobile only — on desktop the company name already shows in the hero */ ?>
        <a href="../customer/dashboard.php" title="Back to wallet" aria-label="Back to wallet"
           style="display:inline-flex;align-items:center;justify-content:center;width:40px;height:40px;border-radius:10px;background:#f1f5f9;border:1.5px solid var(--border);color:var(--text);text-decoration:none;font-size:20px;font-weight:800;flex-shrink:0;">←</a>
        <div class="brand-logo">CO</div>
        <div>
            <div class="brand-name"><?= htmlspecialchars($companyName) ?></div>
            <div class="brand-sub">Company Portal</div>
        </div>
        <?php endif; ?>
    </div>

    <div class="top-bar-right">
        <div class="lang-bar">
            <span class="lang-label">ພາສາ:</span>
            <button class="lang-btn" id="btn-lo" onclick="setLang('lo')">🇱🇦 ລາວ</button>
            <button class="lang-btn" id="btn-en" onclick="setLang('en')">🇬🇧 EN</button>
        </div>

        <?php if (!$isDesktopUA): ?>
        <div style="display:flex;align-items:center;gap:8px;">
            <div class="avatar"><?= strtoupper(mb_substr($_SESSION["name"] ?? "C", 0, 1)) ?></div>
            <div class="user-info">
                <strong><?= htmlspecialchars($_SESSION["name"]) ?></strong>
                <span data-lo="ຜູ້ດູແລບໍລິສັດ" data-en="Company Admin">ຜູ້ດູແລບໍລິສັດ</span>
            </div>
        </div>
        <?php endif; ?>

        <a class="logout-link" href="../auth/logout.php">
            🚪 <span data-lo="ອອກ" data-en="Logout">ອອກ</span>
        </a>
    </div>
</div>

<div class="page-body">

    <!-- ── Hero ── -->
    <div class="hero">
        <div class="hero-left">
            <h1><?= htmlspecialchars($companyName) ?></h1>
            <div class="subtitle" data-lo="ລະບົບ HR ແລະ ເຄຣດິດ" data-en="HR & Credit Management Portal">
                HR &amp; Credit Management Portal
            </div>
            <div class="welcome">
                👋 <span data-lo="ຍິນດີຕ້ອນຮັບ," data-en="Welcome,">ຍິນດີຕ້ອນຮັບ,</span>
                <?= htmlspecialchars($_SESSION["name"]) ?>
            </div>
            <div style="margin-top:12px;display:flex;gap:8px;flex-wrap:wrap;">
                <button onclick="sdEnablePush()" style="background:rgba(255,255,255,.18);color:#fff;border:1.5px solid rgba(255,255,255,.35);padding:8px 14px;border-radius:10px;font-weight:800;font-size:13px;cursor:pointer;font-family:inherit;">
                    🔔 <span data-lo="ເປີດການແຈ້ງເຕືອນ" data-en="Enable alerts">Enable alerts</span>
                </button>
                <button onclick="sdTestPush()" style="background:rgba(255,255,255,.10);color:#fff;border:1.5px solid rgba(255,255,255,.25);padding:8px 14px;border-radius:10px;font-weight:700;font-size:13px;cursor:pointer;font-family:inherit;">
                    <span data-lo="ທົດສອບ" data-en="Test">Test</span>
                </button>
            </div>
        </div>

        <div class="hero-badges">
            <div class="hero-badge">
                <span class="val"><?= number_format($totalStaff) ?></span>
                <span class="lbl" data-lo="ພະນັກງານ" data-en="Staff">ພະນັກງານ</span>
            </div>
            <div class="hero-badge">
                <span class="val"><?= number_format($todayPresent) ?></span>
                <span class="lbl" data-lo="ມາວັນນີ້" data-en="Present today">ມາວັນນີ້</span>
            </div>
            <?php if ($pendingLeave > 0): ?>
            <div class="hero-badge" style="background:rgba(220,38,38,.25);border-color:rgba(220,38,38,.4);">
                <span class="val"><?= $pendingLeave ?></span>
                <span class="lbl" data-lo="ຂໍລາພັກລໍຖ້າ" data-en="Leave pending">ຂໍລາພັກລໍຖ້າ</span>
            </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- ── Credit balance card ── -->
    <div class="credit-card">
        <div class="credit-left">
            <div class="credit-lbl" data-lo="ຍອດເຄຣດິດຄົງເຫຼືອ" data-en="Available Credit Balance">
                ຍອດເຄຣດິດຄົງເຫຼືອ
            </div>
            <div class="credit-val">
                ₭ <?= number_format((float)$creditBalance, 0) ?>
            </div>
            <div class="credit-sub" data-lo="ໂອນໂດຍ Seven Digital Admin" data-en="Funded by Seven Digital Admin">
                ໂອນໂດຍ Seven Digital Admin
            </div>
        </div>
        <div class="credit-icon">💰</div>
    </div>

    <!-- ── Stats ── -->
    <div class="stat-row">
        <div class="stat-card">
            <div class="stat-icon teal">👥</div>
            <div class="stat-info">
                <div class="val"><?= number_format($totalStaff) ?></div>
                <div class="lbl" data-lo="ພະນັກງານທັງໝົດ" data-en="Total Staff">ພະນັກງານທັງໝົດ</div>
            </div>
        </div>

        <div class="stat-card">
            <div class="stat-icon green">✅</div>
            <div class="stat-info">
                <div class="val"><?= number_format($todayPresent) ?></div>
                <div class="lbl" data-lo="ເຂົ້າວຽກມື້ນີ້" data-en="Present Today">ເຂົ້າວຽກມື້ນີ້</div>
            </div>
        </div>

        <div class="stat-card">
            <div class="stat-icon amber">📋</div>
            <div class="stat-info">
                <div class="val"><?= number_format($pendingLeave) ?></div>
                <div class="lbl" data-lo="ຂໍລາພັກລໍຖ້າ" data-en="Leave Requests">ຂໍລາພັກລໍຖ້າ</div>
            </div>
        </div>

        <div class="stat-card">
            <div class="stat-icon blue">💵</div>
            <div class="stat-info">
                <div class="val">₭ <?= number_format((float)$monthPayroll, 0) ?></div>
                <div class="lbl" data-lo="ເງິນເດືອນເດືອນນີ້" data-en="This Month Payroll">ເງິນເດືອນເດືອນນີ້</div>
            </div>
        </div>
    </div>

    <?php if (module_on($enabledModules,'staff')): ?>
    <!-- ── Staff Management module ── -->
    <div class="section-title" data-lo="ຈັດການພະນັກງານ" data-en="Staff Management">ຈັດການພະນັກງານ / Staff Management</div>
    <div class="menu-grid">
        <a class="menu-card card-teal" href="staff.php">
            <div class="card-icon">👥</div>
            <h3 data-lo="ພະນັກງານ" data-en="Staff List">ພະນັກງານ</h3>
            <p  data-lo="ເບິ່ງ, ເພີ່ມ, ແກ້ໄຂ ແລະ ລຶບພະນັກງານ" data-en="View, add, edit and remove staff members.">ເບິ່ງ, ເພີ່ມ, ແກ້ໄຂ ແລະ ລຶບພະນັກງານ</p>
        </a>
        <a class="menu-card card-purple" href="approval_settings.php">
            <div class="card-icon">✅</div>
            <h3 data-lo="ການຕັ້ງຄ່າການອະນຸມັດ" data-en="Approval Settings">ການຕັ້ງຄ່າການອະນຸມັດ</h3>
            <p  data-lo="ກຳນົດຫົວໜ້າພະແນກ ແລະ ຜູ້ຈັດການຝ່າຍປະຕິບັດການ ສຳລັບອະນຸມັດ ລາພັກ/ກູ້ຢືມ/OT" data-en="Set department managers and the Operation Manager who approves leave, loans and OT.">ກຳນົດຫົວໜ້າພະແນກ ແລະ ຜູ້ຈັດການຝ່າຍປະຕິບັດການ</p>
        </a>
    </div>
    <?php endif; ?>

    <?php if (module_on($enabledModules,'customer')): ?>
    <!-- ── Customer Management module ── -->
    <div class="section-title" data-lo="ຈັດການລູກຄ້າ" data-en="Customer Management">ຈັດການລູກຄ້າ / Customer Management</div>
    <div class="menu-grid">
        <a class="menu-card card-blue" href="sp_customers.php">
            <div class="card-icon">👤</div>
            <h3 data-lo="ລູກຄ້າ" data-en="Customers">Customers</h3>
            <p  data-lo="ລູກຄ້າ, ເຄື່ອງຈັກ ແລະ ສັນຍາ" data-en="Customers, machines & contracts.">ລູກຄ້າ, ເຄື່ອງຈັກ ແລະ ສັນຍາ</p>
        </a>
        <a class="menu-card card-gray" href="module_access_form.php?m=customer">
            <div class="card-icon">🔑</div>
            <h3 data-lo="ກຳນົດສິດ" data-en="Assign Access">Assign Access</h3>
            <p  data-lo="ເລືອກໃຜແກ້ໄຂໄດ້ ຫຼື ເບິ່ງຢ່າງດຽວ" data-en="Choose who can manage vs view only.">ເລືອກໃຜແກ້ໄຂໄດ້ ຫຼື ເບິ່ງຢ່າງດຽວ</p>
        </a>
    </div>
    <?php endif; ?>

    <?php if (module_on($enabledModules,'stock')): ?>
    <!-- ── Stock Management module ── -->
    <div class="section-title" data-lo="ຈັດການສະຕັອກ" data-en="Stock Management">ຈັດການສະຕັອກ / Stock Management</div>
    <div class="menu-grid">
        <a class="menu-card card-purple" href="sp_stock.php">
            <div class="card-icon">📦</div>
            <h3 data-lo="ສະຕັອກ" data-en="Stock">Stock</h3>
            <p  data-lo="ສິນຄ້າຄົງຄັງ, ເຂົ້າ-ອອກ" data-en="Inventory & stock in/out.">ສິນຄ້າຄົງຄັງ, ເຂົ້າ-ອອກ</p>
        </a>
        <a class="menu-card card-gray" href="module_access_form.php?m=stock">
            <div class="card-icon">🔑</div>
            <h3 data-lo="ກຳນົດສິດ" data-en="Assign Access">Assign Access</h3>
            <p  data-lo="ເລືອກໃຜແກ້ໄຂໄດ້ ຫຼື ເບິ່ງຢ່າງດຽວ" data-en="Choose who can manage vs view only.">ເລືອກໃຜແກ້ໄຂໄດ້ ຫຼື ເບິ່ງຢ່າງດຽວ</p>
        </a>
    </div>
    <?php endif; ?>

    <?php if (module_on($enabledModules,'jobs')): ?>
    <!-- ── Job Management module ── -->
    <div class="section-title" data-lo="ຈັດການວຽກບໍລິການ" data-en="Job Management">ຈັດການວຽກບໍລິການ / Job Management</div>
    <div class="menu-grid">
        <a class="menu-card card-blue" href="sp_jobs_admin.php">
            <div class="card-icon">📊</div>
            <h3 data-lo="ໜ້າຫຼັກ" data-en="Dashboard">Dashboard</h3>
            <p  data-lo="ພາບລວມວຽກ, ສະຖິຕິ ແລະ ເປີດວຽກ" data-en="Jobs overview, stats & Open Job.">ພາບລວມວຽກ, ສະຖິຕິ ແລະ ເປີດວຽກ</p>
        </a>
        <a class="menu-card card-teal" href="sp_jobs_admin.php">
            <div class="card-icon">🛠️</div>
            <h3 data-lo="ຈັດການວຽກ (Admin)" data-en="Manage Jobs">Manage Jobs</h3>
            <p  data-lo="ສ້າງ, ມອບໝາຍ ແລະ ຈັດການວຽກທັງໝົດ" data-en="Create, assign & manage all jobs.">ສ້າງ, ມອບໝາຍ ແລະ ຈັດການວຽກທັງໝົດ</p>
        </a>
        <a class="menu-card card-green" href="sp_jobs_staff.php">
            <div class="card-icon">✅</div>
            <h3 data-lo="ວຽກຂອງຂ້ອຍ" data-en="My Jobs">My Jobs</h3>
            <p  data-lo="ເບິ່ງ ແລະ ປິດວຽກຂອງທ່ານເອງ" data-en="View & close your own jobs.">ເບິ່ງ ແລະ ປິດວຽກຂອງທ່ານເອງ</p>
        </a>
        <a class="menu-card card-gray" href="module_access_form.php?m=jobs">
            <div class="card-icon">🔑</div>
            <h3 data-lo="ກຳນົດສິດ" data-en="Assign Access">Assign Access</h3>
            <p  data-lo="ເລືອກໃຜແກ້ໄຂໄດ້ ຫຼື ເບິ່ງຢ່າງດຽວ" data-en="Choose who can manage vs view only.">ເລືອກໃຜແກ້ໄຂໄດ້ ຫຼື ເບິ່ງຢ່າງດຽວ</p>
        </a>
        <a class="menu-card card-amber" href="sp_map_staff.php">
            <div class="card-icon">🔗</div>
            <h3 data-lo="ຈັບຄູ່ພະນັກງານເກົ່າ" data-en="Map Old Staff">Map Old Staff</h3>
            <p  data-lo="ໂອນວຽກຈາກຜູ້ໃຊ້ເກົ່າໄປຫາຜູ້ໃຊ້ໃໝ່" data-en="Transfer old staff's jobs to new users.">ໂອນວຽກຈາກຜູ້ໃຊ້ເກົ່າໄປຫາຜູ້ໃຊ້ໃໝ່</p>
        </a>
    </div>
    <?php endif; ?>

    <?php if (module_on($enabledModules,'part_request')): ?>
    <!-- ── Request Form module ── -->
    <div class="section-title" data-lo="ໃບຂໍ" data-en="Request Form">ໃບຂໍ / Request Form</div>
    <div class="menu-grid">
        <?php
            require_once '../config/part_request_flow.php';
            $__ready = function_exists('pr_ready_for_creator') ? pr_ready_for_creator($pdo,$companyId,(int)$_SESSION['user_id']) : 0;
        ?>
        <a class="menu-card card-amber<?= $__ready>0?' notif':'' ?>" href="sp_part_requests.php?status=ready" style="position:relative;">
            <?php if ($__ready>0): ?><span class="pending-dot"><?= $__ready ?></span><?php endif; ?>
            <div class="card-icon">🧾</div>
            <h3 data-lo="ໃບຂໍ (ສ້າງໃໝ່)" data-en="Request Form">Request Form</h3>
            <p  data-lo="ສ້າງໃບຂໍອາໄຫຼ່/ໝຶກ ໃໝ່" data-en="Create a new parts/toner request.">ສ້າງໃບຂໍອາໄຫຼ່/ໝຶກ ໃໝ່</p>
        </a>
        <?php
            $__prole=pr_role($pdo,$companyId,(int)$_SESSION['user_id'],$_SESSION['role']??'');
            $__newReq=pr_badge_count($pdo,$companyId,$__prole);
        ?>
        <a class="menu-card card-pink<?= $__newReq>0?' notif':'' ?>" href="sp_part_request_queue.php" style="position:relative;">
            <?php if ($__newReq>0): ?><span class="pending-dot" id="reqQueueDot"><?= $__newReq ?></span><?php endif; ?>
            <div class="card-icon">📥</div>
            <h3 data-lo="ຄິວໃບຂໍ" data-en="Request Form Queue">Request Form Queue</h3>
            <p  data-lo="ເບິ່ງ ແລະ ຈັດການໃບຂໍໃໝ່" data-en="View & handle new requests.">ເບິ່ງ ແລະ ຈັດການໃບຂໍໃໝ່</p>
        </a>
        <a class="menu-card card-gray" href="module_access_form.php?m=part_request">
            <div class="card-icon">🔑</div>
            <h3 data-lo="ກຳນົດສິດ" data-en="Assign Access">Assign Access</h3>
            <p  data-lo="ເລືອກໃຜຈ່າຍ (Stock Control) ຫຼື ຂໍຢ່າງດຽວ" data-en="Choose who fulfils vs request only.">ເລືອກໃຜຈ່າຍ ຫຼື ຂໍຢ່າງດຽວ</p>
        </a>
    </div>
    <?php endif; ?>

    <?php if (module_on($enabledModules,'attendance')): ?>
    <!-- ── Attendance Management module ── -->
    <div class="section-title" data-lo="ຈັດການການເຂົ້າວຽກ" data-en="Attendance Management">ການເຂົ້າວຽກ / Attendance Management</div>
    <div class="menu-grid">
        <a class="menu-card card-green" href="attendance_today.php">
            <div class="card-icon">📋</div>
            <h3 data-lo="ການເຂົ້າວຽກມື້ນີ້" data-en="Today's Check-in">ການເຂົ້າວຽກມື້ນີ້</h3>
            <p  data-lo="ເບິ່ງວ່າໃຜເຂົ້າແລ້ວ ແລະ ໃຜຍັງບໍ່ເຂົ້າ" data-en="See who has checked in today and who hasn't yet.">ເບິ່ງວ່າໃຜເຂົ້າແລ້ວ ແລະ ໃຜຍັງບໍ່ເຂົ້າ</p>
        </a>
        <a class="menu-card card-amber" href="attendance_log.php">
            <div class="card-icon">🕐</div>
            <h3 data-lo="ການເຂົ້າວຽກ & ຮູບ" data-en="Attendance & Selfies">ການເຂົ້າວຽກ & ຮູບ</h3>
            <p  data-lo="ເບິ່ງການລົງເວລາ 4 ຄັ້ງ, ຮູບ, ມາຊ້າ ແລະ ແກ້ໄຂ" data-en="View 4-punch records, selfies, late flags and edit.">ເບິ່ງການລົງເວລາ, ຮູບ, ມາຊ້າ ແລະ ແກ້ໄຂ</p>
        </a>
        <a class="menu-card card-gray" href="hr_attendance.php">
            <div class="card-icon">📝</div>
            <h3 data-lo="ບັນທຶກດ້ວຍມື" data-en="Manual Attendance">ບັນທຶກດ້ວຍມື</h3>
            <p  data-lo="ເພີ່ມ/ແກ້ໄຂການເຂົ້າວຽກແບບເກົ່າ" data-en="Add or edit attendance the classic way.">ເພີ່ມ/ແກ້ໄຂການເຂົ້າວຽກແບບເກົ່າ</p>
        </a>
        <a class="menu-card card-teal" href="attendance_settings.php">
            <div class="card-icon">⚙️</div>
            <h3 data-lo="ຕັ້ງຄ່າການເຂົ້າວຽກ" data-en="Attendance Settings">ຕັ້ງຄ່າການເຂົ້າວຽກ</h3>
            <p  data-lo="ກຳນົດສາຂາ (GPS), ກະວຽກ ແລະ ກົດລະບຽບເງິນເດືອນ" data-en="Set up branches (GPS), shifts and payroll rules.">ກຳນົດສາຂາ (GPS), ກະວຽກ ແລະ ກົດລະບຽບ</p>
        </a>
        <a class="menu-card card-blue" href="staff_attendance.php">
            <div class="card-icon">🧑‍💼</div>
            <h3 data-lo="ມອບໝາຍກະ/ສາຂາ" data-en="Staff Assignments">ມອບໝາຍກະ/ສາຂາ</h3>
            <p  data-lo="ກຳນົດກະ, ສາຂາ, ຄົນຍົກເວັ້ນ ແລະ ຕາຕະລາງວຽກ" data-en="Assign shifts, branches, exempt staff and roster.">ກຳນົດກະ, ສາຂາ, ຄົນຍົກເວັ້ນ ແລະ ຕາຕະລາງວຽກ</p>
        </a>
        <a class="menu-card card-amber" href="ot_approvals.php">
            <div class="card-icon">⏱️</div>
            <h3 data-lo="ອະນຸມັດ OT" data-en="OT Approvals">ອະນຸມັດ OT</h3>
            <p  data-lo="ກວດ ແລະ ອະນຸມັດໂມງລ່ວງເວລາຂອງພະນັກງານ" data-en="Review and approve staff overtime before it is paid.">ກວດ ແລະ ອະນຸມັດໂມງລ່ວງເວລາ</p>
        </a>
        <a class="menu-card card-blue" href="reminder_test.php">
            <div class="card-icon">🔔</div>
            <h3 data-lo="ທົດສອບການແຈ້ງເຕືອນ" data-en="Reminder Test">ທົດສອບການແຈ້ງເຕືອນ</h3>
            <p  data-lo="ສົ່ງທົດສອບ ແລະ ເບິ່ງວ່າໃຜເປີດ/ໄດ້ຮັບການແຈ້ງເຕືອນ" data-en="Send a test alert and see who has notifications enabled.">ສົ່ງທົດສອບ ແລະ ເບິ່ງວ່າໃຜໄດ້ຮັບ</p>
        </a>
    </div>
    <?php endif; ?>

    <?php if (module_on($enabledModules,'leave')): ?>
    <!-- ── Leave Management module ── -->
    <div class="section-title" data-lo="ຈັດການການລາພັກ" data-en="Leave Management">ການລາພັກ / Leave Management</div>
    <div class="menu-grid">
        <a class="menu-card card-green<?= $pendingLeave>0?' notif':'' ?>" href="hr_leave.php" style="position:relative;">
            <?php if ($pendingLeave > 0): ?><span class="pending-dot"><?= $pendingLeave ?></span><?php endif; ?>
            <div class="card-icon">🏖️</div>
            <h3 data-lo="ການລາພັກ" data-en="Leave Requests">ການລາພັກ</h3>
            <p  data-lo="ອະນຸມັດ ຫຼື ປະຕິເສດການຂໍລາພັກ" data-en="Approve or reject staff leave requests.">ອະນຸມັດ ຫຼື ປະຕິເສດການຂໍລາພັກ</p>
        </a>
        <a class="menu-card card-teal" href="leave_balances.php">
            <div class="card-icon">📅</div>
            <h3 data-lo="ໂຄຕາລາພັກ" data-en="Leave Balances">ໂຄຕາລາພັກ</h3>
            <p  data-lo="ກຳນົດວັນລາພັກປະຈຳປີ ແລະ ເບິ່ງໂຄຕາແຕ່ລະຄົນ" data-en="Set annual leave days and view each staff's balance.">ກຳນົດວັນລາພັກປະຈຳປີ ແລະ ເບິ່ງໂຄຕາ</p>
        </a>
    </div>
    <?php endif; ?>

    <?php if (!empty($enabledModules)): ?>
    <!-- ── Tools & Modules ── -->
    <div class="section-title" data-lo="ເຄື່ອງມື ແລະ ໂມດູນ" data-en="Tools & Modules">
        Tools &amp; Modules
    </div>
    <div class="menu-grid">
        <?php foreach (available_modules() as $mkey => $mod): ?>
            <?php if (!in_array($mkey, $enabledModules) || !empty($mod['built'])) continue; /* HR modules have their own sections */ ?>
            <a class="menu-card <?= $mod['color'] ?>" href="module_soon.php?m=<?= $mkey ?>" style="position:relative;">
                <span style="position:absolute;top:12px;right:12px;background:#fef3c7;color:#92400e;font-size:10px;font-weight:800;padding:2px 8px;border-radius:999px;"
                      data-lo="ໄວໆນີ້" data-en="Soon">Soon</span>
                <div class="card-icon"><?= $mod['icon'] ?></div>
                <h3 data-lo="<?= $mod['lo'] ?>" data-en="<?= $mod['en'] ?>"><?= $mod['en'] ?></h3>
                <p data-lo="<?= $mod['desc_lo'] ?>" data-en="<?= $mod['desc_en'] ?>"><?= $mod['desc_en'] ?></p>
            </a>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>

    <?php if (module_on($enabledModules,'payroll')): ?>
    <!-- ── Payroll Management module ── -->
    <div class="section-title" data-lo="ຈັດການເງິນເດືອນ" data-en="Payroll Management">ເງິນເດືອນ / Payroll Management</div>

    <div class="menu-grid">

        <a class="menu-card card-blue" href="hr_salary.php">
            <div class="card-icon">💵</div>
            <h3 data-lo="ເງິນເດືອນ" data-en="Salary">ເງິນເດືອນ</h3>
            <p  data-lo="ຕັ້ງ ແລະ ແກ້ໄຂຂໍ້ມູນເງິນເດືອນ, OT ພະນັກງານ"
                data-en="Set and edit staff base salary and OT records.">
                ຕັ້ງ ແລະ ແກ້ໄຂຂໍ້ມູນເງິນເດືອນ, OT ພະນັກງານ
            </p>
        </a>

        <a class="menu-card card-teal" href="payroll_generate.php">
            <div class="card-icon">⚡</div>
            <h3 data-lo="ສ້າງເງິນເດືອນອັດຕະໂນມັດ" data-en="Auto Payroll">ສ້າງເງິນເດືອນອັດຕະໂນມັດ</h3>
            <p  data-lo="ຄິດໄລ່ມາຊ້າ, ຂາດວຽກ ແລະ OT ຈາກການເຂົ້າວຽກ"
                data-en="Calculate late, absence and OT from attendance.">
                ຄິດໄລ່ມາຊ້າ, ຂາດວຽກ ແລະ OT ຈາກການເຂົ້າວຽກ
            </p>
        </a>

        <a class="menu-card card-purple" href="hr_allowances.php">
            <div class="card-icon">🎁</div>
            <h3 data-lo="ເບ້ຍຕ່າງໆ" data-en="Allowances">ເບ້ຍຕ່າງໆ</h3>
            <p  data-lo="ຈັດການເບ້ຍຂົນສົ່ງ, ທີ່ພັກ, ອາຫານ ແລະ ອື່ນໆ"
                data-en="Manage transport, housing, meal and other allowances.">
                ຈັດການເບ້ຍຂົນສົ່ງ, ທີ່ພັກ, ອາຫານ ແລະ ອື່ນໆ
            </p>
        </a>

        <a class="menu-card card-pink" href="hr_payslips.php">
            <div class="card-icon">🧾</div>
            <h3 data-lo="ໃບແຈ້ງເງິນເດືອນ" data-en="Payslips">ໃບແຈ້ງເງິນເດືອນ</h3>
            <p  data-lo="ສ້າງ ແລະ ສ່ງໃບແຈ້ງເງິນເດືອນລາຍເດືອນ"
                data-en="Generate and distribute monthly payslips.">
                ສ້າງ ແລະ ສ່ງໃບແຈ້ງເງິນເດືອນລາຍເດືອນ
            </p>
        </a>

        <a class="menu-card card-amber" href="loans.php">
            <div class="card-icon">💸</div>
            <h3 data-lo="ການກູ້ຢືມພະນັກງານ" data-en="Staff Loans">ການກູ້ຢືມພະນັກງານ</h3>
            <p  data-lo="ອະນຸມັດການກູ້ ແລະ ຫັກຈາກເງິນເດືອນອັດຕະໂນມັດ"
                data-en="Approve loans; auto-deduct from salary each cycle.">
                ອະນຸມັດການກູ້ ແລະ ຫັກອັດຕະໂນມັດ
            </p>
        </a>

    </div>
    <?php endif; ?>

    <!-- ── Credit & Reporting ── -->
    <div class="section-title" data-lo="ເຄຣດິດ ແລະ ລາຍງານ" data-en="Credit & Reports">
        ເຄຣດິດ &amp; ລາຍງານ
    </div>

    <div class="menu-grid">

        <a class="menu-card card-gray" href="credit_balance.php">
            <div class="card-icon">💰</div>
            <h3 data-lo="ຍອດເຄຣດິດ" data-en="Credit Balance">ຍອດເຄຣດິດ</h3>
            <p  data-lo="ເບິ່ງຍອດຄົງເຫຼືອ ແລະ ປະຫວັດການໂອນເຄຣດິດ"
                data-en="View balance and credit transfer history from Seven Digital.">
                ເບິ່ງຍອດຄົງເຫຼືອ ແລະ ປະຫວັດການໂອນເຄຣດິດ
            </p>
        </a>

    </div>

    <!-- ── Footer ── -->
    <div class="footer-row">
        <span><?= htmlspecialchars($companyName) ?> &copy; <?= date("Y") ?></span>
        <span style="color:var(--muted);font-size:11px;">
            Powered by <strong style="color:var(--primary);">Seven Digital</strong>
        </span>
    </div>

</div>

<script>
/* Live polling — pending leave badge every 10s */
const elPendingDot  = document.querySelector("a[href='hr_leave.php'] .pending-dot");
const elHeroPending = document.querySelector(".hero-badge:last-child .val");
const elStatPending = document.querySelectorAll(".stat-card")[2]?.querySelector(".val");

async function pollLeave() {
    try {
        const res  = await fetch("get_leave_count.php", { cache: "no-store" });
        const data = await res.json();
        if (!data.success) return;

        const n = data.pendingLeave;

        if (elStatPending) elStatPending.textContent = n.toLocaleString();

        /* Dot on leave card */
        const card = document.querySelector("a[href='hr_leave.php']");
        if (card) {
            let dot = card.querySelector(".pending-dot");
            if (n > 0) {
                if (!dot) { dot = document.createElement("span"); dot.className = "pending-dot"; card.appendChild(dot); }
                dot.textContent = n;
            } else if (dot) { dot.remove(); }
        }

        document.title = n > 0
            ? `(${n}) <?= htmlspecialchars($companyName) ?> Dashboard`
            : "<?= htmlspecialchars($companyName) ?> Dashboard";

    } catch (e) {}
}

pollLeave();
setInterval(pollLeave, 10000);
/* Auto-refresh so the Request Form Queue badge stays live (pause while typing) */
setInterval(function(){
    var a=document.activeElement;
    if(a && /^(INPUT|TEXTAREA|SELECT)$/.test(a.tagName)) return;
    if(!document.hidden) location.reload();
}, 20000);
</script>

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