<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\NavigationSections;
use App\Repository\PackagesRepository;
use App\Navigations;
use Facades\App\Repository\Navigation;
class FrontNavigationServiceProvider extends ServiceProvider
{
 
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
         $navigations = array();
        $model = NavigationSections::get();
        
        $i=0;
        foreach($model as $row){
            $navigations[$i]["title"] = $row->Title;
            $navigations[$i]["id"] = $row->PKSectionID;
            $navigations[$i]["parents"] = array();

            $parentModel = Navigations::where("ParentID",0)
                ->where("FKSectionID", $row->PKSectionID)
                ->get();
            
            $j=0;
            $parents=array();
            foreach($parentModel as $p){
                $parents[$j]["name"]=$p->Name;
                $parents[$j]["id"]=$p->PKNavigationID;
                $parents[$j]["url"]=$p->LinkWithURL;
                
                $children = array();
                
                $childModel = Navigations::where("ParentID",$p->PKNavigationID)
                ->where("FKSectionID", $row->PKSectionID)
                ->get();
                $k=0;
                foreach($childModel as $c){
                
                   $children[$k]["name"]=$c->Name;
                   $children[$k]["id"]=$c->PKNavigationID;
                   $children[$k]["url"]=$c->LinkWithURL;
                   $children[$k]["type"]=$c->LinkWith;
                   $k++; 
                }
                $parents[$j]["children"]=$children;
                $j++;
            }
            $navigations[$i]["parents"]=$parents;
            $i++;
          
        }
//        d($navigations,1);
        
       
        \View::share('categories',Navigation::all());
        \View::share('navigations',$navigations);

    }
}
