<?php
require_once(MODULE_PATH . "collection/collection_config.inc.php");

$siteparts     = \DynCom\mysyde\common\classes\Siteparts::get();
$pageIds       = array();
$sitepartCount = array();
$formname      = "form_edit_collection";
$translation   = \DynCom\mysyde\common\classes\Registry::get("translation");

$action = (isset($_GET['action']) ? $_GET['action'] : "");

function countCollection($setup_id){
    $query  = "SELECT id FROM main_collection WHERE main_collection_setup_id = ".$setup_id;
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    $num_rows = @mysqli_num_rows($result);
    return $num_rows;
}
function loadWorlds(){
    $query    = "SELECT * FROM main_collection_setup_world ORDER BY sorting ASC";
    $result   = @mysqli_query($GLOBALS['mysql_con'], $query);
    while ($row = @mysqli_fetch_array($result, 1)) {
        echo "<div class='pop-up-element'>";
        $query_setup    = "SELECT main_collection_setup.* FROM main_collection_setup LEFT JOIN main_collection_setup_worlds_link ON main_collection_setup_worlds_link.main_collection_setup_id = main_collection_setup.id WHERE (main_language_id = " . (int)$GLOBALS["language"]['id'] . " OR all_languages = 1) AND main_collection_setup_world_id = ".$row['id']." ORDER BY main_collection_setup.id ASC";
        $result_setup   = @mysqli_query($GLOBALS['mysql_con'], $query_setup);
        while ($row_setup = @mysqli_fetch_array($result_setup, 1)) {
            ?>
            <a class="dashoard_box button_inhalt_collection"
               href="<?php echo get_menu_link($_GET["level_1"] . "/" . $row_setup['id'], $GLOBALS['site'], $GLOBALS['language']); ?>">
                <span
                    class="dashboard_box_number"><?php echo countCollection($row_setup['id']); ?></span>
                <span class="dashboard_box_description"><?php echo $row_setup['description']; ?></span>
                <img
                    src="<?php echo PATH_SETUP_ICON_COLLECTION . ($row_setup['icon'] != "" ? $row_setup['icon'] : 'dokumente_2.png'); ?>" />
            </a>
        <?php
        }
        echo "</div>";
    }
}

function allCollections(){
    echo "<div class='pop-up-element'>";
    $query_setup    = "SELECT * FROM main_collection_setup WHERE (main_language_id = " . (int)$GLOBALS["language"]['id'] . " OR all_languages = 1) ORDER BY main_collection_setup.id ASC";
    $result_setup   = @mysqli_query($GLOBALS['mysql_con'], $query_setup);
    while ($row_setup = @mysqli_fetch_array($result_setup, 1)) {
        ?>
        <a class="dashoard_box button_inhalt_collection"
           href="<?php echo get_menu_link($_GET["level_1"] . "/" . $row_setup['id'], $GLOBALS['site'], $GLOBALS['language']); ?>">
            <span
                class="dashboard_box_number"><?php echo countCollection($row_setup['id']); ?></span>
            <span class="dashboard_box_description"><?php echo $row_setup['description']; ?></span>
            <img
                src="<?php echo PATH_SETUP_ICON_COLLECTION . ($row_setup['icon'] != "" ? $row_setup['icon'] : 'dokumente_2.png'); ?>" />
        </a>
    <?php
    }
    echo "</div>";
}

function loadWorldButtons(){
    $query    = "SELECT * FROM main_collection_setup_world ORDER BY sorting ASC";
    $result   = @mysqli_query($GLOBALS['mysql_con'], $query);
    echo "<div class='pop-up-button'>Alle</div>";
    while ($row = @mysqli_fetch_array($result, 1)) {
        echo "<div class='pop-up-button'>".$row['description']."</div>";
    }
}
?>


<div id="mainContent" class="w-100">
    <div class="collection-content dashboard-left-content">
        <img class="mysyde-icon img-fluid"src="/layout/admin/img/icons/mysyde-icon.svg" alt="MySyde Icon">
        <div class="text-content">
            <div class="text-white"><?php echo current_website_language($site, $language); ?></div>
            <h1 class="new-h1 text-white"><?php echo $translation->get("intranet_posts"); ?></h1>
            <p class="text-white">Sie sind das Herz und die Niere einer jeden Content Welt</p>
        </div>
        <img src="/layout/admin/img/icons/mysyde-bg.jpg" alt="" class="module-content-img hintergrund module-hintergrund position-absolute top-0 left-0 z-index-1">
    </div>
    <div class="collection_overview">
        <div class='collection_world_buttons'>
            <?php loadWorldButtons(); ?>
        </div>
        <div class='collection_world_content'>
            <?php allCollections();?>
            <?php loadWorlds();?>
        </div>
    </div>
</div>

<script>
    
// Popup Element automatisches öffnen
var popupButton = document.getElementsByClassName("pop-up-button");
for (let i = 0; i < popupButton.length; i++) {
    popupButton[i].setAttribute("data-button", i);
}

var popupElement = document.getElementsByClassName("pop-up-element");
$(".pop-up-element").hide();
for (let i = 0; i < popupElement.length; i++) {
    popupElement[i].setAttribute("data-element", i);
}

$(".pop-up-button").click(function() {

    let buttonID = this.getAttribute("data-button");

    $(".pop-up-element").each(function() {

        let elementID = this.getAttribute("data-element");
        $(".pop-up-button").removeClass('current');

        if (buttonID == elementID) {
            $(this).animate({ 'opacity': 'show' }, 500);
        } else {
            $(this).hide();
        }

    });
    $(this).addClass('current');
});


$(".pop-up-element:first-child").show();
$(".pop-up-button:first-child").addClass("current");
$(".pop-up-element:first-child").addClass("current");
</script>