<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Timeline;
use App\Pagina;
use App\Log;
use App\Rules\SecureImage;

class TimelineController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $objs = Timeline::orderBy('id', 'DESC')->paginate(30);
        $caminhos = [
            ['url' => '/admin', 'titulo' => 'Admin'],
            ['url' => '', 'titulo' => 'Timeline'],
        ];
        return view('admin.timeline.index', compact('objs', 'caminhos'));
    }

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

        $paginas = Pagina::all();
        
        $caminhos = [
            ['url' => '/admin', 'titulo' => 'Admin'],
            ['url' => route('timeline.index'), 'titulo' => 'Timeline'],
            ['url' => '', 'titulo' => 'Adicionar'],
        ];
        return view('admin.timeline.adicionar', compact('caminhos', 'paginas'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'titulo' => 'required|max:255',
            'image' => ['file',
                        new SecureImage,
                ],
        ]);

        $obj = new \App\Timeline();
        $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').'timeline', $nameFile);
            if ( !$upload )
                return redirect()
                            ->back()
                            ->with('error', 'Falha ao fazer upload')
                            ->withInput();
        }  

        $obj->titulo = $request['titulo'];
        $obj->ano = $request['ano'];
        $obj->language_id = $request['language_id'];
        $obj->description = $request['description'];
        $obj->image = $nameFile;

        $obj->save();

        $logs = new Log();
        $logs->insertLog('Timeline', $obj->id);  

        return redirect()->route('timeline.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)
    {

        $obj = Timeline::find($id);
        
        $paginas = Pagina::all();
       
        $caminhos = [
            ['url' => '/admin', 'titulo' => 'Admin'],
            ['url' => route('timeline.index'), 'titulo' => 'Timeline'],
            ['url' => '', 'titulo' => 'Editar'],
        ];
        return view('admin.timeline.editar', compact('obj','caminhos', 'paginas'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request->validate([
            'titulo' => 'required|max:255',
            'image' => ['file',
                        new SecureImage,
                ],
        ]);

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

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

        $obj->id = $id;
        $obj->titulo = $request['titulo'];
        $obj->ano = $request['ano'];
        $obj->language_id = $request['language_id'];
        $obj->description = $request['description'];
        $obj->image = $nameFile;
        
        $obj->save();

        
        $logs = new Log();
        $logs->updateLog('Timeline', $id);           
        
        return redirect()->route('timeline.edit', $id )->with('status', 'Registro atualizado!');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Timeline::find($id)->delete();
        
        $logs = new Log();
        $logs->deleteLog('Timeline', $id);         

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

}
