<?php
$translation = \DynCom\mysyde\common\classes\Registry::get('translation');
$adminMenu = \DynCom\mysyde\common\classes\Adminmenu::get(0);

function check_if_betriebsrat_role($admin_user)
{
    // Angepasste SQL-Abfrage, um nach is_betriebsrat = 1 zu filtern
    $query = "SELECT * FROM main_admin_user WHERE is_betriebsrat = 1 AND id = " . $admin_user;

    // Führe die Abfrage aus
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);

    // Prüfe, ob das Ergebnis mindestens eine Zeile hat
    if (@mysqli_num_rows($result) > 0) {
        // Wenn mindestens ein Eintrag gefunden wurde, gibt die Funktion 1 zurück
        return 1;
    } else {
        // Wenn kein Eintrag gefunden wurde, gibt die Funktion 0 zurück
        return 0;
    }
}

$is_betriebsrat = check_if_betriebsrat_role($admin_user['id']);



function create_collections_menu($site, $language, $translation)
{
    // Greife auf die globale Variable $is_betriebsrat zu
    global $is_betriebsrat;

    // Wenn $is_betriebsrat gleich 1 ist, gib "Test" aus und beende die Funktion
    if ($is_betriebsrat == 1) {
        ?>
        <li class="structure_nav">
            <a class="<?php echo (isset($_GET["level_2"]) && $_GET["level_2"] == $row['id']) ? 'active' : ''; ?>"
                href="/mysyde/awo/de/collections/17/">Aktuelles</a>
            <a class="<?php echo (isset($_GET["level_2"]) && $_GET["level_2"] == $row['id']) ? 'active' : ''; ?>"
                href="/mysyde/awo/de/knowledge/category/">Wiki Kategorien</a>
            <a class="<?php echo (isset($_GET["level_2"]) && $_GET["level_2"] == $row['id']) ? 'active' : ''; ?>"
                href="/mysyde/awo/de/collections/3/">Wiki Beiträge</a>

        </li>
        <?php
        return; // Beende die Funktion
    }

    // Andernfalls, führe die normale Logik zur Menüerstellung aus
    $query = "SELECT * FROM main_collection_setup WHERE (main_language_id = " . (int) $GLOBALS["language"]['id'] . " OR all_languages = 1) AND show_in_menu = 1";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);

    // Prüfe, ob das Ergebnis mindestens eine Zeile hat
    if (@mysqli_num_rows($result) == 0) {
        return; // Wenn kein Ergebnis vorhanden ist, beende die Funktion
    }

    // Schleife durch die Ergebnisse und erstelle das Menü
    while ($row = @mysqli_fetch_assoc($result)) {
        ?>
        <li class='structure_nav'>
            <a class="<?php echo (isset($_GET["level_2"]) && $_GET["level_2"] == $row['id']) ? 'active' : ''; ?>"
                href="<?php echo get_menu_link('collections/' . $row['id'], $site, $language); ?>"><?php echo $row['description'] ?></a>
        </li>
    <?php
    }
}

// var_dump($GLOBALS);




