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

    public function index(Page $page)
    {

        $aDatas['728x90'] = array();
        $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();
        

        $pages_advs = PageAdv::where('page_id', '=', $page->id)->orderBy('position')->orderBy('sort')->get();
        foreach($pages_advs as $a)
        {
            $aDatas[$a->position][] = $a;
        }
        
        return view('pages_advs.index',compact('aDatas', 'page'));
            // ->with('i', (request()->input('page', 1) - 1) * 5);
    }
   
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Page $page)
    {
        return view('pages_advs.create', compact('page'));
    }
  
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, Page $page, PageAdv $page_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["page_id"] = $page->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;

        }

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