<?php
$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
$formname    = "form_collection_list";
$inputname   = "input_id";
$filter = (isset($_REQUEST['filter']) ? $_REQUEST['filter'] : "");
global $filter_mandant;
$filter_mandant = (isset($_REQUEST['mandant_id']) ? $_REQUEST['mandant_id'] : 0);

$collection_setup_info = get_collection_setup_info();

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 main_mandant";
                $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 }
 
?>

<script type="text/javascript">
    function filter_collection() {
        jQuery('#form_collection_list .requestLoader').show();

        var filter = jQuery('#collection_filter').val();

        jQuery('#form_collection_list').attr("action", "?filter=" + filter).submit();

        return;

        var formData = {
            filter_id: jQuery('#collection_filter').val(),
        };

        jQuery('#filter_collection_setup_id').val(jQuery('#collection_filter').val());
        $.ajax({
                   url    : "?action=filter_collection",
                   type   : 'POST',
                   data   : formData,
                   success: function ( output, status, xhr ) {
                       if (xhr.getResponseHeader('REQUIRES_AUTH') == 1) {
                           window.location.replace("/mysyde/");
                       } else {
                           jQuery('#form_collection_list .requestLoader').hide();
                           jQuery('#page_linklist').html(output);

                           initForm();
                           initLinklist();
                       }
                   }
               });
    }

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

<ul class="toolbar_menu">
    <?= button("new", $translation->get("new"), $formname, "loadCard('new_collection', true)"); ?>
    <!-- <?= button("edit", $translation->get("edit"), $formname, "loadCard('edit_collection')"); ?> -->
    <?php if(!$collection_setup_info['is_custom_collection']){ 
        //  echo button("new", $translation->get("mask"), $formname, "loadCard('mask_collection', true)");
        }
        ?>


    <li>
        <ul>
            <!-- <?= button("up", $translation->get("go_up"), $formname, "sendRequest('moveup_line_collection')", "", FALSE); ?>
            <?= button("down", $translation->get("go_down"), $formname, "sendRequest('movedown_line_collection')", "", FALSE); ?>
            <?= button("delete", $translation->get("delete"), $formname, "sendRequest('delete_collection', false, '{$translation->get('delete_collection_confirm')}')", "", FALSE); ?> -->
            <!-- <?= button("xml", $translation->get("generate_xml"), $formname, "sendRequest('generate_xml')", "", FALSE); ?> -->
        </ul>
    </li>
    <div class="filter-collection">
        <h3><?php echo get_translation('filter'); ?>:</h3>
        <div class="input">
            <select id="collection_filter" name="collection_filter" class="bigselect" onchange="filter_collection();">
                <option value="0"><?php echo get_translation('collection_filter_active'); ?></option>
                <option value="past"   <?php echo ($filter == "past" ? 'selected="selected"' : ""); ?>><?php echo get_translation('collection_filter_past'); ?></option>
                <option value="future" <?php echo ($filter == "future" ? 'selected="selected"' : ""); ?>><?php echo get_translation('collection_filter_future'); ?></option>
                <option value="all"    <?php echo ($filter == "all" ? 'selected="selected"' : ""); ?>><?php echo get_translation('collection_filter_all'); ?></option>
            </select>
        </div>
    </div>
    <div class="filter_mandant">
        <?= filter_mandant($filter_mandant) ?>
    </div>
</ul>

