<?php

namespace App\Http\Middleware;
use Illuminate\Support\Facades\Auth;
use Closure;

class Teller
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $Roles = ["teller","admin","penilai","khazanah"];
        $routeMiddleware = $request->route()->middleware();
        $filteredMiddleware = array_intersect($routeMiddleware, $Roles);

        $filtered = array_map(function($res){
            switch($res){
                case 'teller' : return 'teller';
                case 'khazanah' : return 'pc';
            }
        },$filteredMiddleware);

        // dd(in_array(Auth::user()->roles,$filtered));
        if(Auth::check() && (Auth::user()->role_harian == 'teller' || in_array(Auth::user()->roles,$filtered))){
            return $next($request);
        }else{
            dd(Auth::user()->role_harian == 'teller',in_array(Auth::user()->roles,$filtered));
            dd('not allowed');
        }
        dd('test');

        return redirect('/login');
    }
}
