<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreUpdateMunicipioRequest;
use App\Models\Municipio;
use Illuminate\Http\Request;

class MunicipioController extends Controller
{
    protected $request;
    private $repository;

    public function __construct(Request $request,  Municipio $municipio)
    {
        $this->request = $request;
        $this->repository = $municipio;
        //$this->middleware('auth');
        /*$this->middleware('auth')->only([
            'create', 'store'
        ]);
        $this->middleware('auth')->except([
            'index', 'show'
        ]);
        */
    }

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

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

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

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        if(!$municipio = $this->repository->find($id))
            return redirect()->back();

        return view('admin.pages.municipios.edit', compact('id'));
    }

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

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

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

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

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

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

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

    }
}
