<?php

namespace App\Providers;

use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
use App\Modules\System\Models\Menus;
use App\Modules\User\Models\Modules_user;
use App\User;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

use App;
use View;
use Auth;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);
        View::composer('*', function($view){
            if (Auth::check()){
                $component = array();
                $user = User::find(Auth::id());
                $permissions = $user->getPermissionsViaRoles();
                $all_roles = User::with('roles')->find(Auth::id());

                foreach ($permissions as $a) {
                    switch ($a->name) {
                        case 'add':
                           $component['perm_add'] = true;
                        break;
                        case 'edit':
                            $component['perm_edit'] = true;
                        break;
                        case 'list':
                            $component['perm_list'] = true;
                        break;
                        case 'delete':
                            $component['perm_delete'] = true;
                        break;
                    }
                }
                           
                $component = (Object) $component;
                
                $view->with('permission_check',$component)->with('roles_check',$all_roles);
            }
        });


    }
}
