<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use App\ClienteHome;
use App\Cliente;
use App\Log;

class ClienteHomeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if (Gate::denies('clienteHome-view')){
            abort(403, "Não autorizado");
        } 
        
        if (isset($request['search'])) {
            $objs = ClienteHome::with('cliente')->where('language_id', '=', $request['search'] )->paginate(30);    
        } else {
            $objs = ClienteHome::with('cliente')->paginate(30);
        }
        //dd($objs);
        $caminhos = [
            ['url' => '/admin', 'titulo' => 'Admin'],
            ['url' => '', 'titulo' => 'Cliente'],
        ];
        return view('admin.clienteHome.index', compact('objs', 'caminhos'));
    }

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

        $clientes = Cliente::orderBy('name', 'ASC')->get();
        $caminhos = [
            ['url' => '/admin', 'titulo' => 'Admin'],
            ['url' => route('clienteHome.index'), 'titulo' => 'Cliente Home'],
            ['url' => '', 'titulo' => 'Adicionar'],
        ];
        return view('admin.clienteHome.adicionar', compact('caminhos', 'clientes'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if (Gate::denies('clienteHome-create')){
            abort(403, "Não autorizado");
        }

        $request->validate([
            'cliente_id' => 'required',
        ]);

        $obj = new \App\ClienteHome();
        
        $obj->cliente_id = $request['cliente_id'];
        $obj->titulo = $request['titulo'];
        $obj->description = $request['description'];
        $obj->btncta = $request['btncta'];
        $obj->btnlinkcta = $request['btnlinkcta'];
        $obj->language_id = $request['language_id'];
        
        $obj->save();

        
        if ($request['cliente_id']) { 
            $cli = Cliente::find($request['cliente_id']);    
            $obj->cliente()->associate($cli)->save();
        }

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

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\ClienteHome  $clienteHome
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        if (Gate::denies('clienteHome-edit')){
            abort(403, "Não autorizado");
        }
        
        $clientes = Cliente::orderBy('name', 'ASC')->get();
        $obj = ClienteHome::with('cliente')->where('id', '=', $id)->first();
        

        $caminhos = [
            ['url' => '/admin', 'titulo' => 'Admin'],
            ['url' => route('clienteHome.index'), 'titulo' => 'Cliente Home'],
            ['url' => '', 'titulo' => 'Editar'],
        ];

        return view('admin.clienteHome.editar', compact('obj','caminhos', 'clientes'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\ClienteHome  $clienteHome
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if (Gate::denies('clienteHome-edit')){
            abort(403, "Não autorizado");
        }

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

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

        $obj->cliente_id = $request['cliente_id'];
        $obj->titulo = $request['titulo'];
        $obj->description = $request['description'];
        $obj->btncta = $request['btncta'];
        $obj->btnlinkcta = $request['btnlinkcta'];
        $obj->language_id = $request['language_id'];
        $obj->save();

        if ($request['cliente_id']) { 
            $cli = Cliente::find($request['cliente_id']);    
            $obj->cliente()->associate($cli)->save();
        }                

        $logs = new Log();
        $logs->updateLog('Cliente Home', $id);

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

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

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

        $logs = new Log();
        $logs->deleteLog('Cliente Home', $id);

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