<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\AdminController;
use App\Http\Requests\StoreBlogCommentRequest;
use App\Http\Requests\UpdateBlogCommentRequest;
use App\Models\BlogComment;

class BlogCommentController extends AdminController {
	protected $entity = 'blogComment';

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

		$pagePayload->title = 'Lista de Comentários';
		$pagePayload->dataTable = new \stdClass();
		$pagePayload->dataTable->active = new \stdClass();
		$pagePayload->dataTable->approved = new \stdClass();
		$pagePayload->dataTable->inactive = new \stdClass();

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

		$pagePayload->dataTable->active->headers =
		$pagePayload->dataTable->approved->headers =
		$pagePayload->dataTable->inactive->headers = [
			[ 'data' => 'id', 'title' => 'ID' ],
			[ 'data' => 'blog.title', 'title' => 'Postagem' ],
			[ 'data' => 'name', 'title' => 'Nome' ],
			[ 'data' => 'email', 'title' => 'Email' ],
			[ 'data' => 'created_at', 'title' => 'Criado em' ],
		];

		$pagePayload->dataTable->approved->headers[] = [ 'data' => 'updated_at', 'title' => 'Aprovado em' ];
		$pagePayload->dataTable->inactive->headers[] = [ 'data' => 'deleted_at', 'title' => 'Recusado em' ];

		$pagePayload->dataTable->active->headers[] = [
			'title' => 'Ações',
			'width' => '50px',
			'renderDoT' => '
				<div class="flex justify-around">
					<a href="/admin/' . $this->entity . '/{{= it.row.id }}/disable">
						<i class="fas fa-times" title="Recusar"></i>
					</a>
					<a href="/admin/' . $this->entity . '/{{= it.row.id }}/enable">
						<i class="fas fa-check-circle" title="Aprovar"></i>
					</a>
					<a href="/admin/' . $this->entity . '/{{= it.row.id }}/edit">
						<i class="fas fa-eye" title="Visualizar"></i>
					</a>
				</div>
			',
		];

		$pagePayload->dataTable->approved->headers[] = [
			'title' => 'Ações',
			'width' => '50px',
			'renderDoT' => '
				<div class="flex justify-around">
					<a href="/admin/' . $this->entity . '/{{= it.row.id }}/disable">
						<i class="fas fa-times" title="Recusar"></i>
					</a>
					<a href="/admin/' . $this->entity . '/{{= it.row.id }}/edit">
						<i class="fas fa-eye" title="Visualizar"></i>
					</a>
				</div>
			',
		];

		$pagePayload->dataTable->inactive->headers[] = [
			'title' => 'Ações',
			'width' => '50px',
			'renderDoT' => '
				<div class="flex justify-around">
					<a href="/admin/' . $this->entity . '/{{= it.row.id }}/enable">
						<i class="fas fa-check-circle" title="Habilitar"></i>
					</a>
				</div>
			',
		];

		$model = BlogComment::query()->with('blog');

		$pagePayload->dataTable->active->rows = (clone $model)->where('approved', 0)->get();
		$pagePayload->dataTable->approved->rows = (clone $model)->where('approved', 1)->get();
		$pagePayload->dataTable->inactive->rows = (clone $model)->onlyTrashed()->get();

		// echo '<pre>'; die(json_encode($pagePayload->dataTable, 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();

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

	public function store(StoreBlogCommentRequest $request) {
		$payload = new BlogComment($request->all());

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

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

	public function edit(BlogComment $blogComment) {
		$pagePayload = new \stdClass();

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

		$pagePayload->data = $blogComment;

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

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

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

	public function enable(BlogComment $blogComment) {
		$blogComment->approved = true;
		$blogComment->save();
		return redirect()->route("admin.{$this->entity}.index");
	}

	public function disable(BlogComment $blogComment) {
		$blogComment->approved = false;
		$blogComment->delete();
		return redirect()->route("admin.{$this->entity}.index");
	}
}