function create_module_menu($site, $language, $translation, $adminMenu)
{
    global $is_betriebsrat;

    // Wenn $is_betriebsrat gleich 1 ist, gib "Test" aus und beende die Funktion
    if ($is_betriebsrat == 1) {
        return; // Beende die Funktion
    }

    $query = "SELECT * FROM main_module WHERE active = 1 AND code != 'basic'";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) == 0) {
        return;
    }
    $i = 1;
    $count = @mysqli_num_rows($result);

    while ($i <= $count) {
        $row = @mysqli_fetch_assoc($result);
        $second_menu = FALSE;
        if (isset($adminMenu[$row['code']])) {
            if (isset($adminMenu[$row['code']]['subsites'])) {
                $second_menu = TRUE;
            }
        }

        $href = '#';

        if ($second_menu == TRUE) { ?>
            <li class='structure_nav toggle_li parent_dropdown'>
                <a class=" <?php echo (isset($_GET["level_1"]) && $_GET["level_1"] == $row['code']) ? 'active' : ''; ?>"
                    href="<?= $href ?>"><?php echo $row['title'] ?></a>
                <ul class='site_dropdown_new'>
                    <?php
                    $x = 0;
                    foreach ($adminMenu[$row['code']]['subsites'] as $subsite) {
                        $key = array_keys($adminMenu[$row['code']]['subsites']);
                        ?>
                        <li class=''>
                            <a class=" <?php echo (isset($_GET["level_2"]) && $_GET["level_2"] == $key[$x]) ? 'active' : ''; ?>"
                                href="<?php echo get_menu_link($row['code'] . "/" . $key[$x], $site, $language); ?>"><?php echo $subsite['name'] ?></a>
                        </li>
                        <?php
                        if ($adminMenu[$row['code']]['name'] == 'Firmenwiki') { ?>
                            <li class=''>
                                <a class=" <?php echo (isset($_GET["level_2"]) && $_GET["level_2"] == $key[$x]) ? 'active' : ''; ?>"
                                    href="<?php echo get_menu_link('collections/3', $site, $language); ?>">Beiträge</a>
                            </li>
                        <?php }
                        $x++;
                    }
                    ?>
                </ul>
            </li>
        <?php } else { ?>
            <li class='structure_nav toggle_li'>
                <a class=" <?php echo (isset($_GET["level_1"]) && $_GET["level_1"] == $row['code']) ? 'active' : ''; ?>"
                    href="<?php echo get_menu_link($row['code'], $site, $language); ?>"><?php echo $row['title'] ?></a>
            </li>
        <?php }
        $i++;
    }
}

?>

