<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

use App\Cta;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use App\Log;
use App\Rules\SecureImage;

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

        $objs = Cta::paginate(30);
        
        $caminhos = [
            ['url' => '/admin', 'titulo' => 'Admin'],
            ['url' => '', 'titulo' => 'CTA'],
        ];
        return view('admin.cta.index', compact('objs', 'caminhos'));
    }

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

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

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

        if ($request['home'] == '1') {
            Cta::where('home', '=', 1)->update(array('home' => 1));
        }

        $obj = new \App\Cta();
        $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').'cta', $nameFile);
            if ( !$upload )
                return redirect()
                            ->back()
                            ->with('error', 'Falha ao fazer upload')
                            ->withInput();
        }  
        
        $obj->name = $request['name'];
        $obj->home = $request['home'];
        $obj->bt_color = $request['bt_color'];
        $obj->titulo_pt = $request['titulo_pt'];
        $obj->titulo_es = $request['titulo_es'];
        $obj->titulo_en = $request['titulo_en'];
        $obj->description_pt = $request['description_pt'];
        $obj->description_es = $request['description_es'];
        $obj->description_en = $request['description_en'];
        $obj->btncta_pt = $request['btncta_pt'];
        $obj->btncta_es = $request['btncta_es'];
        $obj->btncta_en = $request['btncta_en'];
        $obj->btnlinkcta_pt = $request['btnlinkcta_pt'];
        $obj->btnlinkcta_es = $request['btnlinkcta_es'];
        $obj->btnlinkcta_en = $request['btnlinkcta_en'];
        $obj->target = ($request['target'] != 0) ? $request['target'] : 0;
        $obj->image = $nameFile;
        $obj->save();

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

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

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

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

        $obj = Cta::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').'cta', $nameFile);
            if ( !$upload )
                return redirect()
                            ->back()
                            ->with('error', 'Falha ao fazer upload')
                            ->withInput();
        } else {
            $nameFile = $request['old_image'];
        }

        $obj->name = $request['name'];
        $obj->home = $request['home'];
        $obj->bt_color = $request['bt_color'];
        $obj->titulo_pt = $request['titulo_pt'];
        $obj->titulo_es = $request['titulo_es'];
        $obj->titulo_en = $request['titulo_en'];
        $obj->description_pt = $request['description_pt'];
        $obj->description_es = $request['description_es'];
        $obj->description_en = $request['description_en'];
        $obj->btncta_pt = $request['btncta_pt'];
        $obj->btncta_es = $request['btncta_es'];
        $obj->btncta_en = $request['btncta_en'];
        $obj->btnlinkcta_pt = $request['btnlinkcta_pt'];
        $obj->btnlinkcta_es = $request['btnlinkcta_es'];
        $obj->btnlinkcta_en = $request['btnlinkcta_en'];
        $obj->target = ($request['target'] != 0) ? $request['target'] : 0;
        $obj->image = $nameFile;

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

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

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

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

    }
}