<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\AdminController;
use App\Http\Requests\StoreCompanyRequest;
use App\Http\Requests\UpdateCompanyRequest;
use Illuminate\Support\Facades\Cache;
use App\Models\Company;

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

		$pagePayload->title = 'Lista de Empresas';
		$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'],
			['title' => 'Endereço', 'data' => 'address'],
			['title' => 'Número', 'data' => 'number'],
			['title' => 'Complemento', 'data' => 'complement'],
			['title' => 'Bairro', 'data' => 'neighborhood'],
			['title' => 'Cidade', 'data' => 'city'],
			['title' => 'UF', 'data' => 'uf'],
			['title' => 'CEP', 'data' => 'zip_code'],
			['title' => 'Facebook', 'data' => 'facebook'],
			['title' => 'Twitter', 'data' => 'twitter'],
			['title' => 'Instagram', 'data' => 'instagram'],
			['title' => 'Pinterest', 'data' => 'pinterest'],
			['title' => 'LinkedIn', 'data' => 'linkedin'],
			['title' => 'YouTube', 'data' => 'youtube'],
			$this->getActionsBtn(),
		];

		$pagePayload->dataTable->active->rows = Company::query()->get();
		$pagePayload->dataTable->inactive->rows = Company::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 Empresa';
		$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(StoreCompanyRequest $request) {
		$company = new Company($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(Company $company) {
		//
	}

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

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

		$pagePayload->data = $company;

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

	/**
	 * Update the specified resource in storage.
	 */
	public function update(UpdateCompanyRequest $request, Company $company) {
		if ($company->update($request->all())) {
			Cache::put('company', $company, 60 * 60);
			return redirect()->route("admin.{$this->entity}.index");
		}

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

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

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

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