<nav>
    <ul>
        <div class='main_menu main_navigation'>

            <!-- Kollektionen -->
            <div class="group-wrapper collections ">
                <?php
                if ($is_betriebsrat == 0) {
                    ?>
                    <li class="structure_nav group small">
                        <a href="<?= get_menu_link('collections', $site, $language); ?>"
                            class="<?= (isset($_GET["level_1"]) && $_GET["level_1"] == "collections") ? 'active' : ''; ?>"><svg
                                xmlns="http://www.w3.org/2000/svg" width="14.701" height="20.582"
                                viewBox="0 0 14.701 20.582">
                                <g id="kollektion" transform="translate(2139 296)">
                                    <path id="Pfad_1" data-name="Pfad 1"
                                        d="M21.23,7.617,16.085,2.471a.667.667,0,0,0-.514-.22H8.22a1.474,1.474,0,0,0-1.47,1.47V21.363a1.474,1.474,0,0,0,1.47,1.47H19.981a1.474,1.474,0,0,0,1.47-1.47V8.131a.668.668,0,0,0-.22-.514Zm-5.66-3.6,4.116,4.116H15.571Zm4.41,17.347H8.22V3.72H14.1v4.41a1.474,1.474,0,0,0,1.47,1.47h4.41Z"
                                        transform="translate(-2145.75 -298.251)" fill="#fff" />
                                    <path id="Pfad_2" data-name="Pfad 2" d="M11.25,24.75h8.821v1.47H11.25Z"
                                        transform="translate(-2147.31 -306.049)" fill="#fff" />
                                    <path id="Pfad_3" data-name="Pfad 3" d="M11.25,18h8.821v1.47H11.25Z"
                                        transform="translate(-2147.31 -303.71)" fill="#fff" />
                                </g>
                            </svg><?php echo $translation->get("intranet_posts"); ?>
                        </a>
                    </li>
                    <?php
                }
                ?>
                <?php create_collections_menu($site, $language, $translation); ?>
            </div>

            <!-- Module -->
            <div class="group-wrapper module">
                <li class="structure_nav group small toggle_trigger">
                    <a href="#"
                        class="<?php echo (isset($_GET["level_1"]) && $_GET["level_1"] == "module") ? 'active' : ''; ?>"><svg
                            xmlns="http://www.w3.org/2000/svg" width="36" height="35.827" viewBox="0 0 36 35.827">
                            <g id="box-seam" transform="translate(0 -0.173)">
                                <path id="Pfad_26" data-name="Pfad 26"
                                    d="M18.419,2.5a1.125,1.125,0,0,0-.837,0L4.154,7.875l5.409,2.162L23.409,4.5Zm8.019,3.208L12.591,11.25,18,13.412,31.846,7.875,26.437,5.713Zm7.313,3.825-14.625,5.85V33.212l14.625-5.85V9.54ZM16.875,33.215V15.386L2.25,9.538V27.365ZM16.747.414a3.375,3.375,0,0,1,2.507,0l16.04,6.417A1.125,1.125,0,0,1,36,7.875v19.49a2.25,2.25,0,0,1-1.415,2.088L18.419,35.919a1.125,1.125,0,0,1-.837,0L1.417,29.453A2.25,2.25,0,0,1,0,27.365V7.875A1.125,1.125,0,0,1,.707,6.831Z"
                                    fill-rule="evenodd" />
                            </g>
                        </svg>
                        <?php echo $adminMenu['module']['name']; ?></a>
                </li>
                <div class="toggle_menu">
                    <li class="structure_nav toggle_li">
                        <a class="<?php echo (isset($_GET["level_1"]) && $_GET["level_1"] == "contactform") ? 'active' : ''; ?>"
                            href="<?php echo get_menu_link('contactform', $site, $language); ?>"><?php echo $translation->get('left_contactforms'); ?></a>
                    </li>
                    <li class="structure_nav toggle_li">
                        <a class="<?php echo (isset($_GET["level_1"]) && $_GET["level_1"] == "photo_approve") ? 'active' : ''; ?>"
                            href="<?php echo get_menu_link('photo_approve', $site, $language); ?>"><?php echo $translation->get('photo_approve'); ?></a>
                    </li>
                    <?php create_module_menu($site, $language, $translation, $adminMenu); ?>
                </div>

            </div>
            <?php
            if ($is_betriebsrat == 0) {
                ?>
                <div class='group-wrapper module'>
                    <li class="structure_nav group small toggle_trigger">
                        <a class='cms' href="#"
                            class="<?php echo (isset($_GET["level_1"]) && $_GET["level_1"] == "intranet") ? 'active' : ''; ?>">
                            <svg xmlns="http://www.w3.org/2000/svg" width="31.5" height="31.5" viewBox="0 0 31.5 31.5">
                                <g id="dashboard" transform="translate(-2.25 -2.25)">
                                    <path id="Pfad_135" data-name="Pfad 135" d="M27,23.625h2.25V29.25H27Z" />
                                    <path id="Pfad_136" data-name="Pfad 136" d="M22.5,18h2.25V29.25H22.5Z" />
                                    <path id="Pfad_137" data-name="Pfad 137"
                                        d="M12.375,29.25A5.632,5.632,0,0,1,6.75,23.625H9a3.375,3.375,0,1,0,3.375-3.375V18a5.625,5.625,0,0,1,0,11.25Z" />
                                    <path id="Pfad_138" data-name="Pfad 138"
                                        d="M31.5,2.25H4.5A2.252,2.252,0,0,0,2.25,4.5v27A2.252,2.252,0,0,0,4.5,33.75h27a2.253,2.253,0,0,0,2.25-2.25V4.5A2.252,2.252,0,0,0,31.5,2.25Zm0,10.125H15.75V4.5H31.5ZM13.5,4.5v7.875h-9V4.5Zm-9,27V14.625h27l0,16.875Z" />
                                </g>
                            </svg>
                            <?php echo $translation->get("intranet_basics") ?></a>
                    </li>
                    <div class="toggle_menu">
                        <li class="structure_nav toggle_li">
                            <a class="<?php echo (isset($_GET["level_2"]) && $_GET["level_2"] == "config_contact") ? 'active' : ''; ?>"
                                href="<?php echo get_menu_link('intranet/config_contact', $site, $language); ?>"><?php echo $translation->get('top_contact'); ?></a>
                        </li>
                        <li class="structure_nav toggle_li">
                            <a class="<?php echo (isset($_GET["level_2"]) && $_GET["level_2"] == "config_mandant") ? 'active' : ''; ?>"
                                href="<?php echo get_menu_link('intranet/config_mandant', $site, $language); ?>"><?php echo $translation->get('top_mandant'); ?></a>
                        </li>
                        <li class="structure_nav toggle_li">
                            <a class="<?php echo (isset($_GET["level_2"]) && $_GET["level_2"] == "config_department") ? 'active' : ''; ?>"
                                href="<?php echo get_menu_link('intranet/config_department', $site, $language); ?>"><?php echo $translation->get('top_department'); ?></a>
                        </li>
                        <li class="structure_nav toggle_li">
                            <a class="<?php echo (isset($_GET["level_2"]) && $_GET["level_2"] == "config_space") ? 'active' : ''; ?>"
                                href="<?php echo get_menu_link('intranet/config_space', $site, $language); ?>"><?php echo $translation->get('top_space'); ?></a>
                        </li>
                        <li class="structure_nav toggle_li">
                            <a class="<?php echo (isset($_GET["level_2"]) && $_GET["level_2"] == "config_facility") ? 'active' : ''; ?>"
                                href="<?php echo get_menu_link('intranet/config_facility', $site, $language); ?>"><?php echo $translation->get('Einrichtungen'); ?></a>
                        </li>
                        <li class="structure_nav toggle_li">
                            <a class="<?php echo (isset($_GET["level_2"]) && $_GET["level_2"] == "config_role") ? 'active' : ''; ?>"
                                href="<?php echo get_menu_link('intranet/config_role', $site, $language); ?>"><?php echo $translation->get('top_role'); ?></a>
                        </li>



                    </div>
                    </li>
                </div>
                <?php
            }
            ?>
            <!-- CMS-System -->
            <li class="structure_nav group small toggle_trigger">
                <a class="cms" href="#"
                    class="<?php echo (isset($_GET["level_1"]) && $_GET["level_1"] == "structure") ? 'active' : ''; ?>">
                    <svg xmlns="http://www.w3.org/2000/svg" width="19.548" height="19.557" viewBox="0 0 19.548 19.557">
                        <g id="struktur" transform="translate(2141 225)">
                            <path id="Pfad_4" data-name="Pfad 4" d="M10.4,7.448a.7.7,0,1,1-.7-.7A.7.7,0,0,1,10.4,7.448Z"
                                transform="translate(-2145.811 -228.957)" fill="#fff" />
                            <path id="Pfad_5" data-name="Pfad 5"
                                d="M19.7,16.31V13.42a1.4,1.4,0,0,0-1.4-1.4H12.722V9.231h6.283a1.4,1.4,0,0,0,1.4-1.4V3.646a1.4,1.4,0,0,0-1.4-1.4H5.042a1.4,1.4,0,0,0-1.4,1.4V7.835a1.4,1.4,0,0,0,1.4,1.4h6.283v2.793H5.741a1.4,1.4,0,0,0-1.4,1.4v2.89a2.793,2.793,0,1,0,1.4,0V13.42h5.585v2.793H9.231V21.8h5.585V16.213H12.722V13.42h5.585v2.89a2.793,2.793,0,1,0,1.4,0Zm-13.265,2.7a1.4,1.4,0,1,1-1.4-1.4A1.4,1.4,0,0,1,6.439,19.005Zm6.981-1.4V20.4H10.627V17.609ZM5.042,7.835V3.646H19.005V7.835ZM19.005,20.4a1.4,1.4,0,1,1,1.4-1.4A1.4,1.4,0,0,1,19.005,20.4Z"
                                transform="translate(-2143.25 -227.25)" fill="#fff" />
                        </g>
                    </svg>
                    <?php echo $translation->get("cms") ?></a>
            </li>
            <div class="toggle_menu">
                <li class="structure_nav toggle_li">
                    <a class="<?php echo (isset($_GET["level_2"]) && $_GET["level_2"] == "sites") ? 'active' : ''; ?>"
                        href="<?php echo get_menu_link('structure/sites', $site, $language); ?>"><?php echo $translation->get('left_sites'); ?></a>
                </li>
                <?php
                if ($is_betriebsrat == 0) {
                    ?>
                    <li class="structure_nav toggle_li">
                        <a class="<?php echo (isset($_GET["level_2"]) && $_GET["level_2"] == "navigation") ? 'active' : ''; ?>"
                            href="<?php echo get_menu_link('structure/navigation', $site, $language); ?>"><?php echo $translation->get('left_navigation'); ?></a>
                    </li>
                    <li class="structure_nav toggle_li">
                        <a class="<?php echo (isset($_GET["level_2"]) && $_GET["level_2"] == "components") ? 'active' : ''; ?>"
                            href="<?php echo get_menu_link('structure/components', $site, $language); ?>"><?php echo $translation->get('left_components'); ?></a>
                    <li class="structure_nav toggle_li">
                        <a class="<?php echo (isset($_GET['level_2']) && $_GET['level_2'] == 'files') ? 'active' : ''; ?>"
                            href="<?php echo get_menu_link('structure/files', $site, $language); ?>"><?php echo $translation->get('left_files'); ?></a>
                    </li>
                    <li class="structure_nav toggle_li">
                        <a class="<?php echo (isset($_GET['level_2']) && $_GET['level_2'] == 'files') ? 'active' : ''; ?>"
                            href="<?php echo get_menu_link('structure/video_uploud', $site, $language); ?>"><?php echo $translation->get('video_uploud'); ?></a>
                    </li>
                    <?php
                }
                ?>
            </div>
        </div>

        <div class='main_menu'>
            <div class="sitelanguage-version"><?php echo current_website_language($site, $language); ?>
                <div class="version">v.23.1</div>
            </div>
            <div class="theme-switch">
                <div>
                    <input type="checkbox" class="checkbox" id="theme-toggle" />
                    <label for="theme-toggle"></label>
                </div>
            </div>

        </div>
        <?php
        if ($is_betriebsrat == 0) {
            ?>
            <div class="menu-wrapper">

                <li class="topmenu gray_arrow" id="user_menu">
                    <a href="#"><?php echo $translation->get("signed_user") . ": " . $admin_user["name"]; ?></i></a>
                    <ul>
                        <li class="menu_userdata">
                            <a
                                href="<?php echo get_menu_link('config/config_users', $site, $language, "?action=open_card&input_id=" . $admin_user['id']); ?>"><?php echo $translation->get('my_account'); ?></a>
                        </li>

                        <li class="spacer">&nbsp;</li>

                        <li class="menu_link_logout">
                            <a href="?action=admin_logout"><?php echo $translation->get('top_logout'); ?></a>
                        </li>
                    </ul>
                </li>

                <li class="topmenu" id="settings_menu">
                    <a href="#"><i class="bi bi-sliders"></i></a>
                    <ul>
                        <li>
                            <a
                                href="<?php echo get_menu_link('config/config_websites', $site, $language); ?>"><?php echo $translation->get('top_websites'); ?></a>
                        </li>

                        <li>
                            <a
                                href="<?php echo get_menu_link('config/config_language', $site, $language); ?>"><?php echo $translation->get('top_languages'); ?></a>
                        </li>

                        <li>
                            <a
                                href="<?php echo get_menu_link('config/config_users', $site, $language); ?>"><?php echo $translation->get('top_users'); ?></a>
                        </li>

                        <li>
                            <a
                                href="<?php echo get_menu_link('config/config_layouts', $site, $language); ?>"><?php echo $translation->get('top_layouts'); ?></a>
                        </li>

                        <li>
                            <a
                                href="<?php echo get_menu_link('config/config_collections', $site, $language); ?>"><?php echo $translation->get('top_collections_config'); ?></a>
                        </li>

                        <li>
                            <a
                                href="<?php echo get_menu_link('config/config_url_management', $site, $language); ?>"><?php echo $translation->get('url_management'); ?></a>
                        </li>

                        <li>
                            <a
                                href="<?php echo get_menu_link('config/sitemap', $site, $language); ?>"><?php echo $translation->get('sitemap'); ?></a>
                        </li>
                    </ul>
                </li>

                <li class="topmenu" id="site_language_menu">
                    <?php echo $translation->get("top_config_websites") ?>
                    <ul class="site_dropdown">
                        <li>
                            <?php $siteLanguage = get_site_language_menu($site, $language); ?>
                        </li>
                        <li>
                            <?php echo $siteLanguage['siteLanguageMenu']; ?>
                        </li>
                    </ul>
                </li>
                <?php
        }
        ?>

            <!-- <li class="topmenu stats">
                <a href="<?php echo get_menu_link('statistics', $site, $language); ?>" class="<?php echo (isset($_GET["level_1"]) && $_GET["level_1"] == "statistics") ? 'active' : ''; ?>">

                <?php echo $adminMenu['statistics']['name']; ?></a>
                <ul class="site_dropdown statistics">
                    <li>
                        <a class="<?php echo (isset($_GET["level_2"]) && $_GET["level_2"] == "mail_stat") ? 'active' : ''; ?>" href="<?php echo get_menu_link('statistics/mail_stat', $site, $language); ?>"><?php echo $translation->get('left_mail_stat'); ?></a>
                    </li>
                    <li>
                        <a class="<?php echo (isset($_GET["level_2"]) && $_GET["level_2"] == "access_stat") ? 'active' : ''; ?>" href="<?php echo get_menu_link('statistics/access_stat', $site, $language); ?>"><?php echo $translation->get('left_access_stat'); ?></a>
                    </li>
                </ul>
            </li> -->
    </ul>
