<?php

namespace App\Providers;
/*use Illuminate\Database\DatabaseManager;
use DB;*/
use App\Models\menu;
use Session;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
use Illuminate\Http\Request;

class ContentServiceProvider extends ServiceProvider
{

    public $menuItems;
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */

public function boot()
    {

        
       /* $this->menuItems = ["Home", "About Us", "Contact"];
    view()->composer('layouts.master', function($view) {
            $view->with(['contents' => $this->menuItems]);
        });*/
     /* $this->menuItems= menu::where('active', '0')->orderBy('menu_order', 'ASC')->get();
        //$this->menuItems =  DB::connection('mysql')->table('menus')->get();
        view()->composer('layouts.admin', function($view) {
            $view->with(['contents' => $this->menuItems]);
        });*/

        // $menuItems= menu::where('active', '0')->orderBy('menu_order', 'ASC')->get();




        /* view()->composer('layouts.admin', function($view) {
            $view->with(['contents' => $this->menuItems]);
        });*/


         view()->composer('layouts.admin', function ($view) 
    {
      $lid=Session::get('user_id');
      $is_admin=Session::get('is_admin');
     if($is_admin){
       $menuItems=menu::where('active','=', '0')->where('to_admin', '=', '1')->orderBy('menu_order', 'ASC')->get();
     }else{
     $menuItems = DB::table('menus as A')
          ->join('menus_user_details as B',function ($join) use ($lid) {
              $join->on('B.menu_id','A.menu_id')
              ->where('B.id','=',$lid)
              ;
              })
             ->where('A.active','=','0')
             ->where('A.to_admin','=','0')
             ->select('A.menu_id', 'A.menu_name', 'A.root', 'A.menu_order', 'A.menu_type', 'A.to_admin', 'B.sedp', 'A.menu_icon', 'A.active', 'A.sp', 'A.column_heading', 'A.column_width', 'A.column_alighn', 'A.column_conversion', 'A.sp_date_picker')
             ->orderBy('A.menu_order', 'ASC')->get();
              //N  $menuItems=menu::where('active','=', '0')->where('to_admin', '=', '0')->orderBy('menu_order', 'ASC')->get();
             }
        //dd($menuItems);
  
    
         $data = json_decode(json_encode($menuItems),true);
       //  dd($data);
         $t_count=sizeof($menuItems);
      $menu=array();
      $k=0;
      $i=0;
      $arr=array();
         foreach($data as $ind=>$val){
            
            if($val['menu_type']==0 || $val['menu_type']==3){
            
                 // $menu[$k]['sub']=$arr;
                $k++;
                $menu[$k] = array('menu_type' => $val['menu_type'],'menu_name' => $val['menu_name'],'menu_id' => $val['menu_id'],'root' => $val['root'],'menu_icon' => $val['menu_icon'],'to_admin' => $val['to_admin'],'sub'=>array());
                       
                
              $arr=array();
              $i=0;

            }else if($val['menu_type']==1){
               $menu[$k]['sub'][$i] = array('menu_type' => $val['menu_type'],'menu_name' => $val['menu_name'],'menu_id' => $val['menu_id'],'root' => $val['root'],'menu_icon' => $val['menu_icon']);
              
               $i++;

            }
           //array_push($menu[$k]['sub'],$arr);

         }
       //  dd($menu);

         //$menu[$k]['sub']=$arr;
         $this->menuItems=$menu;
        $view->with(['contents' => $this->menuItems]);    
    });  
    }
}
