<?php
/**
 * Created by PhpStorm.
 * User: marco
 * Date: 31/07/18
 * Time: 20.06
 */ ?>

    <?php
    //                    $theme_location = 'menu-1';
    // if ( ($theme_location) && ($locations = get_nav_menu_locations()) && isset($locations[$theme_location]) ) {
    //	 $menu       = get_term( $locations[ $theme_location ], 'nav_menu' );
    //	 $menu_items = wp_get_nav_menu_items( $menu->term_id );
    //
    //	 var_dump($menu_items);
    //

    /*
     *
    <div class="menu-menu-1-container"><ul id="primary" class="menu"><li id="menu-item-7" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-home menu-item-7"><a href="//192.168.1.7:3000/helijet/">Pagina di esempio.</a></li>
<li id="menu-item-92" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-90 current_page_item menu-item-92"><a href="//192.168.1.7:3000/helijet/elicotteri/">Elicotteri</a></li>
<li id="menu-item-93" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-93"><a href="#">Test Parent</a>
<ul class="sub-menu">
<li id="menu-item-94" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-94"><a href="//192.168.1.7:3000/helijet/news/">News</a></li>
</ul>
</li>
</ul></div>

     */

    // }
    ?><?php

    $theme_location = 'primary';
    $locations = get_nav_menu_locations();
    $is_loc_isset = isset($locations[$theme_location]);
    $menu = get_term($locations[$theme_location], 'nav_menu');
    $menu_items = wp_get_nav_menu_items($menu->term_id);


    //                        wp_nav_menu(array(
    //                            'theme_location' => 'primary',
    //                            'menu_id' => 'primary',
    //                        ));


    ?>
    <div class="blok blok-menu-mobile">
        <ul id="primary" class="ul-blok-menu-mobile">

            <?php foreach ($menu_items as $menu_item) :
                //  prent_r($menu_item);
                $menu_parent = $menu_item->menu_item_parent;
                $link = $menu_item->url;
                $custom_field = get_field('attiva_gerarchia', $menu_item->ID);
                $a_start = '<a href="' . $link . '">';
                $subs = '';
                $li_cass = '';
                $a_end = '</a>';
                if (!empty($custom_field)) {
                    if ($custom_field === true) {
                        $li_cass = 'menu-item-has-children';
                        $a_start = '<a href="#">';
                        $subs .= '';
                        $wp_id_menu = $menu_item->object_id;
                        $inside_subs = '';

                        if (!function_exists('recursive_sub_menu_wp')) {

                            function recursive_sub_menu_wp(&$parent_id, &$subs)
                            {

                                $args = array(
                                    'post_parent' => $parent_id,
                                    'post_type' => 'any',
                                    'numberposts' => -1,
                                    'post_status' => 'any',
                                    'orderby' => 'menu_order',
                                    'order' => 'ASC'
                                );
                                $children = get_children($args);
                                // prent_r($children);
                                if (!empty($children)) {
                                    $subs .= '<ul class="sub-menu">';
                                    foreach ($children as $child) {
                                        $title = $child->post_title;
                                        $child_wp_id = $child->ID;
                                        $link = get_the_permalink($child_wp_id);
                                        $sub_args = array(
                                            'post_parent' => $child_wp_id,
                                            'post_type' => 'any',
                                            'numberposts' => -1,
                                            'post_status' => 'any',
                                            'orderby' => 'menu_order',
                                            'order' => 'ASC'
                                        );
                                        $sub_children = get_children($sub_args);
                                        if (!empty($sub_children)) {
//                                                        $inside_subs .= '<ul>';
//                                                        recursive_sub_menu_wp($child_wp_id, $subs, $inside_subs);
//                                                        $inside_subs .= '</ul>';
                                        }


                                        $subs .= '<li><a href="' . $link . '">' . $title . '</a></li>';
                                    }
                                    $subs .= '</ul>';
                                }

                            }
                        }

                        recursive_sub_menu_wp($wp_id_menu, $subs);
                        $subs .= '';
                    }
                }

                if ($menu_parent == 0) :
                    ?>
                    <li class="<?php echo $li_cass; ?>"><?php echo $a_start . $menu_item->title . $a_end . $subs; ?></li>
                <?php
                endif;
            endforeach;
            ?>


        </ul>
    </div>
