<form action="{{Utils::transformURL($grid->data->path())}}" ajax-request="{{$grid->htmlid}}" {{$grid->ajaxGrid ? 'ajax-enabled="true"' : '' }}>
<input type="hidden" name="sf" value="{{request('sf')}}">
<input type="hidden" name="so" value="{{request('so')}}">
<input type="hidden" name="page" value="{{$grid->data->currentPage()}}">
<input type="hidden" name="search" value="{{request('search')}}" />
@foreach ($grid->parameters as $parameter)
<input type="hidden" name="{{$parameter}}" value="{{request($parameter)}}">
@endforeach
<table class="table table-hover table-striped table-bordered custom-table-grid">
    <thead>
        <tr>
        @forelse ($grid->columns as $key => $grid_column)
            <th>
            @if ( $grid_column->sort )
                @php
                    $sort_style = 'column-sort-default';
                    $sort_order = 'asc';
                    if( $grid->sort_field == $grid_column->position ) {
                        $sort_style = $grid->sort_order == 'asc' ? 'column-sort-asc' : 'column-sort-desc';
                        $sort_order = $grid->sort_order == 'desc' ? 'asc' : 'desc';
                    }
                @endphp
                <a href="javascript:void(0)" onclick="submit_grid(this, '{{$grid_column->position}}', '{{$sort_order}}' )" class="{{$sort_style}}" >{!!$grid_column->title!!}</a>
            @else
                {!!$grid_column->title!!}
            @endif
            <br>
            @if ( $grid_column->filter )
                @php
                    $filter_css_class = $grid_column->filter_field_type == 'date' ? 'date-range' : null;
                    $filter_user_value = request('filter')[$grid_column->position] ?? '';

                    if( $grid_column->filter_field_type == 'date' && !strtotime($filter_user_value) ) {
                        $filter_user_value = '';
                    }
                @endphp

                @if ( $grid_column->filter_values )
                    <select class="form-control" name="filter[{{$grid_column->position}}]" onchange="submit_grid(this,null,null,1);">
                    <option value="">Seleccionar</option>
                    @foreach ($grid_column->filter_values as $key => $value)
                        @php
                            $option_selected = ($key ==  $filter_user_value) ? 'selected' : '';
                        @endphp 
                        <option value="{{$key}}" {{$option_selected}} >{{$value}}</option>
                    @endforeach
                    </select>
                @else
                    <input type="text" name="filter[{{$grid_column->position}}]" value="{{$filter_user_value}}" class="form-control {{$filter_css_class}}" onkeypress="if(event.which==13)submit_grid(this,null,null,1);">
                @endif
                
            @endif
            </th>
        @empty
            <th>No columns</th>
        @endforelse
        </tr>
    </thead>
    <tbody>
        @forelse ($grid->data as $grid_record)
        @php 
            $row_style = null;
            if( !$grid->row_style ) {
                $row_style = '';
            } else if ( is_string($grid->row_style) ) {
                $row_style = $grid->row_style;
            } else {
                $row_style = ($grid->row_style)($grid, $grid_record);
            }
        @endphp
        <tr {!! $row_style !!} >
            @foreach ($grid->columns as $grid_column)
            <td {!! $grid_column->style !!}>
                @if( $grid_column->html_decorator )
                    @if($grid_column->encode)
                        {{($grid_column->html_decorator)($grid_record)}}
                    @else
                        {!!($grid_column->html_decorator)($grid_record)!!}
                    @endif
                @else
                    @php 
                        $attribute_name = explode('.', $grid_column->name);
                        $attribute_name = count($attribute_name) > 1 ? $attribute_name[1] : $attribute_name[0];
                        $column_value = $grid_record[$attribute_name];
                    @endphp
                    @if($grid_column->encode)
                        {{$column_value}}
                    @else
                        {!!$column_value!!}
                    @endif
                @endif
            </td>
            @endforeach
        </tr>
        @empty
        <tr>
            <td colspan="1000" align="center">{{$grid->label_no_records}}</td>
        </tr>
        @endforelse
    </tbody>
    @php $paginator = $grid->data; @endphp
    @if ($paginator->hasPages())
    <tfoot>
        <tr>
            <td colspan="1000">
                <div class="row">
                    <div class="col text-left">Mostrando {{$grid->data->firstItem()}} a {{$grid->data->lastItem()}} de {{ $grid->data->total() }} Registros</div>
                    <div class="col">
                        {{-- copied from: bootstrap-4.blade.php --}}
                        <ul class="pagination justify-content-end">
                            {{-- Previous Page Link --}}
                            @if ($paginator->onFirstPage())
                                <li class="page-item disabled">
                                    <span class="page-link">@lang('pagination.previous')</span>
                                </li>
                            @else
                                <li class="page-item">
                                    <input type="button" class="page-link cold-color" value="@lang('pagination.previous')" onclick="submit_grid(this, null, null, {{$paginator->currentPage()-1}} )" />
                                </li>
                                
                            @endif

                            @if ( $paginator->currentPage() > 2 )
                                <li class="page-item">
                                    <input type="button" class="page-link cold-color" value="1" onclick="submit_grid(this, null, null, 1)" />
                                </li>
                                <li class="page-item disabled"><span class="page-link"> ... </span></li>
                            @endif


                            @if ( $paginator->currentPage() > 1 )
                                <li class="page-item">
                                    <input type="button" class="page-link cold-color" value="{{$paginator->currentPage()-1}}" onclick="submit_grid(this, null, null, {{$paginator->currentPage()-1}})" />
                                </li>
                            @endif
                                
                            <li class="page-item">
                                <input type="button" class="page-link cold-color" style="background-color: #2E7178;color:white" value="{{$paginator->currentPage()}}" />
                            </li>

                            @if ( $paginator->currentPage() < $paginator->lastPage() )
                                <li class="page-item">
                                    <input type="button" class="page-link cold-color" value="{{$paginator->currentPage()+1}}" onclick="submit_grid(this, null, null, {{$paginator->currentPage()+1}})" />
                                </li>
                            @endif

                            
                            @if ( $paginator->currentPage() < $paginator->lastPage() - 1 )
                                <li class="page-item disabled"><span class="page-link"> ... </span></li>
                                <li class="page-item">
                                    <input type="button" class="page-link cold-color" value="{{$paginator->lastPage()}}" onclick="submit_grid(this, null, null, {{$paginator->lastPage()}})" />
                                </li>
                            @endif

                            {{-- Next Page Link --}}
                            @if ($paginator->hasMorePages())
                                <li class="page-item">
                                    <input type="button" class="page-link cold-color" value="@lang('pagination.next')" onclick="submit_grid(this, null, null, {{$paginator->currentPage()+1}} )" />
                                </li>
                            @else
                                <li class="page-item disabled">
                                    <span class="page-link"> @lang('pagination.next') </span>
                                </li>
                            @endif
                        </ul>
                    </div>
                </div>
            </td>
        </tr>
    </tfoot>
    @endif
</table>
</form>
