<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Backend\PostsController;
use App\Page;
use Image;
use Illuminate\Http\Request;
use DB;

class PagesController extends PostsController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $pages = Page::all()->sortBy('post.order');

        return view('backend.pages.index', [
            'pages' => $pages
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Page $page)
    {
        $pages = Page::all()->sortBy('post.order')->pluck('post.title', 'id')->toArray();
        $templates = config('cms.templates');
        ksort($templates);

        return view('backend.pages.form', [
            'page' => $page,
            'templates' => $templates,
            'pages' => $pages
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $page = Page::create($request->except('_token', '_method', 'post'));
        $page->post()->create($request->post + ['order' => Page::count()]);

        $this->uploadSingle($request, $page);

        return redirect()->route('backend.pages.edit', $page->id)->with('status', 'La página ha sido creada');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Page  $page
     * @return \Illuminate\Http\Response
     */
    public function edit(Page $page)
    {
        $pages = Page::where('id', '!=', $page->id)->get()->sortBy('post.order')->pluck('post.title', 'id')->toArray();
        $templates = config('cms.templates');
        ksort($templates);

        return view('backend.pages.form', [
            'page' => $page,
            'templates' => $templates,
            'pages' => $pages
        ]);
    }

    /**
     * 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)
    {
        $page->fill($request->except('_token', '_method', 'post'))->save();
        $page->post->fill($request->post)->save();

        $this->uploadSingle($request, $page);
        $this->upload($request->file('images'), $page);

        $status = 'La página ha sido editada';

        return back()->with('status', $status);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Page  $page
     * @return \Illuminate\Http\Response
     */
    public function destroy(Page $page)
    {
        $page->post->media()->delete();
        $page->post->blocks()->delete();
        $page->post()->delete();
        $page->delete();

        return redirect()->route('backend.pages.index')->with('status', 'La página ha sido eliminada');
    }
}
