<?php

declare(strict_types=1);
// Variables: $baseUrl, $assetsUrl, $project, $user, $navItems, $tables, $miscConfig, $smsDriverList, $pageTitle
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
    <title><?= htmlspecialchars($project['name'] ?? 'Project') ?> - Misc</title>
    <?php include constant('BASE_PATH') . '/core/views/project-head.php'; ?>
    <link rel="stylesheet" href="<?= htmlspecialchars($assetsUrl) ?>/css/module-guide.css?v=<?= (int) @filemtime(constant('BASE_PATH') . '/public/assets/css/module-guide.css') ?>">
    <?= \PpPhp\Core\ModuleAssets::style('Misc', 'Views/styles.css') ?>
</head>

<body class="misc-module">
    <?php include __DIR__ . '/partials/shell.php'; ?>

    <div id="mainArea" class="main-area">
        <?php if (empty($standalone)): ?>
        <div class="left-panel misc-table-panel">
            <div class="panel-header misc-panel-header">
                <div>
                    <span class="misc-panel-kicker">Data source</span>
                    <strong>Tables</strong>
                </div>
                <span class="misc-table-count"><?= count($tables) ?></span>
            </div>
            <ul class="table-list" id="miscTableList">
                <?php foreach ($tables as $t): ?>
                    <li data-table="<?= htmlspecialchars($t['name']) ?>">
                        <i class="bi bi-<?= $t['type'] === 'VIEW' ? 'eye' : 'table' ?> me-1"></i>
                        <span><?= htmlspecialchars($t['name']) ?></span>
                    </li>
                <?php endforeach; ?>
            </ul>
        </div>
        <?php endif; ?>

        <div class="right-workspace">
            <div class="toolbar" id="miscToolbar">
                <button class="btn btn-sm btn-outline-secondary d-md-none me-1" id="btnToggleMiscList" title="Show Tables">
                    <i class="bi bi-list"></i> Tables
                </button>
                <span class="misc-toolbar-icon"><i class="bi bi-sliders2"></i></span>
                <span class="misc-toolbar-context">
                    <small>Application settings</small>
                    <strong id="selectedTableLabel">Project Settings</strong>
                </span>
                <button type="button" class="pp-guide-trigger ms-auto" data-bs-toggle="modal" data-bs-target="#miscGuideModal" title="Open the complete Misc module guide" aria-label="Open the Misc module guide">
                    <i class="bi bi-question-circle"></i><span>Guide</span>
                </button>
                <span class="misc-toolbar-hint d-none d-sm-inline"><i class="bi bi-cloud-check"></i> Changes save automatically</span>
            </div>
            <div class="workspace-content" id="miscWorkspace">
                <?php $__misc_standalone = !empty($standalone); ?>
                <ul class="nav nav-tabs small" id="miscTabs" role="tablist">
                    <li class="nav-item"><button class="nav-link active" data-bs-toggle="tab" data-bs-target="#projectPane" type="button">Project</button></li>
                    <?php if (!$__misc_standalone): ?>
                    <li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#tablePane" type="button">Table</button></li>
                    <li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#advancedPane" type="button">Advanced</button></li>
                    <li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#emailPane" type="button">Email</button></li>
                    <li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#smsPane" type="button">SMS</button></li>
                    <li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#aiPane" type="button">AI</button></li>
                    <li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#labelsPane" type="button">Label Editor</button></li>
                    <?php endif; ?>
                </ul>
                <div class="tab-content mt-2" id="miscTabContent">
                    <div class="tab-pane fade show active" id="projectPane"><?php include __DIR__ . '/partials/project_settings.php'; ?></div>
                    <?php if (!$__misc_standalone): ?>
                    <div class="tab-pane fade" id="tablePane"><?php include __DIR__ . '/partials/table_settings.php'; ?></div>
                    <div class="tab-pane fade" id="advancedPane"><?php include __DIR__ . '/partials/advanced_settings.php'; ?></div>
                    <div class="tab-pane fade" id="emailPane"><?php include __DIR__ . '/partials/email_settings.php'; ?></div>
                    <div class="tab-pane fade" id="smsPane"><?php include __DIR__ . '/partials/sms_settings.php'; ?></div>
                    <div class="tab-pane fade" id="aiPane"><?php include __DIR__ . '/partials/ai_settings.php'; ?></div>
                    <div class="tab-pane fade" id="labelsPane"><?php include __DIR__ . '/partials/labels_editor.php'; ?></div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <?php include __DIR__ . '/partials/languages_modal.php'; ?>
    </div>
    <?php include __DIR__ . '/partials/guide.php'; ?>

    <!-- Variable injection FIRST -->
    <script>
        const projectSlug = <?= json_encode($project['slug'] ?? '') ?>;
        const baseUrl = <?= json_encode($baseUrl) ?>;
        const dbTables = <?= json_encode($tables) ?>;
        const miscConfig = <?= json_encode($miscConfig ?? []) ?>;
        const smsDrivers = <?= json_encode($smsDriverList ?: new stdClass()) ?>;
        const tablesWithPageTypes = <?= $tablesWithPageTypesJson ?>;
        const pagesConfig = <?= json_encode($pagesConfig ?? []) ?>;
        const uiStringGroups = <?= json_encode($uiStringGroups ?? new stdClass()) ?>;
        const menuItems = <?= json_encode($menuItems ?? new stdClass()) ?>;
    </script>

    <?php include constant('BASE_PATH') . '/core/views/project-scripts.php'; ?>
    <script src="<?= htmlspecialchars($assetsUrl) ?>/js/module-guide.js"></script>


    <!-- Modular scripts (order matters: core first) -->
    <?= \PpPhp\Core\ModuleAssets::script('Misc', 'Views/scripts/core.js') ?>
    <?= \PpPhp\Core\ModuleAssets::script('Misc', 'Views/scripts/languages.js') ?>
    <?= \PpPhp\Core\ModuleAssets::script('Misc', 'Views/scripts/project.js') ?>
    <?= \PpPhp\Core\ModuleAssets::script('Misc', 'Views/scripts/email.js') ?>
    <?= \PpPhp\Core\ModuleAssets::script('Misc', 'Views/scripts/sms.js') ?>
    <?= \PpPhp\Core\ModuleAssets::script('Misc', 'Views/scripts/ai.js') ?>
    <?= \PpPhp\Core\ModuleAssets::script('Misc', 'Views/scripts/advanced.js') ?>
    <?= \PpPhp\Core\ModuleAssets::script('Misc', 'Views/scripts/table.js') ?>
    <?= \PpPhp\Core\ModuleAssets::script('Misc', 'Views/scripts/labels.js') ?>


    <!-- Bootstrap the page -->
    <script>
        document.addEventListener('DOMContentLoaded', async () => {
            initMobileToggle();
            await loadFullConfig();

            // Initialise all tabs
            populateProjectTab();
            initLanguages();
            populateAdvancedTab();
            populateEmailTab();
            populateSmsTab();
            if (typeof populateAiTab === 'function') populateAiTab();
            initLabelsTab();

            // Add "what does this do?" tooltips to every control.
            if (typeof initMiscTooltips === 'function') initMiscTooltips();

            // Deep-link: /misc#advancedPane (etc.) opens that tab directly.
            const hash = (location.hash || '').replace('#', '');
            if (hash) {
                const trigger = document.querySelector('[data-bs-target="#' + hash + '"]');
                if (trigger && window.bootstrap && bootstrap.Tab) {
                    bootstrap.Tab.getOrCreateInstance(trigger).show();
                }
            }
        });
    </script>
</body>

</html>
