@extends('layouts.app')

@section('content')
    <div class="{{ $fluid ? 'container-fluid' : 'container' }}">
        <div class="row">
            <div class="{{ $containerSize }}">
                @include('layouts.alerts')
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <strong>@lang($heading) {{ $customHeading?$plural:'' }}</strong>
                        @if(count($results) > 0 && method_exists($results, 'total'))
                            <div class="hidden-xs pull-right">
                                {{ trans_choice('common.total_found', $results->total(), ['total' => $results->total() ]) }}
                            </div>
                        @endif
                    </div>
                    <div class="panel-body">
                        @if(count($results) > 0)
                            <div class="table-responsive">
                                @if(Auth::getUser()->canCreate($router))
                                    <div class="pull-right" style="margin-bottom: 15px;">
                                        <a title="@lang("do.Create a New " . $singular)"
                                           href="{{ route($router . '.create', ['classId' => $classId]) }}">
                                            <button class="btn btn-success">
                                                <i class="glyphicon glyphicon-plus"></i>
                                                @lang("do.Create a New $singular")
                                            </button>
                                        </a>
                                    </div>
                                @endif
                                <table class="table table-striped table-bordered">
                                    <thead>
                                    <tr>
                                        @foreach($columns as $column)
                                            <th class="{{ $orderingClass($column) }} clickable text-center
                                                @if(ends_with($column,'id')) col-md-1 text-center
                                                {{-- Hide created_at & updated_at on extra small screens .. --}}
                                            @elseif($column === 'created_at' || $column === 'updated_at') hidden-xs"
                                                style="width: 147px; @endif"
                                                onclick="{{ $nextOrderingJs($column) }}"
                                                title="@lang('Click to change sorting using this column')">
                                                @lang($column)
                                            </th>
                                        @endforeach
                                        @if($canEdit)
                                            <th style="width: 57px" class="text-center">
                                                @lang('do.Edit')
                                            </th>
                                        @endif
                                        @if($canDelete)
                                            @php
                                                $csrfToken = csrf_token();
                                            @endphp
                                            <th style="width: 57px" class="text-center">
                                                @lang('do.Delete')
                                            </th>
                                        @endif
                                    </tr>
                                    </thead>
                                    <tfoot>
                                    <tr>
                                        <td colspan="{{ count($columns) + ($canEdit ? 1 : 0 ) + ($canDelete ? 1 : 0 ) }}">
                                            <div class="col-xs-2">
                                            </div>
                                            <div class="col-xs-8 text-center">
                                                {{ $results->appends(Request::all())->links() }}
                                                @else
                                                    @lang($nothingFound)!
                                                @endif
                                            </div>
                                            <div class="col-xs-2">
                                            </div>
                                        </td>
                                    </tr>
                                    @if(count($results) > 0 && array_search($router, ['entity', 'users']) === false)
                                        <tr>
                                            @foreach($columns as $column)
                                                <td>
                                                    @if(array_search($column, ['id', 'created_at', 'updated_at', 'children_ids']) === false)
                                                        <input name="{{$column}}" type="text"
                                                               value="{{ substr(Input::get($column), 1, -1) }}"
                                                               placeholder="@lang($column)"
                                                               title="@lang($column)"/>
                                                    @elseif($column === 'id')
                                                        <a href="#" id="searchAnchor">
                                                            <button class="btn btn-primary pull-right">
                                                                @lang('do.Filter')
                                                            </button>
                                                        </a>
                                                    @endif
                                                </td>
                                            @endforeach
                                        </tr>
                                    @endif
                                    </tfoot>
                                    <tbody>
                                    @foreach($results as $result)
                                        @php($hasChildren = false)
                                        <tr>
                                            @foreach($columns as $column)
                                                {{-- Hide created_at & updated_at on extra small screens .. --}}
                                                <td @if(array_search($column,['updated_at', 'created_at']) !== false)class="hidden-xs" @endif>
                                                    @if($column === 'id')
                                                        <a href="{{  route($router . '.show', [
                                                            'id' => $result->id,
                                                            'classId' => $classId
                                                        ]) }}">
                                                            <span class="btn btn-link center-block">
                                                                {{ $result->$column ?: '-' }}
                                                            </span>
                                                        </a>
                                                    @elseif($column === 'location' && $result->location_id !== null)
                                                        <a href="{{  route('locations.show', [
                                                            'id' => $result->location_id
                                                        ]) }}">
                                                            <span class="btn btn-link center-block glyphicon glyphicon-rem">
                                                                {{ $result->$column  }}
                                                            </span>
                                                        </a>
                                                    @elseif($column === 'manufacturer' && $result->manufacturer_id !== null)
                                                        <a href="{{  route('manufacturers.show', [
                                                            'id' => $result->manufacturer_id
                                                        ]) }}">
                                                            <span class="btn btn-link center-block">
                                                                {{ $result->$column  }}
                                                            </span>
                                                        </a>
                                                    @elseif($column === 'supplier' && $result->supplier_id !== null)
                                                        <a href="{{  route('suppliers.show', [
                                                            'id' => $result->supplier_id
                                                        ]) }}">
                                                            <span class="btn btn-link center-block">
                                                                {{ $result->$column  }}
                                                            </span>
                                                        </a>
                                                    @elseif($column === 'parent_id' && $result->parent_id !== null)
                                                        <a href="{{  route($router . '.show', [
                                                            'id' => $result->parent_id,
                                                            'classId' => $result->parent_class_id
                                                        ]) }}">
                                                            <span class="btn btn-link center-block">
                                                                {{ $result->parent_id  }} - {{ $result->parent_class }}
                                                            </span>
                                                        </a>
                                                    @elseif($column === 'children_ids' && is_array($result->children_ids))
                                                        @foreach($result->children_ids as $children_id)
                                                            @php($hasChildren = true)
                                                            <a href="{{  route("entities.index", [
                                                                'classId' => $children_id['class_id'],
                                                                ($router === 'entity'? 'type_id' : 'parent_id') => "*$result->id*"
                                                            ]) }}">
                                                            <span class="btn btn-link center-block">
                                                                {{ $children_id['class_name'] }}
                                                                ({{ $children_id['entities_count'] }})
                                                            </span>
                                                            </a>
                                                        @endforeach
                                                    @else
                                                        {{ $result->$column ?: '-' }}
                                                    @endif
                                                </td>
                                            @endforeach
                                            @if($canEdit)
                                                <td>
                                                    <a class="btn btn-labeled btn-default center-block"
                                                       href="{{ route($router . '.edit', [
                                                           'id' => $result->id,
                                                            'classId' => $classId
                                                      ]) }}">
                                                       <span class="btn-label">
                                                           <i class="glyphicon glyphicon-edit"></i>
                                                       </span>
                                                    </a>
                                                </td>
                                            @endif
                                            @if($canDelete)
                                                <td>
                                                    <form action="{{ route($router . '.destroy', [
                                                        'id' => $result->id,
                                                        'classId' => $classId
                                                    ]) }}"
                                                          method="post">
                                                        {{ method_field('DELETE') }}
                                                        {{ csrf_field() }}
                                                        <button type="button" data-toggle="modal"
                                                                data-target="#confirmDelete"
                                                                class="btn btn-labeled btn-default center-block"
                                                                @if($hasChildren)
                                                                disabled="disabled"
                                                                title="@lang('Cannot delete because of children')"
                                                                @endif
                                                        >
                                                            <span class="btn-label">
                                                                <i class="glyphicon glyphicon-trash text-danger"></i>
                                                            </span>
                                                        </button>
                                                    </form>
                                                </td>
                                            @endif
                                        </tr>
                                    @endforeach
                                    </tbody>
                                </table>
                            </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
