<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreUpdateTituloRequest;
use App\Models\Titulo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use function GuzzleHttp\Promise\all;

class TituloController extends Controller
{
    protected $request;
    //Injeção de dependência
    public function __construct(Request $request,  Titulo $titulo)
    {
        $this->request = $request;
        $this->repository = $titulo;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $titulos = Titulo::paginate();
       
        return view('admin.pages.titulos.index', [
         'titulos' => $titulos
       ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.pages.titulos.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  App\Http\Requests\StoreUpdateTituloRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreUpdateTituloRequest $request)
    {
        $data = $request->all();
        $this->repository->create($data);
        return redirect()->route('titulos.index');
        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        if(!$titulo = Titulo::find($id))
            return redirect()->back();
        
        return view('admin.pages.titulos.show', [
            'titulos' => $titulo
        ]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        if(!$titulo = $this->repository->find($id))
        return redirect()->back();
        //dd($titulo);
        // return view('admin.pages.titulos.edit', [
        //     'titulos' => $titulo
        // ]);
        return view('admin.pages.titulos.edit', compact('titulo'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(StoreUpdateTituloRequest $request, $id)
    {
        if(!$titulo = $this->repository->find($id))
        return redirect()->back();

        $titulo->update($request->all());
        return redirect()->route('titulos.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if(!$titulo = $this->repository->find($id))
            return redirect()->back();
        $titulo->delete();

        return redirect()->route('titulos.index');
    }

    public function search(Request $request)
    {
        $filters = $request->except('_token');

        $titulo = $this->repository->search($request->filter);

        return view('admin.pages.titulos.index', [
            'titulos' => $titulo,
            'filters' => $filters,
          ]);

    }
}
