<div wire:init="loadSolicitors">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="h3 mb-1">Solicitors</h1>
            <p class="text-muted mb-0">Manage your solicitor records</p>
        </div>

        <div>
            @can('solicitors.create')
                <a wire:navigate href="{{ route('solicitors.create') }}" class="btn btn-primary">
                    Add Solicitor
                </a>
            @endcan
        </div>
    </div>

    @if (session()->has('success'))
        <div class="alert alert-success">
            {{ session('success') }}
        </div>
    @endif

    <div class="card shadow-sm">
        <div class="card-body">
            <div class="row g-3 mb-3">
                <div class="col-md-8">
                    <label class="form-label">Search</label>
                    <input
                            type="text"
                            class="form-control"
                            placeholder="Search by name, domain, email or phone..."
                            wire:model.live.debounce.300ms="search"
                    >
                </div>

                <div class="col-md-4">
                    <label class="form-label">Per Page</label>
                    <select class="form-select" wire:model.live="perPage">
                        <option value="10">10</option>
                        <option value="25">25</option>
                        <option value="50">50</option>
                        <option value="100">100</option>
                    </select>
                </div>
            </div>

            @if (! $readyToLoad)
                <div class="table-responsive">
                    <table class="table align-middle">
                        <thead>
                        <tr>
                            <th>Name</th>
                            <th>Domain</th>
                            <th>Email Address</th>
                            <th>Phone Number</th>
                            <th class="text-end">Actions</th>
                        </tr>
                        </thead>
                        <tbody>
                        @for ($i = 0; $i < 5; $i++)
                            <tr>
                                <td><span class="placeholder col-8"></span></td>
                                <td><span class="placeholder col-6"></span></td>
                                <td><span class="placeholder col-8"></span></td>
                                <td><span class="placeholder col-6"></span></td>
                                <td class="text-end"><span class="placeholder col-8"></span></td>
                            </tr>
                        @endfor
                        </tbody>
                    </table>
                </div>
            @elseif ($solicitors->count())
                <div class="table-responsive">
                    <table class="table table-hover align-middle">
                        <thead>
                        <tr>
                            <th>Name</th>
                            <th>Domain</th>
                            <th>Email Address</th>
                            <th>Phone Number</th>
                            <th class="text-end">Actions</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach ($solicitors as $solicitor)
                            <tr wire:key="solicitor-{{ $solicitor->id }}">
                                <td>{{ $solicitor->name }}</td>
                                <td>{{ $solicitor->domain ?: '—' }}</td>
                                <td>{{ $solicitor->defaultEmailAddress() ?: '—' }}</td>
                                <td>{{ $solicitor->primaryPhoneNumber() ?: '—' }}</td>
                                <td class="text-end">
                                    <div class="btn-group">
                                        @can('solicitors.view')
                                            <a wire:navigate
                                                    href="{{ route('solicitors.show', $solicitor) }}"
                                                    class="btn btn-sm"
                                            >
                                                <i class="mdi mdi-eye text-primary"></i>
                                            </a>
                                        @endcan

                                        @can('edit', $solicitor)
                                            <a wire:navigate
                                                    href="{{ route('solicitors.edit', $solicitor) }}"
                                                    class="btn btn-sm"
                                            >
                                                <i class="mdi mdi-pencil"></i>
                                            </a>
                                        @endcan

                                        @can('delete', $solicitor)
                                            <button
                                                    type="button"
                                                    class="btn btn-sm"
                                                    wire:click="delete({{ $solicitor->id }})"
                                                    onclick="confirm('Are you sure you want to delete this solicitor?') || event.stopImmediatePropagation()"
                                            >
                                                <i class="mdi mdi-trash-can text-danger"></i>
                                            </button>
                                        @endcan
                                    </div>
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>

                <div>
                    {{ $solicitors->links() }}
                </div>
            @else
                <div class="text-center py-4">
                    <p class="text-muted mb-3">No solicitors found.</p>

                    @can('solicitors.create')
                        <a wire:navigate href="{{ route('solicitors.create') }}" class="btn btn-primary">
                            Create First Solicitor
                        </a>
                    @endcan
                </div>
            @endif
        </div>
    </div>
</div>