<?php


namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Gate;
use App\MatCategoria;
use App\Log;

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

        if (isset($request['search'])) {
            $objs = MatCategoria::where('language_id', '=', $request['search'] )->orderBy('id', 'DESC')->paginate(30);    
        } else {
            $objs = MatCategoria::orderBy('id', 'DESC')->paginate(30);
        }

        //$objs = MatCategoria::orderBy('id', 'DESC')->paginate(30);
        $caminhos = [
            ['url' => '/admin', 'titulo' => 'Admin'],
            ['url' => '', 'titulo' => 'Categorias'],
        ];
        return view('admin.mat-categorias.index', compact('objs', 'caminhos'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        if (Gate::denies('matCategoria-create')){
            abort(403, "Não autorizado");
        }

        $caminhos = [
            ['url' => '/admin', 'titulo' => 'Admin'],
            ['url' => route('mat-categoria.index'), 'titulo' => 'Material Educativo Categoria'],
            ['url' => '', 'titulo' => 'Adicionar'],
        ];
        return view('admin.mat-categorias.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('produtos-create')){
        //     abort(403, "Não autorizado");
        // }                

        $request->validate([
            'title' => 'required|max:255',
            'language_id' => 'required',
        ]);

        $obj = new \App\MatCategoria();
      
        $obj->title = $request['title'];
        $obj->language_id = $request['language_id'];
        
        $obj->save();
                
        $logs = new Log();
        $logs->insertLog('Mat. Categoria', $obj->id);        
       
        return redirect()->route('mat-categoria.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('matCategoria-edit')){
            abort(403, "Não autorizado");
        }                

        $obj = MatCategoria::find($id);
        
        $caminhos = [
            ['url' => '/admin', 'titulo' => 'Admin'],
            ['url' => route('mat-categoria.index'), 'titulo' => 'Material Educativo Categoria'],
            ['url' => '', 'titulo' => 'Editar'],
        ];
        return view('admin.mat-categorias.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('matCategoria-edit')){
            abort(403, "Não autorizado");
        }                

        $request->validate([
            'title' => 'required|max:255',
            'language_id' => 'required',
        ]);

        $obj = MatCategoria::find($id);
       
        $obj->title = $request['title'];
        $obj->language_id = $request['language_id'];
        
        $obj->save();

        $logs = new Log();
        $logs->updateLog('Mat. Categoria', $id);         

        return redirect()->route('mat-categoria.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('matCategoria-delete')){
            abort(403, "Não autorizado");
        }                
        
        MatCategoria::find($id)->delete();

        $logs = new Log();
        $logs->deleteLog('Mat. Categoria', $id);                 

        return redirect()->route('mat-categoria.index')->with('status', 'Registro Excluído!');
    }
}
