<?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Biblioteca;
use Illuminate\Http\Request;
use \App\Log;
use App\Rules\SecureImage;

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

        $objs = Biblioteca::orderBy('id', 'DESC')->paginate(30);
        $caminhos = [
            ['url' => '/admin', 'titulo' => 'Admin'],
            ['url' => '', 'titulo' => 'Biblioteca Ícones'],
        ];
        return view('admin.biblioteca.index', ['objs' => $objs, 'caminhos' => $caminhos]);
    }

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

        // if (Gate::denies('biblioteca-create')){
        //     abort(403, "Não bibliotecaizado");
        // }

        $caminhos = [
            ['url' => '/admin', 'titulo' => 'Admin'],
            ['url' => route('biblioteca.index'), 'titulo' => 'Biblioteca Ícones'],
            ['url' => '', 'titulo' => 'Adicionar'],
        ];
        return view('admin.biblioteca.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('biblioteca-create')){
        //     abort(403, "Não bibliotecaizado");
        // }

        $request->validate([
            'name' => 'required|max:255',
            'image' => ['file',
                        new SecureImage,
                ],
        ]);

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

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

        $logs = new Log();
        $logs->insertLog('Biblioteca', $obj->id);
        
        return redirect()->route('biblioteca.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('biblioteca-edit')) {
        //     abort(403, "Não bibliotecaizado");
        // }        

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

        $caminhos = [
            ['url' => '/admin', 'titulo' => 'Admin'],
            ['url' => route('biblioteca.index'), 'titulo' => 'Biblioteca'],
            ['url' => '', 'titulo' => 'Editar'],
        ];
        return view('admin.biblioteca.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('biblioteca-edit')) {
        //     abort(403, "Não bibliotecaizado");
        // }        

        $request->validate([
            'name' => 'required|max:255',
            'image' => ['file',
                        new SecureImage,
                ],
        ]);

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

        $nameFile = '';

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

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

        return redirect()->route('biblioteca.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('biblioteca-delete')){
        //     abort(403, "Não bibliotecaizado");
        // }  

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

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

    }
}
