<?php
  
namespace App\Http\Controllers;
  
use App\Event;
use App\News;
use Illuminate\Http\Request;
use File;
  
class EventController extends Controller
{
    
    protected $rules = [
        'title' => ['required']
    ];

    public function index()
    {
        $events = Event::orderBy('title')->get(); //->paginate(50);
  
        return view('events.index',compact('events'))
            ->with('i', (request()->input('event', 1) - 1) * 5);
    }
   
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('events.create');
    }
  
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate($this->rules);
  
        $input = $request->all();
        $input["online"] = true; //isset($input["online"]) ? ($input["online"] == 'on' ? true : false) : false;

        if(request()->image)
        {

            $file = request()->image;
            $filename  = time() . '_' . $file->getClientOriginalName();

            if (! File::exists(public_path()."/files/events"))
                File::makeDirectory(public_path()."/files/events");
            
            $path = public_path('files/events');

            request()->image->move($path, $filename);

            $input["image"] = $filename;

        }

        Event::create($input);
   
        return redirect()->route('events.index')
                        ->with('success','Event created successfully.');
    }
   
    /**
     * Display the specified resource.
     *
     * @param  \App\Event  $event
     * @return \Illuminate\Http\Response
     */
    public function show(Event $event)
    {
        return view('events.show',compact('event'));
    }
   
    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Event  $event
     * @return \Illuminate\Http\Response
     */
    public function edit(Event $event)
    {
        return view('events.edit',compact('event'));
    }
  
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Event  $event
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Event $event)
    {
        $request->validate($this->rules);
  
        $input = $request->all();
        $input["online"] = isset($input["online"]) ? ($input["online"] == 'on' ? true : false) : false;

        if(request()->image)
        {

            $file = request()->image;
            $filename  = time() . '_' . $file->getClientOriginalName();

            if (! File::exists(public_path()."/files/events"))
                File::makeDirectory(public_path()."/files/events");
            
            $path = public_path('files/events');

            request()->image->move($path, $filename);

            $input["image"] = $filename;

        }
        
        $event->update($input);
  
        return redirect()->route('events.index')
                        ->with('success','Event updated successfully');
    }
  
    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Event  $event
     * @return \Illuminate\Http\Response
     */
    public function destroy(Event $event)
    {

        $event->advs()->delete();

        $news = News::where('event_id', '=', $event->id)->get();
        foreach($news as $n)
        {
            $n->event_id = null;
            $n->save();
        }

        $event->delete();
  
        return redirect()->route('events.index')
                        ->with('success','Event deleted successfully');
    }
}