<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreUpdateAgendamentoRequest;
use App\Models\Agendamento;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AgendamentoController extends Controller
{

    protected $request;
    private $repository;

    public function __construct(Request $request,  Agendamento $agendamento)
    {
        $this->request = $request;
        $this->repository = $agendamento;
        //$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()
    {
        
        $agendamentos = Agendamento::get();
        foreach($agendamentos as $agendamento){
            return view('admin.pages.agendamentos.index', [
                'agendamentos' => $agendamentos
              ]);
        }
        
    }

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

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

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

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

        return view('admin.pages.agendamentos.edit', compact('agendamento'));
    }

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

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

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

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

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

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

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

    }
}