</nav>

<script>
    // Dark Mode Light Mode Switch Header
    var toggle = document.getElementById("theme-toggle");

    var storedTheme = localStorage.getItem('theme') || (window.matchMedia("(prefers-color-scheme: light)").matches ? "dark" : "light");
    if (storedTheme)
        document.documentElement.setAttribute('data-theme', storedTheme)

    toggle.onclick = function () {
        var currentTheme = document.documentElement.getAttribute("data-theme");
        var targetTheme = "light";

        if (currentTheme === "light") {
            targetTheme = "dark";
        }

        document.documentElement.setAttribute('data-theme', targetTheme)
        localStorage.setItem('theme', targetTheme);
    };

    const menuToggle = document.querySelector('.menu-toggle');

    menuToggle.addEventListener('click', () => {
        const isOpened = menuToggle.getAttribute('aria-expanded') === "true";
        isOpened ? closeMenu() : openMenu();
    });

    function openMenu() {
        menuToggle.setAttribute('aria-expanded', "true");
    }
    function closeMenu() {
        menuToggle.setAttribute('aria-expanded', "false");
    }

    function saveMenuState() {
        const openMenus = [];
        document.querySelectorAll('li.structure_nav.group.small').forEach((el, index) => {
            if (el.classList.contains('rotate_chevron')) {
                openMenus.push(index);
            }
        });
        localStorage.setItem('openMenus', JSON.stringify(openMenus));
    }