<div id="mainContent">

    <?php echo current_website_language($site, $language); ?>
    <h1><?php echo $collection_setup_info['description']; ?></h1>





    <form id="<?= $formname ?>" name="<?= $formname ?>" method="post">
        <input type="hidden" class="selected_linklist_row" name="input_collection_id" value="" />
        <input type="hidden" id="filter_collection_setup_id" name="filter_collection_setup_id"
               value="<?php echo(isset($_REQUEST['filter_id']) ? (int)$_REQUEST['filter_id'] : ''); ?>" />
        <div class="requestLoader"></div>
        <div id="page_linklist">
            <?
            $where = "";
            if ($collection_setup_id != 0) {
                $where = " and main_collection.main_collection_setup_id = " . $collection_setup_id;
            }

            switch($filter) {
                case 'past':
                    $where .= " AND (validity_to IS NOT NULL AND validity_to < '" . date("Y-m-d") . "') ";
                    break;
                case 'future':
                    $where .= " AND (validity_from IS NOT NULL AND validity_from > '" . date("Y-m-d") . "') ";
                    break;

                case 'all':
                    // muss nicht nach datum gefiltert werden
                    break;

                default:
                    $where .= " AND (validity_from IS NULL OR validity_from <= '" . date("Y-m-d") . "') ";
                    $where .= " AND (validity_to   IS NULL OR validity_to   >= '" . date("Y-m-d") . "') ";
                    break;
            }

            if($filter_mandant != NULL){
                $where = " AND ((main_collection_mandant_link.main_collection_id IS NOT NULL AND main_mandant_id = ".$filter_mandant.") OR main_collection_mandant_link.main_collection_id IS NULL)";
            }

            $query = "SELECT
		DISTINCT main_collection.id, 
		main_collection.description AS '" . $translation->get("description") . "',
		from_unixtime(main_collection.modified_date, '%d.%m.%Y %H:%i:%s') AS '" . $translation->get("changed_on") . "', 
		main_admin_user.name AS '" . $translation->get("changed_by") . "' 
	from 
		main_collection left join main_admin_user
	ON 
		main_collection.modified_user = main_admin_user.id
		inner join main_collection_setup
	ON
	 	main_collection.main_collection_setup_id = main_collection_setup.id
    LEFT JOIN main_collection_setup_websites
        ON main_collection_setup.id = main_collection_setup_websites.main_collection_setup_id
    LEFT JOIN main_collection_mandant_link 
        ON main_collection.id = main_collection_mandant_link.main_collection_id       
	where 
		(main_collection_setup.main_language_id = " . (int)$GLOBALS["language"]['id'] . " OR main_collection_setup.all_languages = 1 OR main_collection_setup_websites.main_site_id = ".$GLOBALS['site']['id']." ) $where ORDER BY main_collection.main_collection_setup_id asc, sorting asc";

            if($collection_setup_id == 4){
                $query = query_infoboard($where);
            }
            $format = array('option', 'text', 'text', 'text', 'text');
            if ($result = @mysqli_query($GLOBALS['mysql_con'], $query)) {
                linklist($result, $formname, $format, "input_collection_id", "edit_collection", TRUE, "update_sortorder_collection", "sendRequest('delete_collection', false, '{$translation->get('delete_collection_confirm')}')");
            }
            ?>
        </div>
    </form>

</div>
<?php

function query_infoboard($where){
    $query = "SELECT DISTINCT main_collection.id, 
        IFNULL(DATE_FORMAT(main_data.data_date, '%d.%m.%Y'), '') AS 'Datum',
        main_collection.description AS 'Beschreibung',
		main_admin_user.name AS 'Geändert von'
    FROM main_collection LEFT JOIN (
        SELECT
            ml.main_collection_id,
            ml.data AS data_text,
            STR_TO_DATE(ml.data, '%Y-%m-%d') AS data_date
        FROM main_collection_link ml
        WHERE ml.main_collection_setup_content_id = 54
    ) AS main_data ON main_collection.id = main_data.main_collection_id
    LEFT JOIN main_admin_user
	    ON main_collection.modified_user = main_admin_user.id
    LEFT JOIN main_collection_mandant_link 
        ON main_collection.id = main_collection_mandant_link.main_collection_id       
    WHERE main_collection.main_collection_setup_id = 4" . $where . " ORDER BY main_collection.main_collection_setup_id asc, main_data.data_date DESC";
    return $query;
}
?>