<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Menu;
use Illuminate\Http\Request;

class MenuController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        if(auth()->user()->can('show_menu')){
          return view('menu.index',['items'=>menu(true)]);
            
        }
        else{
              return abort(404);

        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->validate([
            'parent_id'=>'nullable',
            'title' => 'required|unique:menus|max:255',
            'icon'=>'required',
            'uri'=>'required',
            'permission'=>'nullable',
        ]);

        if(isset($data['parent_id'])){
            $data['order'] = Menu::highestOrderMenuItem($data['parent_id']);
        }
        

        Menu::create($data);

        return redirect()->route('menu.index')->with('success', __('Task was successful'));
    }

    public function order(Request $request)
    {  
        $menuItemOrder = $request->input('order');
        $this->orderMenu($menuItemOrder, null);
        return ['status'=>'success','message'=>__('Task was successful')];
    }

    private function orderMenu(array $menuItems, $parentId)
    {
        foreach ($menuItems as $index => $menuItem) {
            $item = Menu::findOrFail($menuItem['id']);
            $item->order = $index + 1;
            $item->parent_id = $parentId;
            $item->save();

            if (isset($menuItem['children'])) {
                $this->orderMenu($menuItem['children'], $item->id);
            }
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Menu  $menu
     * @return \Illuminate\Http\Response
     */
    public function show(Menu $menu)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Menu  $menu
     * @return \Illuminate\Http\Response
     */
    public function edit(Menu $menu)
    {
        $menus_all=Menu::all();
        return view('menu.edit',['menu'=>$menu,'menus_all'=>$menus_all]);
      
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Menu  $menu
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Menu $menu)
    {
      $data = $request->validate([
            'parent_id'=>'nullable',
            'title' => 'required|unique:menus,title,'.$menu->id.'|max:255',
            'icon'=>'required',
            'uri'=>'nullable',
            'permission'=>'nullable',
        ]);
       
      $menu_permission=Menu::where('title',$request->parent_id)->first();
    
       if( optional($menu_permission)->parent ==null){
        
          $menu->parent_id=null;
           $menu->save();
       
          return redirect()->route('menu.index')->with('success', __('Task was successful'));
           
       }
     
          $menu->update($data);
   

        return redirect()->route('menu.index')->with('success', __('Task was successful'));
    }
    /**

     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Menu  $menu
     * @return \Illuminate\Http\Response
     */
    public function destroy(Menu $menu)
    {
        $menu->delete();
        return ['status'=>'success','message'=>__('Task was successful')];
    }
}
