<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Section;
use Illuminate\Http\Request;
use App\Page;

class SectionsController extends PostsController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $query = Section::query()->with('post');
        if($request->has('q')) {
            $query->whereHas('post', function($q) use($request) {
                $q->where('title', 'LIKE', "%{$request->q}%");
            });
        }
        if($request->has('page_id')) {
            $query->whereIn('id', $request->page_id);
        }

        $pages = Page::get()->sortBy('post.order');

        $sections = $query->orderBy('selector')->paginate(10);

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

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

        return view('backend.sections.form', [
            'section' => $section,
            'pages' => $pages,
            'selectors' => $selectors,
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        try {
            $section = Section::create($request->only('page_id', 'selector', 'url'));
            $section->post()->create($request->post);

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

            return redirect()->route('backend.sections.edit', $section->id)->with('status', 'La sección ha sido creada');
        } catch(\Exception $e) {
            return back()->withInput()->withErrors($e->getMessage());
        }
    }

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

        return view('backend.sections.form', [
            'section' => $section,
            'selectors' => $selectors,
            'pages' => $pages,
        ]);
    }

    /**
     * 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)
    {
        try {
            $section->fill($request->only('page_id', 'selector', 'url'))->save();
            $section->post->fill($request->post)->save();

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

            return back()->with('status', 'La sección ha sido editada');
        } catch(\Exception $e) {
            return back()->withInput()->withErrors($e->getMessage());
        }
    }

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

            return redirect()->route('backend.sections.index')->with('status', 'La sección ha sido eliminada');
        } catch(\Exception $e) {
            return back()->withInput()->withErrors($e->getMessage());
        }
    }
}
