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

    public function index(Section $section)
    {

        if ($section->layout == 'layout_1')
        {
            $aDatas['300x75'] = array();
            $aDatas['300x75 Sotto'] = array();
            $aDatas['300x600'] = array();                        
        }
        if ($section->layout == 'layout_2')
        {
            $aDatas['300x600'] = array();
            $aDatas['300x75'] = array();      
            $aDatas['300x75 Sotto'] = array();      
        }
        if ($section->layout == 'layout_3')
        {
            $aDatas['300x600'] = array();
            $aDatas['300x250'] = array();
        }
        if ($section->layout == 'layout_4')
        {
            $aDatas['300x250 Sopra'] = array();
            $aDatas['300x250 Centro'] = array();
            $aDatas['300x250 Sotto'] = array();        
            $aDatas['300x250 Quarto'] = array();        
            // $aDatas['300x75'] = array(); 
        }
        if ($section->layout == 'layout_5')
        {
            $aDatas['300x75'] = array(); 
            $aDatas['300x250 Sopra'] = array();
            $aDatas['300x250 Centro'] = array();
            $aDatas['300x250 Sotto'] = array();            
        }
        if ($section->layout == 'layout_6')
        {
            $aDatas['300x250'] = array();            
            $aDatas['300x250 Sotto'] = array();            
            // $aDatas['300x75'] = array();            
        }
        if ($section->layout == 'layout_7')
       {
            $aDatas['300x250 Sopra'] = array();
            $aDatas['300x75 Sopra'] = array();
            $aDatas['300x75 Centro'] = array();
            $aDatas['300x75 Sotto'] = array();
            $aDatas['300x75 Quarto'] = array();
            $aDatas['300x250 Sotto'] = array();
        }
        
        $sections_advs = SectionAdv::where('section_id', '=', $section->id)->orderBy('position')->get();
        foreach($sections_advs as $a)
        {
            if (array_key_exists($a->position, $aDatas))
                $aDatas[$a->position][] = $a;
        }
        
        return view('sections_advs.index',compact('aDatas', 'section'));
            // ->with('i', (request()->input('page', 1) - 1) * 5);
    }
   
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Section $section)
    {
        return view('sections_advs.create', compact('section'));
    }
  
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, Section $section, SectionAdv $section_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["section_id"] = $section->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;

        }

        SectionAdv::create($input);
   
        return redirect()->route('sections.advs.index', compact('section'))
                        ->with('success','Section created successfully.');
    }
   
    /**
     * Display the specified resource.
     *
     * @param  \App\Section  $section
     * @return \Illuminate\Http\Response
     */
    public function show(SectionAdv $section_adv, Section $section)
    {
        return view('sections_advs.show',compact('section'));
    }
   
    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Section  $section
     * @return \Illuminate\Http\Response
     */
    public function edit(Section $section, SectionAdv $adv)
    {
        return view('sections_advs.edit',compact('section', 'adv'));
    }
  
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Section  $section
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Section $section, SectionAdv $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('sections.advs.index', compact('section'))
                        ->with('success','Section adv updated successfully');
    }
  
    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Section  $section
     * @return \Illuminate\Http\Response
     */
    public function destroy(Section $section, SectionAdv $adv)
    {
        $adv->delete();
  
        return redirect()->route('sections.advs.index', compact('section'))
                        ->with('success','Section adv deleted successfully');
    }
}