<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Incidence;
use App\Http\Requests\IncidenceRequest;
use Illuminate\Support\Facades\DB;
use Auth;
use App\User;

class IncidenceController extends Controller
{
		public function __construct()
    {
        $this->middleware('auth');
    }
	
    public function index()
    {
			if(Auth::user()->role->role_permission('view_incidence')){
				if(Auth::user()->role->role_permission('view_reservation')->level == 'all'){
						$incidences = Incidence::orderBy('date', 'desc')->paginate(30);
				}else{
					$incidences = Incidence::where('user_id', Auth::user()->id)->orderBy('date', 'desc')->paginate(30);
				}
				return view('incidences.index', compact('incidences'));
			}else{
				abort(403);
			}
    }
	
		public function search(Request $request)
		{
			if(Auth::user()->role->role_permission('view_incidence')){
				$incidences = Incidence::where('subject', 'LIKE', '%'. $request->get('term') .'%')->paginate(30);
				return view('incidences.index', compact('incidences'));
			}else{
				abort(403);
			}
		}
    
    public function create()
    {
			if(Auth::user()->role->role_permission('create_incidence')){
	
			if(Auth::user()->role->role_permission('view_reservation')->level == 'all'){
                 		$users = ["" => ""] + User::select(DB::raw('CONCAT(first_name, " ",last_name) AS fullname, id'))
				->lists('fullname', 'id')->toArray();

			}else{
				$users = ["" => ""] + User::where('id', Auth::user()->id)->select(DB::raw('CONCAT(first_name, " ",last_name) AS fullname, id'))
				->lists('fullname', 'id')->toArray();
			}

		 $incidences = Incidence::where('user_id', Auth::user()->id)->orderBy('date', 'desc')->first();
				return view('incidences.create', compact('users'));
			}else{
				abort(403);
			}
    }
	
    public function store(IncidenceRequest $request)
		{
			if(Auth::user()->role->role_permission('create_incidence')){
				$incidences = Incidence::create($request->all());
				return redirect('incidences')->withSuccess($incidences->subject.' has been saved.');
			}else{
				abort(403);
			}
    }
	
    public function show(Incidence $incidences)
    {
			if(Auth::user()->role->role_permission('view_incidence')){
				return view('incidences.show', compact('incidences'));
			}else{
				abort(403);
			}
    }
	
    public function edit(Incidence $incidences)
    {
			if(Auth::user()->role->role_permission('edit_incidence')){
			    
			    if(Auth::user()->role->role_permission('view_reservation')->level == 'all'){
                 		$users = ["" => ""] + User::select(DB::raw('CONCAT(first_name, " ",last_name) AS fullname, id'))
				->lists('fullname', 'id')->toArray();

			}else{
				$users = ["" => ""] + User::where('id', Auth::user()->id)->select(DB::raw('CONCAT(first_name, " ",last_name) AS fullname, id'))
				->lists('fullname', 'id')->toArray();
			}
			    
				return view('incidences.edit', compact('incidences','users'));
			}else{
				abort(403);
			}
    }
	
    public function update(IncidenceRequest $request, Incidence $incidences)
    {
			if(Auth::user()->role->role_permission('edit_incidence')){
				$incidences->update($request->all());
				return redirect('incidences')->withSuccess($incidences->subject.' has been updated.');
			}else{
				abort(403);
			}
    }
	
    public function destroy(Incidence $incidences)
    {
			if(Auth::user()->role->role_permission('delete_incidence')){
				$incidences->delete();
				return redirect('incidences')->withSuccess($incidences->subject.' has been deleted.');
			}else{
				abort(403);
			}
    }

    public function markIncidence(Request $request)
    {
        $incidence = Incidence::where('id', $request->incidence)->first();
	$incidence->read = 1;
	$incidence->save();
	return redirect('incidences')->withSuccess($incidence->subject.' has been readed.');

    }

}
