@extends('template.layout')
@section('styles')
@endsection

@section('content')
    <h1 class="page-title">
        <a href="{{ url('foro') }}" title="Volver"><i class="icon-action-undo"></i></a>
        {{ $categoria->nombre }}
        <small>{{ $categoria->descripcion }}</small>
    </h1>
    <div class="portlet light portlet-fit bordered">
        <div class="portlet-title" style="margin-bottom: 0">
            <div class="actions" style="padding-bottom: 0">
                {{ Form::open(array('id' => 'form-validation','route' => ['categoria.search',$categoria->id], 'method' => 'get', 'class'=>'form-horizontal', 'style'=>'display: inline-block;')) }}
                <div class="input-icon">
                    <i class="fa fa-search"></i>
                    {!! Form::search('search', null, array('placeholder' => 'Buscar un tema...','class' => 'form-control', 'maxlength' => '50')) !!}
                </div>
                <input type="submit" style="display: none" />
                {!! Form::close() !!}
                <button type="button" class="btn btn-primary Crear">
                    <i class="fa fa-plus"></i><span class="hidden-xs"> Nuevo Tema </span>
                </button>
            </div>
        </div>
        <div class="portlet-body forum-body">
            <div class="">
                @if(!empty($temas) && count($temas) > 0)
                    @foreach($temas as $tema)
                        <div class="ForumTopic">
                            <span class="ForumTopic-type">
                                <a href="javascript:;" id="fav-{{ $tema->id }}" class="button-a" onclick="addFavorito({{ $tema->id }})" tabindex="-1" title="Favorito">
                                    @if($tema->favoritos->contains('id_usuario', Auth::user()->id))
                                        <i id="tema-{{ $tema->id }}" class="fa fa-star"></i>
                                    @else
                                        <i id="tema-{{ $tema->id }}" class="fa fa-star-o"></i>
                                    @endif
                                </a>
                            </span>
                            <a href="{{ url('foro/tema',['id' => $tema->id]) }}" class="ForumTopic-details">
                            <span class="ForumTopic-heading">
                                <span class="ForumTopic-title--wrapper">
                                    <span class="ForumTopic-timestamp on-mobile"><span class="ForumTopic-timestamp--lastPost" href="#">{{ $tema->created_at->diffForHumans() }}</span></span>
                                    <span class="ForumTopic-title">{{ $tema->nombre }}</span>
                                </span>
                            </span>
                                <span class="ForumTopic-author">{{ $tema->usuario->nombres . ' ' . $tema->usuario->apellido_paterno . ' ' . $tema->usuario->apellido_materno }}</span>
                                <span class="ForumTopic-replies"><i class="icon-bubbles" style="margin-right: .25rem;"></i><span>{{ count($tema->comentarios) }}</span></span>
                                <span class="ForumTopic-timestamp">{{ $tema->created_at->diffForHumans() }}</span>
                            </a>
                            @if(\Illuminate\Support\Facades\Auth::user()->isAdmin() || $tema->usuario->id == \Illuminate\Support\Facades\Auth::user()->id)
                                <a href="javascript:;" id="delete-{{ $tema->id }}" class="button-a delete-color ForumTopic-Admin" onclick="borrarTema({{ $tema->id }})" tabindex="-1" title="Eliminar">
                                    <i id="tema-{{ $tema->id }}" class="icon-close"></i>
                                </a>
                            @endif
                        </div>
                    @endforeach
                        <div class="text-right">
                            {{ $temas->appends(\Illuminate\Support\Facades\Input::except('page'))->links() }}
                        </div>
                @else
                    <label class="no-existen">No existen temas actualmente</label>
                @endif
            </div>
        </div>
    </div>
    <div id="Modal"></div>
@endsection

@section('scripts')
<script>
    $('button.Crear').on('click', function () {
        $('button.Crear').prop('disabled', true);
        this.blur();
        $.ajax({
            url:  '{{ url('foro/tema/registrar') }}' + '/' + '{{ $categoria->id }}',
            type: 'GET',
            cache: false,
            success: function (result) {
                $('#Modal').html(result).find('.modal').modal({
                    show: true
                });
            },
            complete: function (result) {
                $('button.Crear').prop('disabled', false);
            }
        });
        return false;
    });
    function addFavorito(id) {
        $('#fav-' + id).blur();
        var icon = $('#tema-' + id );
        if (icon.hasClass('fa fa-star') == true) {
            icon
                .removeClass('fa fa-star')
                .addClass('fa fa-star-o')
        }
        else {
            if (icon.hasClass('fa fa-star-o') == true) {
                icon
                    .removeClass('fa fa-star-o')
                    .addClass('fa fa-star')
            }
        }
        $.ajax({
            url: '{{ url('foro/tema/favorito') }}' + '/' + id,
            type: 'POST',
            success: function (result) {
                console.log(result);
            },
            error: function (result) {
                swal("Oops", "Ocurrió un error al realizar su solicitud", "error");
                var icon = $('#tema-' + id);
                if (icon.hasClass('fa fa-star') == true) {
                    icon
                        .removeClass('fa fa-star')
                        .addClass('fa fa-star-o')
                }
                else {
                    if (icon.hasClass('fa fa-star-o') == true) {
                        icon
                            .removeClass('fa fa-star-o')
                            .addClass('fa fa-star')
                    }
                }
            }
        });
    };
    function borrarTema(id) {
        swal({
                title: "¿Está seguro?",
                text: "Se eliminará el tema seleccionado.",
                type: "warning",
                confirmButtonText: "Si",
                cancelButtonText: " No",
                showCancelButton: true,
                closeOnConfirm: false,
                showLoaderOnConfirm: true
            },
            function () {
                post('{{ url('foro/tema/eliminar') }}' + "/" + id);
            });
    };
</script>
@endsection