<?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\SessaoNumeros;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use \App\Log;
use App\Rules\SecureImage;

class SessaoNumerosController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        if (Gate::denies('sessaonumeros-view')){
            abort(403, "Não sessaonumerosizado");
        }

        $objs = SessaoNumeros::orderBy('ordem', 'ASC')->orderBy("ordem", 'ASC')->paginate(30);
        $caminhos = [
            ['url' => '/admin', 'titulo' => 'Admin'],
            ['url' => '', 'titulo' => 'Sessão Números'],
        ];
        return view('admin.sessaonumeros.index', ['objs' => $objs, 'caminhos' => $caminhos]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

        if (Gate::denies('sessaonumeros-create')){
            abort(403, "Não sessaonumerosizado");
        }

        $caminhos = [
            ['url' => '/admin', 'titulo' => 'Admin'],
            ['url' => route('numeros.index'), 'titulo' => 'Sessão Numeros'],
            ['url' => '', 'titulo' => 'Adicionar'],
        ];
        return view('admin.sessaonumeros.adicionar', compact('caminhos'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        if (Gate::denies('sessaonumeros-create')){
            abort(403, "Não sessaonumerosizado");
        }

        
        $request->validate([
            'image' => ['file',
                        new SecureImage,
                ],
        ]);


        $obj = new \App\SessaoNumeros();
        $nameFile = '';

        if ($request->hasFile('image') && $request->file('image')->isValid()) {
            $name = uniqid(date('HisYmd'));
            $extension = $request->image->getClientOriginalExtension();
            $nameFile = "{$name}.{$extension}";
            $upload = $request->image->storeAs(config('app.public').'sessaonumeros', $nameFile);
            if ( !$upload )
                return redirect()
                            ->back()
                            ->with('error', 'Falha ao fazer upload')
                            ->withInput();
        }  
        
        $obj->name = $request['name'];
        $obj->description = $request['description'];
        $obj->numero = $request['numero'];
        $obj->image = $nameFile;
        $obj->save();

        $logs = new Log();
        $logs->insertLog('SessaoNumeros', $obj->id);
        
        return redirect()->route('numeros.index')->with('status', 'Registro Salvo!');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        if (Gate::denies('sessaonumeros-edit')) {
            abort(403, "Não sessaonumerosizado");
        }        

        $obj = SessaoNumeros::find($id);

        $caminhos = [
            ['url' => '/admin', 'titulo' => 'Admin'],
            ['url' => route('numeros.index'), 'titulo' => 'SessaoNumeros'],
            ['url' => '', 'titulo' => 'Editar'],
        ];
        return view('admin.sessaonumeros.editar', compact('obj','caminhos'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

        if (Gate::denies('sessaonumeros-edit')) {
            abort(403, "Não sessaonumerosizado");
        }        

        $request->validate([
            'image' => ['file',
                        new SecureImage,
                ],
        ]);


        $obj = SessaoNumeros::find($id);

        $nameFile = '';

        if ($request->hasFile('image') && $request->file('image')->isValid()) {
            $name = uniqid(date('HisYmd'));
            $extension = $request->image->getClientOriginalExtension();
            $nameFile = "{$name}.{$extension}";
            $upload = $request->image->storeAs(config('app.folder').'sessaonumeros', $nameFile);
            if ( !$upload )
                return redirect()
                            ->back()
                            ->with('error', 'Falha ao fazer upload')
                            ->withInput();
        } else {
            $nameFile = $request['old_image'];
        }
        $obj->name = $request['name'];
        $obj->description = $request['description'];
        $obj->numero = $request['numero'];
        $obj->image = $nameFile;

        $obj->save();
        
        $logs = new Log();
        $logs->updateLog('SessaoNumeros', $id);

        return redirect()->route('numeros.index')->with('status', 'Registro Atualizado!');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if (Gate::denies('sessaonumeros-delete')){
            abort(403, "Não sessaonumerosizado");
        }  

        SessaoNumeros::find($id)->delete();

        $logs = new Log();
        $logs->deleteLog('SessaoNumeros', $id);
        
        return redirect()->route('numeros.index')->with('status', 'Registro Excluído!');

    }
}
