<?php
$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
$formname    = "form_department_list";
$inputname   = "input_id";

global $filter_mandant;
$filter_mandant = (isset($_REQUEST['mandant_id']) ? $_REQUEST['mandant_id'] : 0);

function filter_mandant($filter){ ?>
    <h3><?php echo get_translation('filter_mandant'); ?>:</h3>
    <div class="input">
        <select id="filter_mandant" name="filter_mandant" class="bigselect" onchange="filter_mandant();">
            <option value="0"><?php echo get_translation('all_mandate'); ?></option>
            <?php
                $query = "SELECT * FROM organogramm_space";
                $result = @mysqli_query($GLOBALS['mysql_con'], $query);
                while($row = @mysqli_fetch_array($result)){ ?>
                    <option value="<?= $row['id'] ?>" <?= ($filter == $row['id'] ? 'selected="selected"' : ""); ?>><?= $row['description'] ?></option>
            <?php } ?>
        </select>
    </div>
<?php }
?>
    <ul class="toolbar_menu">
        <?= button("new", $translation->get("new"), $formname, "loadCard('new', true)"); ?>
        <div class="filter_mandant">
            <?= filter_mandant($filter_mandant) ?>
        </div>
    </ul>

<div id="mainContent">

    <?php echo current_website_language($site, $language); ?>
    <h1><?php echo get_translation('top_space'); ?></h1>

    <form id="<?= $formname ?>" name="<?= $formname ?>" method="post">
        <input type="hidden" class="selected_linklist_row" name="input_id" value="" />

        <div class="requestLoader"></div>
        <?
        $where = "";
        if($filter_mandant != 0){
            $where = "WHERE main_mandant_id = ".$filter_mandant;
        }
        $query  = "SELECT DISTINCT id, description AS '" . $translation->get("name") . "' FROM organogramm_space ORDER BY description ASC";
        $format = array("option", "text");
        if ($result = mysqli_query($GLOBALS['mysql_con'], $query)) {
            linklist($result, "form_department_list", $format);
        }
        ?>

    </form>
</div>

<script>
        function filter_mandant() {
        var filter = jQuery('#filter_mandant').val();
        const url = window.location.href.split('?')[0];
        var newUrl = url + '?mandant_id='+filter;
        window.location.href = newUrl;
    }
</script>