<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Answer;
use App\Http\Requests\AnswerRequest;
use App\User;
use App\Question;
use Auth;

class AnswerController extends Controller
{
	public function __construct()
    {
        $this->middleware('auth');
    }
	
    public function index(request $request)
    {
		if(Auth::user()->role->role_permission('view_answers')){
			if(Auth::user()->role->role_permission('view_answers')->level == 'all'){
				$answers = Answer::where('question_id', $request->question_id)->get();
			}

			$questionId = $request->question_id;

			return view('answers.index', compact('answers','questionId'));
		}else{
			abort(403);
		}
    }
    
    public function create(Request $request)
    {
		if(Auth::user()->role->role_permission('create_answers')){

			$questionId = $request->question_id;

			return view('answers.create', compact('questionId'));
		}else{
			abort(403);
		}
    }
	
    public function store(AnswerRequest $request, Question $question)
    {
		if(Auth::user()->role->role_permission('create_answers')){
			$answer = Answer::create($request->all());
			return redirect('questions/' . (isset($question->id) ? $question->id : $request->question_id ) . '/answers')->withSuccess('Answer has been saved.');
			//return redirect()->action('QuestionController@index', ['question_id' => $request->question_id])->withSuccess('Answer has been saved.');
		}else{
			abort(403);
		}
    }
	
    public function show(Answer $answer)
    {
			if(Auth::user()->role->role_permission('view_answers')){
				if(Auth::user()->role->role_permission('view_answers')->level == 'team'){
					$answer = Answer::where('id', $answer->id)->first();
				}elseif(Auth::user()->role->role_permission('view_answers')->level == 'self'){
					$answer = Answer::where('id', $answer->id)->first();
				}
				return view('answer.show', compact('answer'));
			}else{
				abort(403);
			}
    }
	
    public function edit(Answer $answer)
    {
		//dd($answer);

		if(Auth::user()->role->role_permission('edit_answers')){
			if(Auth::user()->role->role_permission('edit_answers')->level == 'team'){
				$answer = Answer::where('id', $answer->id)->first();
			}elseif(Auth::user()->role->role_permission('edit_answers')->level == 'self'){
				$answer = Answer::where('id', $answer->id)->first();
			}
			return $answer ? view('answers.edit', compact('answer')) : abort(403);
		}else{
			abort(403);
		}
    }
	
    public function update(AnswerRequest $request)
	    {
			if(Auth::user()->role->role_permission('edit_answers')){
				$answer = Answer::find($request->id);
				$answer->update($request->all());
				return redirect('questions/' . (isset($question->id) ? $question->id : $request->question_id ) . '/answers')->withSuccess('Answer has been saved.');
			}else{
				abort(403);
			}
    }
	
    public function destroy(Answer $answer)
    {
		if(Auth::user()->role->role_permission('delete_answers')){
			$answer->delete();
			return redirect('questions/answers')->withSuccess('Answer has been deleted.');
		}else{
			abort(403);
		}
    }
}
