<div class="client-list">
    <x-title>
        Institutions

        <x-slot:buttons>
            <a href="{{ route('institutions.create') }}" class="btn btn-success waves-effect waves-light">
                <i class="mdi mdi-plus me-2"></i> Add Institution
            </a>
        </x-slot:buttons>
    </x-title>

    <div class="row">
        <div class="col-9">
            <div class="card">
                <div class="card-body">

                    <div class="row">
                        <div class="col-4">
                            <div class="mb-3">
                                <label class="form-label">Per Page</label>
                                <select class="form-select" wire:model.live="perPage">
                                    <option value="5">5</option>
                                    <option value="10">10</option>
                                    <option value="25">25</option>
                                    <option value="50">50</option>
                                    <option value="100">100</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-4"></div>
                        <div class="col-4">
                            <div class="mb-3">
                                <label class="form-label">Search</label>
                                <input type="text" class="form-control" wire:model.live="search"
                                       placeholder="search...">
                            </div>
                        </div>
                    </div>


                    <div class="table-responsive">
                        <table class="table table-centered table-striped table-nowrap mb-0">
                            <thead>
                            <tr>
                                <td>ID</td>
                                <td>Name</td>
                                <td>Institution Type</td>
                                <td>DSAR Type</td>
                                <td>Tags</td>
                                <td>Created</td>
                                <td></td>
                            </tr>
                            </thead>
                            <tbody>
                            @if(count($institutions))
                                @foreach($institutions as $institution)
                                    <tr>
                                        <td><b>{{ $institution->displayId }}</b></td>
                                        <td>
                                            @if($institution->is_prioritised)
                                                <span class="mdi mdi-earth text-warning me-1" title="Prioritised"></span>
                                            @endif
                                            <b>{{ $institution->display_name }}</b>
                                        </td>
                                        <td>{{ $institution->type?->display_name }}</td>
                                        <td>{{ $institution->dsarType?->display_name }}<br>
                                            <span class="badge bg-info text-dark">{{ ucwords($institution->send_type ?? 'N/A') }}</span>
                                        </td>
                                        <td>
                                            <x-tag-list :model="$institution"/>
                                        </td>
                                        <td>{{ $institution->created_at->format("d/m/Y H:i") }}</td>
                                        <td class="text-end">
                                            @can('view', $institution)
                                                <a href="{{ route('institutions.show', $institution) }}"
                                                   class="btn btn-md">
                                                    <span class="mdi mdi-shield-search text-primary"></span>
                                                </a>
                                            @endcan
                                            @can('edit', $institution)
                                                <a href="{{ route('institutions.edit', $institution) }}"
                                                   class="btn btn-md">
                                                    <span class="mdi mdi-pencil text-primary"></span>
                                                </a>
                                            @endcan
                                            @if($institution->care_settings_count === 0 && $institution->claims_count === 0)
                                                @can('delete', $institution)
                                                    <a wire:click="deleteInstitution({{ $institution->id }})"
                                                       wire:confirm="Are you sure you want to delete this institution?"
                                                       class="btn btn-md">
                                                        <span class="mdi mdi-trash-can text-danger"></span>
                                                    </a>
                                                @endcan
                                            @endif
                                        </td>
                                    </tr>
                                @endforeach
                            @else
                                <tr>
                                    <td colspan="5">No lead institutions found.</td>
                                </tr>
                            @endif
                            </tbody>
                        </table>
                    </div>


                    {{ $institutions->links() }}
                </div>
            </div>
        </div>
        <div class="col-12 col-md-3">
            <button class="btn btn-dark w-100 d-md-none mb-3" type="button" data-bs-toggle="collapse"
                    data-bs-target="#filterSidebar">
                <i class="mdi mdi-filter-variant me-1"></i> Advanced Filters
            </button>

            <div class="collapse d-md-block" id="filterSidebar">
                <div class="card">
                    <div class="card-body">
                        <h5 class="card-title mb-3">Filters</h5>
                        <div class="row g-2">
                            <div class="col-6">
                                <div class="mb-3">
                                    <x-ui.select :options="$types" name="type_id" label="Type"/>
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="mb-3">
                                    <x-ui.select :options="$tags" name="tag_id" label="Tags"/>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="card-footer bg-transparent border-top-0 d-grid">
                        <button class="btn btn-sm btn-outline-danger rounded-pill" wire:click="clearFilters">
                            Clear All Filters
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>