<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Port;
use App\Http\Requests\PortRequest;
use App\User;
use App\PortType;
use Auth;

class PortController extends Controller
{
		public function __construct()
		{
				$this->middleware('auth');
		}

		public function index()
		{
	
			if(Auth::user()->role->role_permission('view_port')){
				if(Auth::user()->role->role_permission('view_port')->level == 'all'){

					$ports = Port::paginate(30);
					$portTypes = PortType::lists('name', 'id')->toArray();
                    $userSelect = User::select(DB::raw("CONCAT(first_name, ' ', last_name) AS full_name, id"))->lists('full_name', 'id')->toArray();
										
				}elseif(Auth::user()->role->role_permission('view_port')->level == 'team'){
					$users = User::pluck('id');
					$ports = Port::paginate(30);
				}else{
					$ports = Port::paginate(30);
	
				}

				return view('ports.index', compact('ports','portTypes','userSelect'));
			}else{
				abort(403);
			}
		}
		
		public function search(Request $request)
		{
			if(Auth::user()->role->role_permission('view_port')){
				if(Auth::user()->role->role_permission('view_port')->level == 'all'){
					
					$ports = Port::where('name','like', ['%'.$request->get('term').'%'])->paginate(30);
				}elseif(Auth::user()->role->role_permission('view_port')->level == 'team'){
				
					$ports = Port::whereIn('name','like', ['%'.$request->get('term').'%'])->paginate(30);
				}else{
					
					$ports = Port::whereIn('user_id', $users)->paginate(30);
				}	
				return view('ports.index', compact('ports'));
			}else{
				abort(403);
			}
		}
    
    public function create(User $user)
    {
			if(Auth::user()->role->role_permission('create_port')){
			    $portTypes = PortType::lists('name', 'id')->toArray();
                $userSelect = User::select(DB::raw("CONCAT(first_name, ' ', last_name) AS full_name, id"))->lists('full_name', 'id')->toArray();
				return view('ports.create', compact('users','user', 'portTypes','userSelect'));
			}else{
				abort(403);
			}
    }
	
    public function store(POrtRequest $request, User $user)
    {
			if(Auth::user()->role->role_permission('create_port')){
			$ports = Port::create($request->all());
			
			return redirect(isset($user->id) ? 'users/'.$user->id : 'ports')->withSuccess('Port has been saved.');
			}else{
				abort(403);
			}
    }
	
    public function show(Port $port)
    {
			
			
			if(Auth::user()->role->role_permission('view_port')){
				if(Auth::user()->role->role_permission('view_port')->level == 'team'){
					$users = User::pluck('id');
					$port = Port::where('id', $port->id)->first();
				}elseif(Auth::user()->role->role_permission('view_port')->level == 'self'){
					$port = Port::where('id', $port->id)->first();
				}
				return $port ? $port : abort(403);
			}else{
				abort(403);
			}
    }
	
    public function edit(Port $port)
    {
		
			if(Auth::user()->role->role_permission('edit_port')){
				if(Auth::user()->role->role_permission('edit_port')->level == 'all'){
					
					$users = User::pluck('id');
					
					$port = Port::where('id', $port->id)->first();
						
				}elseif(Auth::user()->role->role_permission('edit_port')->level == 'team'){
					
					$port = Port::where('id', $port->id)->first();
				}
				
				
				$portTypes = PortType::lists('name', 'id')->toArray();
                $userSelect = User::select(DB::raw("CONCAT(first_name, ' ', last_name) AS full_name, id"))->lists('full_name', 'id')->toArray();
				return view('ports.edit', compact('port', 'users','portTypes','userSelect'));
			}else{
				abort(403);
			}
    }
	
    public function update(PortRequest $request, Port $port)
    {
		
			if(Auth::user()->role->role_permission('edit_port')){
				$port->update($request->all());
                return redirect('ports')->withSuccess('Port has been updated.');
			}else{
				abort(403);
			}
    }
	
    public function destroy(Port $port)
    {
			if(Auth::user()->role->role_permission('delete_port')){
				$port->delete();
				return redirect('ports')->withSuccess('Port has been deleted.');
			}else{
				abort(403);
			}
    }
}
