<div>
    <div class="card shadow-sm mb-4">
        <div class="card-body">
            <h4 class="fw-bold mb-3 d-flex justify-content-between align-items-center">
                Attached Institutions
                <button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#attachInstitutionModal">
                    <i class="mdi mdi-plus"></i>
                </button>
            </h4>

            @if (session()->has('message'))
                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    {{ session('message') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            @endif

            @if($mainInstitution->relatedInstitutions->count())
                <ul class="list-group">
                    @foreach($mainInstitution->relatedInstitutions as $linked)
                        <li class="list-group-item bg-light d-flex justify-content-between align-items-center" wire:key="linked-{{ $linked->id }}">
                            <a href="{{ route('institutions.show', $linked) }}">
                                <span class="fw-bold text-primary">{{ $linked->display_name }}</span>
                                <span class="ms-2 small text-muted">({{ $linked->type->display_name ?? 'N/A' }})</span>
                            </a>
                            <span>
                                <i class="mdi mdi-trash-can text-danger cursor-pointer"
                                   style="font-size: 1.1rem;"
                                   wire:click="detach({{ $linked->id }})"
                                   wire:confirm="Are you sure you want to remove this connection?"></i>
                            </span>
                        </li>
                    @endforeach
                </ul>
            @else
                <div class="text-center p-5 text-muted">
                    <i class="mdi mdi-office-building" style="font-size: 48px;"></i>
                    <p class="mt-3 fw-bold">No institutions have been attached yet.</p>
                </div>
            @endif
        </div>
        <div class="card-footer bg-white text-end">
            <small class="text-muted">Total Connections: {{ $mainInstitution->relatedInstitutions->count() }}</small>
        </div>
    </div>

    <!-- Modal: Search and Attach -->
    <div wire:ignore.self class="modal fade" id="attachInstitutionModal"  tabindex="-1" style="--bs-offcanvas-width: 600px;" aria-labelledby="attachInstitutionModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg modal-dialog-scrollable">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="attachInstitutionModalLabel">Attach New Institutions</h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <!-- Search Bar -->
                    <div class="input-group mb-3">
                        <span class="input-group-text"><i class="bi bi-search"></i></span>
                        <input type="text"
                               class="form-control"
                               placeholder="Search available institutions..."
                               wire:model.live.debounce.300ms="search">
                    </div>

                    <div class="table-responsive">
                        <table class="table table-sm table-hover align-middle">
                            <thead class="table-light sticky-top">
                            <tr>
                                <th style="width: 40px;"></th>
                                <th>Name</th>
                                <th>Type</th>
                            </tr>
                            </thead>
                            <tbody>
                            @forelse($availableInstitutions as $item)
                                <tr wire:key="avail-{{ $item->id }}">
                                    <td>
                                        <div class="form-check">
                                            <input class="form-check-input"
                                                   type="checkbox"
                                                   value="{{ $item->id }}"
                                                   wire:model.live="selectedIds"
                                                   id="check-{{ $item->id }}">
                                        </div>
                                    </td>
                                    <td>
                                        <label class="form-check-label d-block cursor-pointer" for="check-{{ $item->id }}">
                                            {{ $item->display_name }}
                                        </label>
                                    </td>
                                    <td class="small text-muted">{{ $item->type->display_name ?? 'N/A' }}</td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="3" class="text-center py-4 text-muted">
                                        No matches found.
                                    </td>
                                </tr>
                            @endforelse
                            </tbody>
                        </table>
                    </div>

                    <div class="mt-3">
                        {{ $availableInstitutions->links() }}
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                    <button type="button"
                            class="btn btn-success"
                            wire:click="attachSelected"
                            @if(empty($selectedIds)) disabled @endif
                            data-bs-dismiss="modal">
                        Attach Selected ({{ count($selectedIds) }})
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>