<?php
namespace App\Blade;

use App\MultiProject;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Auth;

class CustomRoleDirective extends ServiceProvider
{
   
    public function boot()
    {
    Blade::directive('role', function ($expression) {
        // dd('crole');
    $roles = json_decode(str_replace("'", '"', $expression));
  
     if(!is_array($roles)){
        $roles = explode(',', $roles);
     }
 
      $mproject=  MultiProject::where('name','like','Super Admin')->first();
      if(empty($mproject)){
        return "<?php if (false): ?>";
      }
      $multiProjectId = $mproject->id;
      $user = Auth::user();
        $roles = $user
        ->roles()
        ->whereIn('name', $roles)
         ->where('name', 'like','admin')
        ->whereHas('projects', function ($query) use ($multiProjectId) {
            $query->where('multi_project_id', $multiProjectId);
        })
        ->get();
 

        if ( (sizeof($roles) > 0)) {
            // return "<?php if(auth()->check() &&  (sizeof($roles) > 0) : "; 

             return "<?php if (true): ?>";
		}
         
        
        return "<?php if (false): ?>";
    });
    Blade::directive('endrole', function () {
        return "<?php endif; ?>";
    });
    
}

    public function register()
    {
        //
    }
}

 
