<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreUpdateGlebaRequest;
use App\Models\Gleba;
use Illuminate\Http\Request;

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

    public function __construct(Request $request,  Gleba $gleba)
    {
        $this->request = $request;
        $this->repository = $gleba;
        //$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()
    {
        
        $gleba = Gleba::paginate();
        return view('admin.pages.glebas.index', [
         'glebas' => $gleba
       ]);
    }

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

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

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

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

        return view('admin.pages.glebas.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(StoreUpdateGlebaRequest $request, $id)
    { 
        if(!$gleba = $this->repository->find($id))
        return redirect()->back();

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

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

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

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

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

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

    } 
}
