<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreUpdateLoteRequest;
use App\Repositories\Contracts\LoteRepositoryInterface;
use Illuminate\Http\Request;

class LoteController extends Controller
{

    protected $repository;

    public function __construct(LoteRepositoryInterface $repository)
    {
        //dd($repository);
        $this->repository = $repository;
    }

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

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  App\Http\Requests\StoreUpdateLoteRequest;  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreUpdateLoteRequest $request)
    {
        $data = $request->all();
        $this->repository->store($data);
        return redirect()->route('lotes.index');
        // $this->repository->insert([
        //     'municipio_id' => $request->municipio_id,
        //     'numero' => $request->numero,
        //     'proprietario' => $request->proprietario,
        //     'cpf' => $request->cpf,
        //     'dhc' => $request->dhc,
        //     'dhm' => $request->dhm,
        //     'area' => $request->area,
        // ]);

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        if (!$repository = $this->repository->findById($id)) {
            return redirect()->back();
        }

        return view('admin.pages.lotes.show', [
            'lotes' => $repository,
        ]);
        // $repository = DB::table('sige.lotes')->where('id', $id)->first();
        //  if(!$repository)
        //     return redirect()->back();

    }

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

        return view('admin.pages.lotes.edit', compact('lote'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  App\Http\Requests\StoreUpdateLoteRequest;  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(StoreUpdateLoteRequest $request, $id)
    {
        $this->repository
            ->update($id, [
                'municipio_id' => $request->municipio_id,
            ]);

        // if (!$repository = $this->repository->findById($id)) {
        //     return redirect()->back();
        // }
        //$repository->update($request->all());

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

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

        $repository->delete();

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

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

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

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

    }
}
