<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Port;
use App\Festive;
use App\Http\Requests\FestiveRequest;
use Auth;

class FestiveController extends Controller
{
	public function __construct()
    {
        $this->middleware('auth');
    }
	
    public function index()
    {
		if(Auth::user()->role->role_permission('view_festive')){
			$festive = Festive::orderBy('created_at', 'desc')->paginate(30);
			return view('festive.index', compact('festive'));
		}else{
			abort(403);
		}
    }
	
	public function search(Request $request)
	{
		if(Auth::user()->role->role_permission('view_festive')){
			$festive = Festive::where('name', 'LIKE', '%'. $request->get('term') .'%')->paginate(30);
			return view('festive.index', compact('festive'));
		}else{
			abort(403);
		}
	}
    
    public function create()
    {
		if(Auth::user()->role->role_permission('create_festive')){
			$port = ['NULL'=>'-'] + Port::lists('name', 'id')->toArray();
			return view('festive.create', compact('port'));
		}else{
			abort(403);
		}
    }
	
    public function store(FestiveRequest $request)
    {
		if(Auth::user()->role->role_permission('create_festive')){
				
			$festive = Festive::create($request->all());
		
			return redirect('festives')->withSuccess($festive->title.' has been saved.');
		}else{
			abort(403);
		}
    }
	
    public function show(Festive $festive)
    {
		if(Auth::user()->role->role_permission('view_festive')){
			return view('festive.show', compact('festive'));
		}else{
			abort(403);
		}
    }
	
    public function edit(Festive $festive)
    {
		if(Auth::user()->role->role_permission('edit_festive')){
			$port =  [''=>'-'] + Port::lists('name', 'id')->toArray();
			return view('festive.edit', compact('festive','port'));
		}else{
			abort(403);
		}
    }
	
    public function update(FestiveRequest $request, Festive $festive)
    {
		if(Auth::user()->role->role_permission('edit_festive')){
			$festive->update($request->all());
			return redirect('festives')->withSuccess($festive->title.' has been updated.');
		}else{
			abort(403);
		}
    }
	
    public function destroy(Festive $festive)
    {
		if(Auth::user()->role->role_permission('delete_festive')){
			$festive->delete();
			return redirect('festives')->withSuccess($festive->title.' has been deleted.');
		}else{
			abort(403);
		}
    }
}
