<?php
$translation         = \DynCom\mysyde\common\classes\Registry::get("translation");
$formname            = "form_collection_preview_list";
$inputname           = "input_id";
$input_contactform_id       = (isset($_REQUEST["input_contactform_id"]) ? $_REQUEST["input_contactform_id"] : "");
$collection_setup_id = 0;

// erstelle kollektionen sammeln
$collection_setups = array();
$query             = "SELECT * FROM main_collection_setup WHERE (main_language_id = " . (int)$GLOBALS["language"]['id'] . " OR all_languages = 1)";
$result            = @mysqli_query($GLOBALS['mysql_con'], $query);
while ($row = @mysqli_fetch_array($result, 1)) {
    $collection_setups[$row['id']] = $row;
}

$query  = "SELECT ms.id, ms.main_language_id, ms.code, ms.description, ms.linked, ms.icon, ms.modified_user, ms.modified_date, ms.all_languages, ms.show_filter FROM main_collection_setup as ms INNER JOIN main_collection_setup_websites as mcsw on ms.id = mcsw.main_collection_setup_id WHERE mcsw.main_site_id = ".$GLOBALS['site']['id']." ";
$result = @mysqli_query($GLOBALS['mysql_con'], $query);
while ($row = @mysqli_fetch_array($result)) {
    $collection_setups[$row['id']] = $row;
}

$collections    = array();
$collection_ids = array();
if ($input_line['main_collection_setup_id'] != "") {
    $query  = "SELECT * FROM main_collection WHERE main_collection_setup_id = " . $input_line['main_collection_setup_id'];
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    while ($row = @mysqli_fetch_array($result, 1)) {
        $collections[]    = $row['description'];
        $collection_ids[] = $row['id'];
    }
}

$active_groups = array();
$query         = "SELECT main_collection_setup_group_id FROM main_page_collection_group_link WHERE main_page_link_id = " . (int)$input_line["id"];
$result        = @mysqli_query($GLOBALS['mysql_con'], $query);
while ($row = @mysqli_fetch_array($result)) {
    $active_groups[] = $row['main_collection_setup_group_id'];
}
?>

<script type="text/javascript">
    function loadPagesWithCollection( collection_setup_id ) {
        load_collections(collection_setup_id);
        console.log('hello');
        $('.collection_pages', $('#<?php echo $formname; ?>')).html("");
        $('.collection_preview_group_select_container', $('#<?php echo $formname; ?>')).html("");

        if (collection_setup_id == "" || collection_setup_id == 0) {
            return;
        }

        $('.loader', $('#<?php echo $formname; ?>')).show();

        // ajax
        var formData = new FormData(jQuery('#<?php echo $formname; ?>')[0]);
        formData.append("collection_setup_id", collection_setup_id);
        formData.append("page_link_id", '<?php echo $input_line["id"]; ?>');
        $.ajax({
                   url        : "?action=load_collection_pages_page",
                   type       : 'POST',
                   data       : formData,
                   cache      : false,
                   processData: false, // Don't process the files
                   contentType: false, // Set content type to false as jQuery will tell the server its a query string request
                   dataType   : 'json',
                   success    : function ( output, status, xhr ) {
                       
                       if (xhr.getResponseHeader('REQUIRES_AUTH') == 1) {
                           window.location.replace("/dc/");
                       } else {
                           $('.loader', $('#<?php echo $formname; ?>')).hide();
                           console.log('history');
                           $('.collection_pages', $('#<?php echo $formname; ?>')).html(output.page_with_collection_select);
                           $('.collection_preview_group_select_container', $('#<?php echo $formname; ?>')).html(output.collection_preview_group_select);
                       }
                   },
                   error: function (jqXHR, exception) {
                        var msg = '';
                        if (jqXHR.status === 0) {
                            msg = 'Not connect.\n Verify Network.';
                        } else if (jqXHR.status == 404) {
                            msg = 'Requested page not found. [404]';
                        } else if (jqXHR.status == 500) {
                            msg = 'Internal Server Error [500].';
                        } else if (exception === 'parsererror') {
                            msg = 'Requested JSON parse failed.';
                        } else if (exception === 'timeout') {
                            msg = 'Time out error.';
                        } else if (exception === 'abort') {
                            msg = 'Ajax request aborted.';
                        } else {
                            msg = 'Uncaught Error.\n' + jqXHR.responseText;
                        }
                        console.log(msg);
                   }
                  
               });
    }

    function load_collections( collection_setup_id ) {
        $('.collection_list_choose', $('#<?php echo $formname; ?>')).html("");

        var formData = new FormData(jQuery('#<?php echo $formname; ?>')[0]);
        formData.append("main_collection_setup_id", collection_setup_id);
        $.ajax({
                   url        : "?action=load_collections_page",
                   type       : 'POST',
                   data       : formData,
                   cache      : false,
                   processData: false, // Don't process the files
                   contentType: false, // Set content type to false as jQuery will tell the server its a query string request
                   success    : function ( output, status, xhr ) {
                       if (xhr.getResponseHeader('REQUIRES_AUTH') == 1) {
                           window.location.replace("/dc/");
                       } else {
                           $('.collection_list_choose', $('#<?php echo $formname; ?>')).html(output);
                       }
                   },
                   error      : function ( jqXHR, textStatus, errorThrown ) {
                       //console.log('ERRORS: ' + textStatus);
                   }
               });
    }

    function toggle_view_type( _type ) {
        if (_type == 0) {
            $('.collection_list_choose', $('#<?php echo $formname; ?>')).show();
            $('.collection_items_choose', $('#<?php echo $formname; ?>')).hide();
        } else {
            $('.collection_list_choose', $('#<?php echo $formname; ?>')).hide();
            $('.collection_items_choose', $('#<?php echo $formname; ?>')).show();
        }
    }

    function save_and_close_page_line() {
        jQuery('#save_and_close', jQuery('#<?php echo $formname; ?>')).val("1");
        loadCard('save_collection_preview_page', true);
    }
