<?php
/* ============================================================================
 *  admin/module_soon.php — admin placeholder for a module under development.
 *  Lists where the module is wired and which companies have it enabled.
 *  Usage: module_soon.php?m=stock
 * ==========================================================================*/
session_start();
if (!isset($_SESSION["user_id"]) || $_SESSION["role"] !== "admin") {
    header("Location: ../auth/login.php"); exit;
}
require_once '../config/database.php';
require_once '../config/modules.php';

$mods = available_modules();
$key  = $_GET['m'] ?? '';
$m    = $mods[$key] ?? ['icon'=>'🚧','en'=>'Module','lo'=>'ໂມດູນ','desc_en'=>'','desc_lo'=>''];

/* companies that have this module enabled */
$companies = [];
if ($key) {
    try {
        $st = $pdo->prepare("SELECT c.name FROM company_modules cm JOIN companies c ON c.id=cm.company_id
                             WHERE cm.module_key=? ORDER BY c.name");
        $st->execute([$key]);
        $companies = array_column($st->fetchAll(PDO::FETCH_ASSOC), 'name');
    } catch (Throwable $e) {}
}
?>
<!DOCTYPE html>
<html lang="lo">
<head>
    <meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= htmlspecialchars($m['en']) ?> — Module</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:#2563eb;--surface:#fff;--bg:#f0f4f8;--border:#e2e8f0;--text:#0f172a;--muted:#64748b;}
        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:12px 24px;display:flex;justify-content:space-between;align-items:center;gap:12px;position:sticky;top:0;z-index:100;box-shadow:0 1px 6px rgba(15,23,42,.06);}
        .back-link{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-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;}
        .wrap{max-width:680px;margin:auto;padding:30px 20px;}
        .card{background:var(--surface);border:1px solid var(--border);border-radius:18px;padding:34px 28px;box-shadow:0 2px 14px rgba(15,23,42,.09);text-align:center;}
        .ic{font-size:60px;margin-bottom:12px;}
        .ft{font-size:24px;font-weight:800;}
        .cs{display:inline-block;background:#fef3c7;color:#92400e;font-weight:800;font-size:13px;padding:5px 14px;border-radius:999px;margin:12px 0;}
        .desc{font-size:14px;color:var(--muted);margin-bottom:18px;line-height:1.6;}
        .info{background:#f8fafc;border:1px solid var(--border);border-radius:12px;padding:16px;text-align:left;font-size:13px;color:var(--muted);line-height:1.8;margin-top:18px;}
        .info b{color:var(--text);}
        .chips{display:flex;flex-wrap:wrap;gap:6px;margin-top:8px;}
        .chip{background:#dbeafe;color:#1e40af;font-size:12px;font-weight:700;padding:4px 10px;border-radius:999px;}
        code{background:#eef2f6;padding:2px 6px;border-radius:6px;font-size:12px;}
    </style>
</head>
<body>

<div class="top-bar">
    <a class="back-link" href="dashboard.php">← <span data-lo="ກັບໜ້າຫຼັກ" data-en="Dashboard">Dashboard</span></a>
    <div style="display:flex;gap:7px;">
        <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="wrap">
    <div class="card">
        <div class="ic"><?= $m['icon'] ?></div>
        <div class="ft"><?= htmlspecialchars($m['en']) ?></div>
        <div class="cs" data-lo="🚧 ກຳລັງພັດທະນາ" data-en="🚧 In development">🚧 In development</div>
        <div class="desc"><?= htmlspecialchars($m['desc_en']) ?></div>

        <div class="info">
            <div><b data-lo="ລະຫັດໂມດູນ" data-en="Module key">Module key</b>: <code><?= htmlspecialchars($key) ?></code></div>
            <div style="margin-top:8px;"><b data-lo="ບໍລິສັດທີ່ເປີດໃຊ້" data-en="Companies using it">Companies using it</b>:</div>
            <?php if ($companies): ?>
                <div class="chips"><?php foreach ($companies as $c): ?><span class="chip"><?= htmlspecialchars($c) ?></span><?php endforeach; ?></div>
            <?php else: ?>
                <div data-lo="ຍັງບໍ່ມີບໍລິສັດໃດເປີດໃຊ້." data-en="No company has enabled it yet.">No company has enabled it yet.</div>
            <?php endif; ?>
            <div style="margin-top:12px;" data-lo="ເພື່ອພັດທະນາ: ສ້າງໜ້າຈິງ ແລ້ວປ່ຽນລິ້ງໃນ company/module_soon.php ໄປຫາໜ້ານັ້ນ."
                 data-en="To build it: create the real page, then point company/module_soon.php for this key to it.">
                To build it: create the real page, then point <code>company/module_soon.php</code> for this key to it.
            </div>
        </div>
    </div>
</div>

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