<?php

namespace App\Http\Controllers;

use App\Page;
use App\PageAdv;
use App\News;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;

class PageController extends Controller
{

    protected $rules = [
        'title' => ['required']
    ];

    public function index()
    {
        $pages = Page::orderBy('title')->get(); //->paginate(50);

        return view('pages.index',compact('pages'))
            ->with('i', (request()->input('page', 1) - 1) * 5);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('pages.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"] = 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/pages"))
                File::makeDirectory(public_path()."/files/pages");

            $path = public_path('files/pages');

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

            $input["image"] = $filename;

        }

        Page::create($input);

        return redirect()->route('pages.index')
                        ->with('success','Page created successfully.');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Page  $page
     * @return \Illuminate\Http\Response
     */
    public function show(Page $page)
    {
        return view('pages.show',compact('page'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Page  $page
     * @return \Illuminate\Http\Response
     */
    public function edit(Page $page)
    {
        return view('pages.edit',compact('page'));
    }

    /**
     * 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)
    {
        $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/pages"))
                File::makeDirectory(public_path()."/files/pages");

            $path = public_path('files/pages');

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

            $input["image"] = $filename;

        }

        $page->update($input);

        return redirect()->route('pages.index')
                        ->with('success','Page updated successfully');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Page  $page
     * @return \Illuminate\Http\Response
     */
    public function destroy(Page $page)
    {

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

        $advs = PageAdv::where('page_id', '=', $page->id)->get();
        foreach($advs as $adv)
        {
            $adv->page_id = null;
            $adv->save();
        }

        $page->delete();

        return redirect()->route('pages.index')
                        ->with('success','Page deleted successfully');
    }
}
