<?php

namespace App\Http\Middleware;

use Closure;
use Spatie\Menu\Laravel\Facades\Menu;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;

class MenuMiddleware
{
   
    public function handle($request, Closure $next)
    {
        $menu = Menu::new()
            ->addClass('nav nav-pills nav-sidebar flex-column nav-flat')
            ->setWrapperTag('ul')
            ->setAttributes([
                'data-widget' => 'treeview',
                'role' => 'menu',
                'data-accordion' => 'false'
            ]);

        $menu->html('<li class="nav-item">
                    <a href="'.url('/').'" class="nav-link">
                    <i class="nav-icon mdi mdi-home"></i>
                    <p>Halaman Utama </p>
                    </a>
                    </li>');

        // Ambil data menu utama
        $user = Auth::guard('admin')->user();
        $role = $user->roles->first();
        $currentRoleId = $role ? $role->id : null;

        $menus = DB::select("
                SELECT m.*, rm.menu_id AS assigned_menu_id
                FROM menus m
                LEFT JOIN role_menu rm
                ON m.id = rm.menu_id
                WHERE m.parent_id IS NULL AND rm.role_id = '".$currentRoleId."'
                ORDER BY m.id
            ");
  
        foreach ($menus as $item) {
            // Ambil submenu jika ada
            $children = DB::select("
                SELECT m.*, rm.menu_id AS assigned_menu_id
                FROM menus m
                LEFT JOIN role_menu rm
                ON m.id = rm.menu_id
                WHERE m.parent_id = '".$item->id."' AND rm.role_id = '".$currentRoleId."'
                ORDER BY m.id
            ");

                

            if (isset($children)) {
                // Menu dengan child
                $submenu = Menu::new()->addClass('nav nav-treeview');

                foreach ($children as $child) {
                
                    $submenu->html(
                        '<li class="nav-item">' .
                            '<a href="' . url($child->url) . '" class="nav-link">' .
                                '<i class="' . $child->icon . ' nav-icon"></i>' .
                                '<p>' . $child->title . '</p>' .
                            '</a>' .
                        '</li>'
                    );
                }
                $menu->html(
                    '<li class="nav-item">' .
                        '<a href="#" class="nav-link">' .
                            '<i class="nav-icon ' . $item->icon . '"></i>' .
                            '<p>' . $item->title . '<i class="fas fa-angle-left right mt-1"></i></p>' .
                        '</a>' .
                        $submenu->render() .
                    '</li>'
                );
            } else {
                // Menu tanpa child
                $menu->html(
                    '<li class="nav-item">' .
                        '<a href="' . url($item->url) . '" class="nav-link">' .
                            '<i class="nav-icon ' . $item->icon . '"></i>' .
                            '<p>' . $item->title . '</p>' .
                        '</a>' .
                    '</li>'
                );
            }
        }

        view()->share('menu', $menu->render());

        return $next($request);
    }
        
}
