<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\AdminController;
use App\Http\Requests\StoreBlogRequest;
use App\Http\Requests\UpdateBlogRequest;
use App\Models\Blog;
use App\Models\BlogCategory;

class BlogController extends AdminController {
	protected $entity = 'blog';

	public function index() {
		$pagePayload = new \stdClass();

		$pagePayload->title = 'Lista de Posts';
		$pagePayload->dataTable = new \stdClass();
		$pagePayload->dataTable->active = new \stdClass();
		$pagePayload->dataTable->inactive = new \stdClass();

		$pagePayload->dataTable->active->title = 'Ativas';
		$pagePayload->dataTable->inactive->title = 'Inativas';

		$pagePayload->dataTable->active->headers =
		$pagePayload->dataTable->inactive->headers = [
			[ 'data' => 'id', 'title' => 'ID' ],
			[ 'data' => 'image', 'title' => 'Img' ],
			[ 'data' => 'count_views', 'title' => 'Visualizações' ],
			[ 'data' => 'count_comments', 'title' => 'Comentários' ],
			[ 'data' => 'count_likes', 'title' => 'Likes' ],
			[ 'data' => 'blog_category.name', 'title' => 'Categoria' ],
			[ 'data' => 'title', 'title' => 'Título' ],
			[ 'data' => 'subtitle', 'title' => 'Subtítulo' ],
			[ 'data' => 'scheduling_date', 'title' => 'Agendado para' ],
			[ 'data' => 'created_at', 'title' => 'Criado em' ],
		];

		$pagePayload->dataTable->active->headers[] = [ 'data' => 'updated_at', 'title' => 'Atualizado em' ];
		$pagePayload->dataTable->inactive->headers[] = [ 'data' => 'deleted_at', 'title' => 'Deletado em' ];

		$pagePayload->dataTable->active->headers[] = $this->getActionsBtn();
		$pagePayload->dataTable->inactive->headers[] = $this->getActionsBtn();

		$model = Blog::query()->with('blogCategory');

		$pagePayload->dataTable->active->rows = (clone $model)->get();
		$pagePayload->dataTable->inactive->rows = (clone $model)->onlyTrashed()->get();

		// echo '<pre>'; die(json_encode($pagePayload->dataTable->active->rows, JSON_PRETTY_PRINT));

		return view("admin.pages.{$this->entity}.index", ['pagePayload' => $pagePayload]);
	}

	public function create() {
		$pagePayload = new \stdClass();

		$pagePayload->title = 'Novo Post';
		$pagePayload->route = new \stdClass();
		$pagePayload->route->name = "admin.{$this->entity}.store";
		$pagePayload->route->method = 'POST';

		$pagePayload->data = new \stdClass();
		$pagePayload->comboBox = new \stdClass();
		$pagePayload->comboBox->blogCategories = BlogCategory::query()->get();

		return view("admin.pages.{$this->entity}.form", ['pagePayload' => $pagePayload]);
	}

	public function store(StoreBlogRequest $request) {
		$payload = new Blog($request->all());

		if ($payload->save()) {
			return redirect()->route("admin.{$this->entity}.index");
		}

		return redirect()->back()->withInput(['data' => $request->all()]);
	}

	public function edit(Blog $blog) {
		$pagePayload = new \stdClass();

		$pagePayload->title = 'Editar Postagem';
		$pagePayload->route = new \stdClass();
		$pagePayload->route->name = "admin.{$this->entity}.update";
		$pagePayload->route->method = 'PUT';
		$pagePayload->route->params = [$this->entity => $blog->id];

		$pagePayload->data = $blog;
		$pagePayload->comboBox = new \stdClass();
		$pagePayload->comboBox->blogCategories = BlogCategory::query()->get();

		return view("admin.pages.{$this->entity}.form", ['pagePayload' => $pagePayload]);
	}

	public function update(UpdateBlogRequest $request, Blog $blog) {
		if ($blog->update($request->all())) {
			return redirect()->route("admin.{$this->entity}.index");
		}

		return redirect()->back()->withInput([ 'data' => $request->all() ]);
	}

	public function enable(Blog $blog) {
		$blog->restore();
		return redirect()->route("admin.{$this->entity}.index");
	}

	public function disable(Blog $blog) {
		$blog->delete();
		return redirect()->route("admin.{$this->entity}.index");
	}
}
