<?php
$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
$formname    = "form_edit_main_navigation_list";
?>

<ul class="toolbar_menu">
    <?= button("new", $translation->get("new_menuitem"), $formname, "loadCard('new_navigation', true)"); ?>
</ul>

<div id="mainContent">
    <?php echo current_website_language($site, $language); ?>
    <h1><?php echo get_translation('left_navigation'); ?></h1>

    <form id="<?php echo $formname; ?>" name="<?php echo $formname; ?>" method="post">
        <div class="requestLoader"></div>
        <input type="hidden" class="selected_linklist_row" name="input_main_navigation_id" value="" />

        <div class="clearfix"></div>
        <div class="requestLoader"></div>
        <div id="page_linklist">
            <?
            // $query = "SELECT id, menu_name AS '".$translation->get("name")."', code, active AS ".$translation->get("state").", modified_date as '".$translation->get("changed_on")."', modified_admin_user_id AS '".$translation->get("changed_by")."' FROM main_navigation WHERE main_site_id = '" . $site["id"] . "' AND main_language_id = '" . $language["id"]."'";
      
            // $result   = mysqli_query($GLOBALS['mysql_con'], $query);
            // $num_rows = mysqli_num_rows($result);
            
            // $format = array('option', 'text', 'text','boolean_active','text','text');
            // if($num_rows > 0) {
            //     // linklist($result,"form_page_list",$format, "input_page_id", "edit_content_page");
            //     linklist($result, $formname, $format, "input_main_navigation_id", "edit_navigation", TRUE, "reorder_navigation", "sendRequest('delete_navigation', false, '{$translation->get('delete_collection_confirm')}')");	
            // } else {
            //     echo "<div class=\"infobox\">" . $translation->get("no_rows_found") . "</div>";
            // }
            show_main_navigation($site, $language);
            ?>
        </div>

    </form>

</div>
<script type="text/javascript">
    var reorderCall = null;
    var dragEnabled = false;

    function toggleDragDrop( _aTag ) {
        if (dragEnabled === false) {
            dragEnabled = true;
            $('ol.sortable').addClass("enabled");
            $('ol.sortable').nestedSortable("option", "disabled", false);
            $(_aTag).html("<?php echo $translation->get('disable_drag_drop'); ?>");
        } else {
            dragEnabled = false;
            $('ol.sortable').removeClass("enabled");
            $('ol.sortable').nestedSortable("option", "disabled", true);
            $(_aTag).html("<?php echo $translation->get('enable_drag_drop'); ?>");
        }
    }

    //$('.dd').nestable({ /* config options */ });
    $('ol.sortable').nestedSortable({
                                        forcePlaceholderSize: true,
                                        handle              : '.dd-icon',
                                        helper              : 'clone',
                                        items               : 'li',
                                        opacity             : .6,
                                        placeholder         : 'placeholder',
                                        revert              : 250,
                                        tabSize             : 25,
                                        tolerance           : 'pointer',
                                        toleranceElement    : '> div',
                                        maxLevels           : 5,
                                        isTree              : true,
                                        expandOnHover       : 1000,
                                        cookieIndex         : 'nestedSortable_expanded',
                                        startCollapsed      : true,
                                        stop                : function ( event, ui ) {
                                            reorder_navigation();
                                            store_expanded();
                                        },
                                        disabled            : false
                                    });

    $('.disclose').click(function () {
        $(this).closest('li').toggleClass('mjs-nestedSortable-collapsed').toggleClass('mjs-nestedSortable-expanded');
        store_expanded();
    });

    function store_expanded() {
        var list = [];
        $('ol.sortable').find('li.mjs-nestedSortable-expanded').each(function () {
            list.push($(this).attr("id"));
        });
        $.cookie('nestedSortable_expanded', list.join(','));
    }

    function reorder_navigation() {
        if (reorderCall !== null) {
            reorderCall.abort();
        }

        serialized = $('ol.sortable').nestedSortable('serialize');
        arraied = $('ol.sortable').nestedSortable('toHierarchy', {startDepthCount: 0});
        var parameter = {
            list: arraied
        };
        reorderCall = jQuery.post("?action=reorder_navigation", parameter, function ( output, status, xhr ) {
            if (xhr.getResponseHeader('REQUIRES_AUTH') == 1) {
                window.location.replace("/dc/");
            } else {
                console.log(output);
            }
        });
    }

    $('.dd-status:contains("Inaktiv")').parent().addClass("inactiveSitecontent");


</script>