<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\AdminController;
use App\Http\Requests\StoreBlogTagRequest;
use App\Http\Requests\UpdateBlogTagRequest;
use App\Models\BlogTag;

class BlogTagController extends AdminController {
	protected $entity = 'blogTag';
	/**
	 * Display a listing of the resource.
	 */
	public function index() {
		$pagePayload = new \stdClass();

		$pagePayload->title = 'Lista de Tags';
		$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 = [
				['title' => 'ID', 'data' => 'id'],
				['title' => 'Nome', 'data' => 'name'],
				$this->getActionsBtn(),
			];

		$pagePayload->dataTable->active->rows = BlogTag::query()->get();
		$pagePayload->dataTable->inactive->rows = BlogTag::query()->onlyTrashed()->get();

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

	/**
	 * Show the form for creating a new resource.
	 */
	public function create() {
		$pagePayload = new \stdClass();

		$pagePayload->title = 'Nova Tag';
		$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]);
	}

	/**
	 * Store a newly created resource in storage.
	 */
	public function store(StoreBlogTagRequest $request) {
		$company = new BlogTag($request->all());

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

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

	/**
	 * Display the specified resource.
	 */
	public function show(BlogTag $blogTag) {
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 */
	public function edit(BlogTag $blogTag) {
		$pagePayload = new \stdClass();

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

		$pagePayload->data = $blogTag;

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

	/**
	 * Update the specified resource in storage.
	 */
	public function update(UpdateBlogTagRequest $request, BlogTag $blogTag) {
		if ($blogTag->update($request->all())) {
			return redirect()->route("admin.{$this->entity}.index");
		}

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

	/**
	 * Remove the specified resource from storage.
	 */
	public function destroy(BlogTag $blogTag) {
		//
	}

	/**
	 * Enable the specified resource.
	 */
	public function enable(BlogTag $blogTag) {
		$blogTag->restore();
		return redirect()->route("admin.{$this->entity}.index");
	}

	/**
	 * Disable the specified resource.
	 */
	public function disable(BlogTag $blogTag) {
		$blogTag->delete();
		return redirect()->route("admin.{$this->entity}.index");
	}
}
