<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Article;
use Illuminate\Http\Request;
use App\Category;

class ArticlesController extends PostsController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $articles = Article::orderBy('published_at', 'desc')->paginate(10);

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Article $article)
    {
        $categories = Category::orderBy('order')->pluck('title', 'id')->toArray();
        $articles = Article::where('id', '!=', $article->id)->orderBy('published_at', 'desc')->take(200)->get()->pluck('select_title', 'id')->toArray();

        return view('backend.articles.form', [
            'article' => $article,
            'categories' => $categories,
            'articles' => $articles
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        try {
            $request->validate([
                'post.slug' => 'unique:posts,slug',
            ], [
                'post.slug.unique' => 'La URL '.$request->post['slug'].' ya está tomada'
            ]);

            $article = Article::create($request->only('content', 'dates', 'published_at'));
            $article->post()->create($request->post + [
                    'slug' => slug($request->post['title'])
                ]);

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

            $article->post->categories()->attach($request->category_id);

            return redirect()->route('backend.articles.edit', $article->id)->with('status', 'El artículo ha sido creado');
        } catch(\Exception $e) {
            return back()->withInput()->withErrors($e->getMessage());
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function edit(Article $article)
    {
        $categories = Category::orderBy('order')->pluck('title', 'id')->toArray();
        $articles = Article::where('id', '!=', $article->id)->orderBy('published_at')->get()->pluck('select_title', 'id')->toArray();

        return view('backend.articles.form', [
            'article' => $article,
            'categories' => $categories,
            'articles' => $articles
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Article $article)
    {
        try {
            $request->validate([
                'post.slug' => 'unique:posts,slug'
            ], [
                'post.slug.unique' => 'La URL '.$request->post['slug'].' ya está tomada'
            ]);

            $article->fill($request->only( 'published_at'))->save();
            $article->post->fill($request->post + [
                'slug' => slug($request->post['title'])
            ])->save();
            $article->post->categories()->sync($request->category_id);

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

            return back()->with('status', 'El artículo ha sido editado');
        } catch(\Exception $e) {
            return back()->withInput()->withErrors($e->getMessage());
        }
    }

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

            return redirect()->route('backend.articles.index')->with('status', 'El artículo ha sido eliminado');
        } catch(\Exception $e) {
            return back()->withInput()->withErrors($e->getMessage());
        }
    }
}