</script>

<div id="overlaycrumb">
    <?php
    if (isset($_REQUEST['is_collection_preview']) && $_REQUEST['is_collection_preview'] == 1) {
        echo $translation->get("assign_collection_preview");
    } else {
        echo $translation->get("assign_collection_list");
    }
    ?>
    <div id="closeoverlay" onclick="disableOverlay();"></div>
</div>

<ul class="toolbar_menu">
    <?= button("left", $translation->get("back"), $formname, "loadCard('edit_contactform', true)"); ?>

    <?= button("save", $translation->get("save"), $formname, "loadCard('save_collection_preview_page', true)"); ?>
    <?= button("delete",$translation->get("delete_content"),$formname,"loadCard('delete_line_contact', true, '{$translation->get('delete_assoc_pageline')}')","", false); ?>
     
</ul>

<div class="clearfix"></div>

<?php


if ($messages !== null) {
    if ((gettype($messages) == 'array' || gettype($messages) == 'object') && count($messages)) {
        echo '<div id="overlayMessages">' . join("\r\n", $messages) . '</div>';
    }
 }
 
?>

<?php if (isset($_REQUEST['is_collection_preview']) && $_REQUEST['is_collection_preview'] == 1): ?>

    <form id="<?= $formname ?>" name="<?= $formname ?>" method="post">
        <input name="input_id" type="hidden" value="<?= $input_line["id"] ?>">
        <input type="hidden" name="get_real_page_link_id" value="0" />
        <input type="hidden" name="input_contactform_id" value="<?php echo $input_contactform_id; ?>" />
        <input name="save_and_close" id="save_and_close" type="hidden" value="0" />
        <input name="is_collection_preview" id="is_collection_preview" type="hidden" value="1" />
        <input type="hidden" name="input_section_id" value="<?php echo $_REQUEST['input_section_id']; ?>" />
       
        <table class="cardform" border="0" cellspacing="0" cellpadding="0">
        <tr>
                <td>
                <? layout_class_select($translation->get("layout_area"), "input_layout_area_id", $input_line['layout_area_id']); ?>
                </td>
            </tr>
            <tr>
                <td>

                    <? create_collection_select($collection_setups, $translation->get("choose_collection_type"), "main_collection_setup_id", $input_line, FALSE, TRUE, "loadPagesWithCollection(this.value);"); ?>
                </td>
                <td>
                    <? input_select($translation->get("collection_preview_type"), "main_collection_view_type", $values = array('0', '1'), $value_names = array($translation->get("single_collection"), $translation->get("collection_list")), $input_line["main_collection_view_type"], FALSE, FALSE, 'onchange="toggle_view_type(this.value);"'); ?>
                    
                </td>
                 
               
               
            </tr>
           

            <tr>
                <td>
                    <img class="loader" style="display:none;" src="/layout/admin/img/icons/ajax-loader.gif" />

                    <div class="collection_pages">
                        <?
                        create_page_with_collection_select($input_line['main_collection_setup_id'], $translation->get("page_with_collections"), "main_collection_page_list_id", $input_line, FALSE);
                        ?>
                    </div>
                </td>

                <td>

                    <?php
                    if ($input_line["main_collection_view_type"] == 0) {
                        $display_list_choose = '';
                        $display_item_choose = 'style="display:none;"';
                    } else {
                        $display_item_choose = '';
                        $display_list_choose = 'style="display:none;"';
                    }

                    // einzelne kollektion
                    echo "<div class=\"collection_list_choose\" " . $display_list_choose . ">";
                    input_select($translation->get("choose_collection"), "main_collection_id", $values = $collection_ids, $value_names = $collections, $input_line["main_collection_id"], FALSE, FALSE);
                    echo "</div>";

                    // kollektionsliste
                    echo "<div class=\"collection_items_choose\" " . $display_item_choose . ">";
                    input($translation->get("collection_list_items"), "main_collection_items", "text", $input_line["main_collection_items"], 255);
                    echo "</div>";
                    ?>

                </td>
            </tr>
            <tr>
                <td>
                <? input($translation->get("active"), "input_active", "checkbox", $input_line['active']); ?>
                <? input($translation->get("self_definded_class"), "layout_class_defined", "text", $input_line["layout_class_defined"], 50) ?>
                </td>
            </tr>
        </table>
        <div class="collection_preview_group_select_container">
            <?php create_collection_preview_group_select($input_line['main_collection_setup_id'], $input_line["id"], $active_groups); ?>
        </div>
    </form>

