<?php
$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
$formname    = "form_infoboard_list";
$inputname   = "input_id";
global $filter_mandant;
$filter_mandant = (isset($_REQUEST['filter']) ? $_REQUEST['filter'] : 0);
?>
<ul class="toolbar_menu">
    <?= button("edit", $translation->get("new_meeting"), $formname, "loadSite('".get_menu_link('collections/4', $site, $language, '?action=open_card_collection')."')"); ?>
    <?= button("edit", $translation->get("new_post"), $formname, "loadCard('new_infoboard', 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_infoboard'); ?></h1>
    
    <div class='infoboard_form'>
        <?= load_infoboard()?>
    </div>

    <div class="infoboard_add">
        <form method='post' name='<?= $formname ?>' id='<?= $formname ?>'>
            <div class="form_title_and_close">
                <h3><?php echo get_translation('add_person'); ?></h3>
                <div class='close'></div>
            </div>
            <input type='hidden' name='input_date' value=''>
            <input type='hidden' name='input_category' value=''>
            <input type='hidden' name='input_mandant' value=''>
            <?= select_contact()?>
            <ul class="toolbar_menu new w-auto">
            <?= button("save", $translation->get("save_and_close"), $formname, "loadCard('save', true, '', true)"); ?>
            </ul>
        </form>
    </div>
</div>

<script>
    $(".infoboard_person").click(function(){
        contact = $(this).data('contact');
        date = $(this).data('date');
        category = $(this).data('category');
        mandant = $(this).data('mandant');

        $("input[name='input_date']").val(date);
        $("input[name='input_category']").val(category);
        $("input[name='input_mandant']").val(mandant);


        $(".infoboard_add").show();

        $('.contact.ui.fluid.multiple.dropdown').dropdown('clear'); 
        $('.contact.ui.fluid.multiple.dropdown').dropdown('set selected', contact); 
    });

    $(".infoboard_add .close").click(function(){
        $(".infoboard_add").hide();
    });
    
    $(".infoboard_person").each(function() {
        $(this).on("click", function() {
            if ($(this).hasClass("person_active")) {
                $(this).removeClass("person_active");
            } else {
                $(".infoboard_person").removeClass("person_active");
                $(this).addClass("person_active");
            }
        });
    });

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