@extends('template.layout')

@section('content')
    <h1 class="page-title">
        <a class=" icon-action-undo" href="{{url('prestamos/validarbusquedaprestamo')}}" style="padding: 5px;"></a>
        Registrar prestamos de libros reservados
    </h1>

    <div class="portlet light portlet-fit portlet-datatable bordered">
        @if(count($usuario) > 0)

            <div class="portlet-title">
                <div class="caption">
                    <span class="caption-subject font-green sbold uppercase"> Registro de prestamos </span>
                </div>
                <div class="col-lg-12 col-xs-12">
                    <span class="font-blue-madison" style="padding: 10px;"> DNI: {{$usuario[0]->dni}} </span>
                    <span class="font-blue-madison"
                          style="padding: 10px;"> Nombre Alumno: {{$usuario[0]->apellido_paterno}} {{$usuario[0]->apellido_materno}}
                        , {{$usuario[0]->nombres}} </span>
                    <span class="font-blue-madison" style="padding: 10px;"> Rol: {{$usuario[0]->name}} </span>
                </div>

            </div>


            <div class="portlet-body">
                <div class="Forum-ForumTopicList row" style="margin:20px">


                    {!! Form::open(array('id' => 'form-validation','route' => array('prestamos.registro'))) !!}

                    <div class="form-group col-lg-6">
                        {!! Form::label('dni', 'DNI:', ['class' => 'control-label']) !!}
                        {!! Form::text('dni', $usuario[0]->dni, ['class' => 'form-control typeahead','required'=>'required']) !!}
                    </div>

                    <div class="form-group col-lg-6">
                        {!! Form::label('cod_libro', 'Código libro:', ['class' => 'control-label']) !!}
                        {!! Form::text('cod_libro', null, ['class' => 'form-control typeahead','required'=>'required']) !!}
                    </div>

                    <div class="col-lg-12">
                        {!! Form::submit('Generar Prestamo', ['class' => 'btn btn-primary']) !!}
                    </div>
                    {!! Form::close() !!}

                </div>
            </div>
        @endif

        <div class="portlet-title">
            <div class="caption">
                <span class="caption-subject font-green sbold uppercase"> Libros reservados <small>para registrar como prestado</small> </span>
            </div>
        </div>
        <div class="portlet-body">
            <div class="Forum-ForumTopicList row">

                <table class="table table-hover nowrap dataTable dtr-inline collapsed tbEncuestas"
                       id="tbEstadosPrestamos"
                       width="100%" role="grid" aria-describedby="tbEstadosPrestamos_info">
                    <thead class="thead-default">
                    <tr>
                        <th>N°</th>
                        <th>Nombre de la bibliograf&iacute;a</th>
                        <th>Autor</th>
                        <th>A&ncaron;o</th>
                        <th>Edici&oacute;n</th>
                        <th>Opci&oacute;n</th>
                    </tr>
                    </thead>
                    <tbody>
                    @php ($count = 1)
                    @foreach($reservas as $item)
                        <tr>
                            <td> {{ $count }} </td>
                            <td> {{ $item->nombre }} </td>
                            <td> {{ $item->nombres }}</td>
                            <td> {{ $item->anio }} </td>
                            <td> {{ $item->edicion }} </td>
                            <td><a href="{{ url('prestamos/registrarprestamo',['id' => $item->id ]) }}"
                                   onclick="$(this).attr('disabled',true)" class="btn btn-sm green"><i
                                            class="fa fa-edit"></i> Prestar </a>
                            </td>

                        </tr>
                        @php ($count++)
                    @endforeach
                    </tbody>
                </table>
            </div>
        </div>

    </div>

@endsection

@section('scripts')
    <script>
        $('#tbEstadosPrestamos').DataTable({
            "lengthMenu": [[25, 20, 50, -1], [25, 20, 50, "Todas"]],
            bFilter: true,
            "bLengthChange": false,
            bInfo: true,
            "responsive": true,
            "autoWidth": true,
            "language": {
                "lengthMenu": "Mostrar _MENU_ Entradas",
                "zeroRecords": "No se encontraron registros",
                "info": "Mostrando la página _PAGE_ de _PAGES_",
                "infoEmpty": "No hay registros disponibles",
                "infoFiltered": "(filtrado de _MAX_ registros totales)",
                "search": "Buscar: ",
                "paginate": {
                    "first": "Primero",
                    "last": "Último",
                    "next": "Siguiente",
                    "previous": "Anterior"
                },
            },
            "order": [],
            "aoColumns": [
                {"bSearchable": false},
                {"bSearchable": true},
                {"bSearchable": true},
                {"bSearchable": false},
                {"bSearchable": false},
                {"bSearchable": false}

            ],
            "columns": [
                {"orderable": false},
                {"orderable": true},
                {"orderable": true},
                {"orderable": false},
                {"orderable": false},
                {"orderable": false}
            ]
        });

        var posts = new Bloodhound({
            datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            remote: {
                url: "{{url('getdniusuario')}}" + "/%QUERY",
                wildcard: '%QUERY',
                transform: function (response) {
                    return $.map(response.data, function (user) {
                        return {value: user.dni};
                    });
                }
            }
        });

        $('.typeahead').typeahead({
                hint: false,
                highlight: true,
                minLength: 2,
                limit: 10
            },
            {
                name: 'Posts',
                display: 'value',
                source: posts
            });

        var postsCod = new Bloodhound({
            datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            remote: {
                url: "{{url('getcodcibliografia')}}" + "/%QUERY",
                wildcard: '%QUERY',
                transform: function (response) {
                    return $.map(response.data, function (libro) {
                        return {value: libro.codigo_CS};
                    });
                }
            }
        });

        $('#cod_libro').typeahead({
                hint: false,
                highlight: true,
                minLength: 2,
                limit: 10
            },
            {
                name: 'postsCod',
                display: 'value',
                source: postsCod
            });

    </script>
@endsection