<?php else: ?>

    <form id="<?= $formname ?>" name="<?= $formname ?>" method="post">
        <input name="input_id" type="hidden" value="<?= $input_line["id"] ?>">
        <input type="hidden" name="get_real_page_link_id" value="0" />
        <input type="hidden" name="input_contactform_id" value="<?php echo $input_contactform_id; ?>" />
         <input type="hidden" name="input_section_id" value="<?php echo $_REQUEST['input_section_id']; ?>" />
        <input name="save_and_close" id="save_and_close" type="hidden" value="0" />
        <input name="is_collection_list" id="is_collection_list" type="hidden" value="1" />

        <input name="main_collection_id" type="hidden" value="0" />
        <input name="main_collection_page_list_id" type="hidden" value="0" />
        <input name="main_collection_items" type="hidden" value="0" />
        <input name="main_collection_view_type" type="hidden" value="0" />

        <table class="cardform" border="0" cellspacing="0" cellpadding="0">
        <tr>
                <td>
                <? layout_class_select($translation->get("layout_area"), "input_layout_area_id", $input_line['layout_area_id']); ?>
                </td>
            </tr>
            <tr>
                <td>

                    <? create_collection_select($collection_setups, $translation->get("choose_collection_type"), "main_collection_setup_id", $input_line, FALSE, TRUE, "loadPagesWithCollection(this.value);"); ?>
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <img class="loader" style="display:none;" src="/layout/admin/img/icons/ajax-loader.gif" />
                    
                </td>
                 
                
                
            </tr>
            <tr>
                <td>
                <? input($translation->get("active"), "input_active", "checkbox", $input_line['active']); ?>
                <? input($translation->get("self_definded_class"), "layout_class_defined", "text", $input_line["layout_class_defined"], 50) ?>
                </td>
            </tr>
        </table>
        <div class="collection_preview_group_select_container">
            <?php create_collection_preview_group_select($input_line['main_collection_setup_id'], $input_line["id"], $active_groups); ?>
        </div>
    </form>

<?php endif; ?>