function restoreMenuState() {
    const saved = localStorage.getItem('openMenus');
    let openMenus;
    
    if (saved === null) {
        openMenus = [];
        document.querySelectorAll('li.structure_nav.group.small').forEach((el, index) => {
            openMenus.push(index);
        });
        localStorage.setItem('openMenus', JSON.stringify(openMenus));
    } else {
        openMenus = JSON.parse(saved);
    }

    document.querySelectorAll('li.structure_nav.group.small').forEach((el, index) => {
        if (openMenus.includes(index)) {
            el.classList.add('rotate_chevron');
            $(el).next().children(".toggle_li").show();
        }
    });
}

    restoreMenuState();

    $("li.structure_nav.group.small").click(function () {
        $(this).toggleClass("rotate_chevron");
        $(this).next().children(".toggle_li").toggle();
        saveMenuState();
    });

    function saveDropdownState() {
        const openDropdowns = [];
        document.querySelectorAll('.parent_dropdown').forEach((el, index) => {
            if ($(el).children(".site_dropdown_new").is(":visible")) {
                openDropdowns.push(index);
            }
        });
        localStorage.setItem('openDropdowns', JSON.stringify(openDropdowns));
    }

    function restoreDropdownState() {
        const openDropdowns = JSON.parse(localStorage.getItem('openDropdowns') || '[]');
        document.querySelectorAll('.parent_dropdown').forEach((el, index) => {
            if (openDropdowns.includes(index)) {
                $(el).children(".site_dropdown_new").show();
            }
        });
    }

    restoreDropdownState();

    $(".parent_dropdown").click(function () {
        $(this).children(".site_dropdown_new").toggle();
        saveDropdownState();
    });

    function fixNavHeight() {
        const nav = document.querySelector('.main_navigation');
        const bottomBar = document.querySelector('.main_menu:nth-child(2)');
        if (!nav || !bottomBar) return;
        const bottomBarHeight = bottomBar.offsetHeight;
        nav.style.height = (window.innerHeight - 160 - bottomBarHeight) + 'px';
    }

    fixNavHeight();
    window.addEventListener('resize', fixNavHeight);
</script>