<?php
/*=========================================================================================
App Name : PPID PTUN Kupang
File Name : Menu Helper
--------------------------------------------------------------------------------------
Author : Fuad Auliya Rohman
Email : fuad.dev16@gmail.com
==========================================================================================*/

use Illuminate\Support\Facades\DB;


if (!function_exists('get_main_menu')) {
    function get_main_menu()
    {
        /* $object = new stdClass(); */

        $main_menu = [];
        $Menus = DB::table('menus')->where('status', 'Y')->orderBy('sequence', 'asc')->get();

        for ($i = 0; $i <= count($Menus) - 1; $i++) {
            if ($Menus[$i]->parent_id == 0) {
                $main_menu[] = $Menus[$i];
            }
        }

        // PARENT MENU
        for ($x = 0; $x < count($main_menu); $x++) {
            $parent_menu = [];
            for ($q = 0; $q < count($Menus); $q++) {
                if ($Menus[$q]->parent_id == $main_menu[$x]->id) {
                    $parent_menu[] = $Menus[$q];
                }
            }
            $main_menu[$x]->parent_menu = $parent_menu;
        }
        // dd($Menus);

        // SUBMENU
        for ($i = 0; $i < count($main_menu); $i++) {
            for ($x = 0; $x < count($main_menu[$i]->parent_menu); $x++) {
                $parent_submenu = [];
                for ($e = 0; $e < count($Menus); $e++) {
                    if ($main_menu[$i]->parent_menu[$x]->id == $Menus[$e]->parent_id) {
                        $parent_submenu[] = $Menus[$e];
                        // $uniqueArray = array_unique($parent_submenu, SORT_REGULAR);
                        // $main_menu[$i]->parent_menu[$x]->parent_submenu = $uniqueArray;
                    }
                }
                $main_menu[$i]->parent_menu[$x]->parent_submenu = $parent_submenu;
            }
        }
        // dd($Menus);

        // SUBSUBMENU
        $a = [];
        for ($i = 0; $i < count($main_menu); $i++) {
            for ($j = 0; $j < count($main_menu[$i]->parent_menu); $j++) {
                for ($k = 0; $k < count($main_menu[$i]->parent_menu[$j]->parent_submenu); $k++) {
                    $parent_subsubmenu = [];
                    for ($e = 0; $e < count($Menus); $e++) {
                        if ($main_menu[$i]->parent_menu[$j]->parent_submenu[$k]->id == $Menus[$e]->parent_id) {
                            $parent_subsubmenu[] = $Menus[$e];
                            $uniqueArray = array_unique($parent_subsubmenu, SORT_REGULAR);
                            $main_menu[$i]->parent_menu[$j]->parent_submenu[$k]->parent_subsubmenu = $uniqueArray;
                        }
                    }
                }
            }
        }

        //$object->main_menu = $main_menu;
        return $main_menu;
    }
}

if (!function_exists('ul')) {
    function ul($parent = 0, $attr = '')
    {
        static $i = 1;
        $indent = str_repeat("\t\t", $i);

        if (isset($parent)) {
            if ($attr) {
                $attr = ' ' . $attr;
            }

            $html = "\n$indent";
            $html .= "<ul$attr>";
            $i++;

            foreach ($parent as $row) {
                $child = $row['id'];
                $html .= "\n\t$indent";
                $html .= '<li' . $row['li_attr'] . '>';
                $html .= $row['label'];
                if ($child) {
                    $i--;
                    $html .= $child;
                    $html .= "\n\t$indent";
                }
                $html .= '</li>';
            }
            $html .= "\n$indent</ul>";

            return $html;
        } else {
            return false;
        }
    }
}

if (!function_exists('addRow')) {
    function addRow($id, $parent, $li_attr, $label)
    {
        $parent[] = array('id' => $id, 'li_attr' => $li_attr, 'label' => $label);
    }
}

if (!function_exists('generateList')) {
    function generateList($ul_attr = '')
    {
        return ul(0, $ul_attr);
        /*    $this->data = array(); */
    }
}

/**
 * Clear the temporary data
 *
 */
if (!function_exists('clear')) {
    function clear()
    {
        /* $this->data = array(); */
    }
}

if (!function_exists('is_array_check')) {
    function is_array_check(array $test_var)
    {
        foreach ($test_var as $key => $el) {
            if (is_array($el)) {
                return true;
            }
        }
        return false;
    }
}
