<?php
  
namespace App\Http\Controllers;
  
use App\Event;
use App\EventAdv;
use Illuminate\Http\Request;
use File;
  
class EventAdvController extends Controller
{
    
    protected $rules = [
        // 'name' => ['required']
    ];

    public function index(Event $event)
    {

        $aDatas['Sopra 234x60 1'] = array();
        $aDatas['Sopra 234x60 2'] = array();
        $aDatas['Sopra 234x60 3'] = array();
        $aDatas['Sopra 234x60 4'] = array();
        $aDatas['Sotto 234x60 1'] = array();
        $aDatas['Sotto 234x60 2'] = array();
        $aDatas['Sotto 234x60 3'] = array();
        $aDatas['Sotto 234x60 4'] = array();
        $aDatas['728x90'] = array();

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

        if(request()->image)
        {

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

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

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

            $input["image"] = $filename;

        }
        if(request()->jingle)
        {

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

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

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

            $input["jingle"] = $filename;

        }

        EventAdv::create($input);
   
        return redirect()->route('events.advs.index', compact('event'))
                        ->with('success','Event created successfully.');
    }
   
    /**
     * Display the specified resource.
     *
     * @param  \App\Event  $event
     * @return \Illuminate\Http\Response
     */
    public function show(EventAdv $event_adv, Event $event)
    {
        return view('events_advs.show',compact('event'));
    }
   
    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Event  $event
     * @return \Illuminate\Http\Response
     */
    public function edit(Event $event, EventAdv $adv)
    {
        return view('events_advs.edit',compact('event', 'adv'));
    }
  
    /**
     * 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, EventAdv $adv)
    {
        $request->validate($this->rules);
  
        $input = $request->all();
        $input["name"] = $input["name"] == null ? '' : $input["name"];
        $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/adv"))
                File::makeDirectory(public_path()."/files/adv");
            
            $path = public_path('files/adv');

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

            $input["image"] = $filename;

        }
        if(request()->jingle)
        {

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

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

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

            $input["jingle"] = $filename;

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