<?php namespace App\Http\Middleware;
 
use Closure;
use App\Custom\Misc;
use App\Custom\SQLConnection;
use stdClass;
use Session;
use DB;

class MultiwebMiddleware {


	/**
	 * Handle an incoming request.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @param  \Closure  $next
	 * @return mixed
	 */
	public function handle($request, Closure $next) {
        $webdata = Session::get('webdata');
        if (!$webdata) {
            //$wdata = DB::selectOne('EXEC dbo.obtener_web_data ?, ?', array($_SERVER['HTTP_HOST'], env('APP_ID')) );
            $wdata = DB::selectOne('EXEC dbo.obtener_web_data ?, ?', array('client.inquirovitae.com', env('APP_ID')) );
            
            $pers = array('web_id' => $wdata->web_id,
                                    'web_name' => $wdata->web_name,
                                    'web_title' => $wdata->web_title,
                                    'http_host' => $wdata->http_host,
                                    'uri_logo' => $wdata->uri_logo,
                                    'uri_icon' => $wdata->uri_icon,
                                    'uri_css1' => $wdata->uri_css1,
                                    'uri_css2' => $wdata->uri_css2,
                                    'uri_js1' => $wdata->uri_js1,
                                    'copyright' => $wdata->copyright,
                                    'servicio_contratado'=> $wdata->servicio_contratado,
                                    'id_plataforma' => $wdata->id_plataforma,
                                    'app_id' => $wdata->app_id);

            Session::put('webdata', $pers);
        }
        /*
        #==  CODIGO ANTERIOR  ==#
        if (!strpos(Session::get('webdata')['http_host'], 'fidelitas')) {
        	return redirect('/login');
        }
        */

        //==  NUEVO  ==
        /*if (strpos(Session::get('webdata')['http_host'], 'avaldigital') !== false) {
        	return redirect('/principal');
        } else if (strpos(Session::get('webdata')['http_host'], 'imperium')){
            return redirect('/principal-imperium');
        } else if (strpos(Session::get('webdata')['http_host'], 'inquiro')){
            return redirect('/principal-inquiro'); 
        } else if (!strpos(Session::get('webdata')['http_host'], 'fidelitas')) {
        	return redirect('/login');
        }*/

	// SOLO PARA TESTING
        if (isset(Session::get('webdata')['id_plataforma']) and Session::get('webdata')['id_plataforma'] != 'FI'){
        	return redirect('/login');
	}

	return $next($request);

	
        if (isset(Session::get('webdata')['id_plataforma']) and Session::get('webdata')['id_plataforma'] != 'FI'){
            /* if ( env('APP_ENV') == 'local' ){
                return redirect('/main-'.strtolower(Session::get('webdata')['id_plataforma']));
            } else {
                return redirect('/login');
                //return redirect(env('URL_STATIC_FRONT')); 
            } */
            if ( env('APP_ENV') == 'production' && Session::get('webdata')['id_plataforma'] == 'IV' ){
                return redirect(env('URL_STATIC_FRONT'));
            } else {
                return redirect('/main-'.strtolower(Session::get('webdata')['id_plataforma']));
            }
        }
        elseif (!isset(Session::get('webdata')['id_plataforma'])){
            return redirect('/login');
        }

		return $next($request);
	}

}
