<?php namespace App\Http\Controllers;

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

use App\Models\Visitor;
use App\Repositories\VisitorRepository;
use Illuminate\Http\Request;

class VisitorsController extends Controller {

    private $visitorRepository;

    public function __construct(VisitorRepository $visitorRepository)
    {
        $this->visitorRepository=$visitorRepository;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $visitor = new Visitor();
        $visitors=$this->visitorRepository->all();
        return view('visitors.index' , compact('visitors','visitor'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {

        return view('visitors.create', compact('visitor'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store(Requests\CreateVisitorRequest $request)
    {
        $visitor = $this->visitorRepository->create($request->all());
        if(!$visitor)
            return "404";
        return redirect(route('visitor.index'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $visitor = $this->visitorRepository->find($id);
        return view('visitors.show' , compact('visitor'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $visitor = $this->visitorRepository->find($id);
        return view('visitors.edit' , compact('visitor'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update(Requests\EditVisitorRequest $request)
    {
        $array=$request->all();
        unset($array['_method']);
        unset($array['_token']);
        $visitor = $this->visitorRepository->update($array,$request->get('id'));
        if  (!$visitor)
            return abort(403, 'Unauthorized action.');

        return redirect(route('visitor.index'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        $this->visitorRepository->delete($id);
        return redirect(route('visitor.index'));
    }

}
