<div class="client-list">
    <x-title>
        Client List
        <x-slot:buttons>
            @can('clients.create')
                <a wire:navigate href="{{ route('clients.create') }}" class="btn btn-success waves-effect waves-light">
                    <i class="mdi mdi-plus me-2"></i> Add Client
                </a>
            @endcan
        </x-slot:buttons>
    </x-title>

    <div class="row">
        <div class="col-12 col-md-9">
            <div class="card">
                <div class="card-body">
                    <div class="row mb-3 align-items-center">
                        <div class="col-6 col-md-4">
                            <label class="form-label">Per Page</label>
                            <select class="form-select form-select-sm" wire:model.live="perPage">
                                <option value="5">5</option>
                                <option value="10">10</option>
                                <option value="25">25</option>
                                <option value="50">50</option>
                            </select>
                        </div>
                        <div class="col-md-4 d-none d-md-block"></div>
                        <div class="col-6 col-md-4">
                            <label class="form-label">Search</label>
                            <input type="text" class="form-control form-control-sm" wire:model.live="search"
                                   placeholder="Search name/email...">
                        </div>
                    </div>

                    <div class="table-responsive d-none d-md-block">

                        <table class="table table-centered table-hover table-striped mb-0">
                            <thead>
                            <tr>
                                <th>ID</th>
                                <th>Name</th>
                                <th>Phone Number</th>
                                <th>Status</th>
                                <th class="d-none d-lg-table-cell">Tags</th>
                                <th>Created</th>
                                <th class="text-end">Action</th>
                            </tr>
                            </thead>
                            <tbody>
                            @forelse($clients as $client)
                                @php
                                    $isHighPriority =
                                        ($client->date_of_birth?->age >= 68)
                                        || ($client->claim?->terminalIllness !== null);
                                @endphp
                                <tr class="{{ $isHighPriority ? 'table-danger' : '' }}">
                                    <td>{{ $client->displayId }}</td>
                                    <td>
                                        @if($isHighPriority)
                                            <span class="badge bg-danger me-1"
                                                  title="Client is 68+ or has a terminal illness">
                                                <i class="mdi mdi-alert-octagon"></i> High
                                            </span>
                                        @endif
                                        <strong>{{ $client->title }} {{ $client->name_first }} {{ $client->name_last }}</strong>
                                        @can('clients.tranche.view')
                                            <br/>
                                            <span class="text-primary">
                                            @if($client->tranche)
                                                    {{ $client->tranche->funder_name }}
                                                    - {{ $client->tranche->tranche_name }}
                                                @endif
                                            </span>
                                        @endcan
                                    </td>
                                    <td class="text-truncate"
                                        style="max-width: 150px;">{{ $client->phone_number }}</td>
                                    <td>
                                        {{ $client->claim->status->display_name }}
                                    </td>
                                    <td class="d-none d-lg-table-cell">
                                        <x-tag-list :model="$client->claim"/>
                                    </td>
                                    <td class="text-muted small text-nowrap">
                                        {{ $client->created_at->format("d/m/y H:i") }}
                                    </td>
                                    <td class="text-end">
                                        @can('view', $client)
                                            <a wire:navigate href="{{ route('clients.show', $client) }}" class="btn btn-sm">
                                                <i class="mdi mdi-eye"></i>
                                            </a>
                                        @endcan
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="7" class="text-center">No clients found.</td>
                                </tr>
                            @endforelse
                            </tbody>
                        </table>
                    </div>

                    <div class="d-md-none">
                        @forelse($clients as $client)
                            @php
                                $isHighPriority =
                                    ($client->date_of_birth?->age >= 68)
                                    || ($client->claim?->terminalIllness !== null);
                            @endphp
                            <div class="card border {{ $isHighPriority ? 'border-danger border-2 bg-danger bg-opacity-10' : '' }} mb-3 shadow-sm">
                                <div class="card-body p-3">
                                    <div class="d-flex justify-content-between align-items-start mb-2">
                                        <div>
                                            @if($isHighPriority)
                                                <span class="badge bg-danger mb-1">
                                                    <i class="mdi mdi-alert-octagon"></i> High Priority
                                                </span>
                                                <br>
                                            @endif
                                            <h6 class="mb-0 text-primary">{{ $client->name_first }} {{ $client->name_last }}</h6>
                                            <small class="text-muted">{{ $client->phone_number }}</small>
                                        </div>
                                        <span class="badge bg-info small">{{ $client->claim->status->display_name }}</span>
                                    </div>

                                    <div class="row g-0 border-top pt-2 mt-2">
                                        <div class="col-8 small">
                                            <i class="mdi mdi-phone me-1"></i> {{ $client->phone_number }}<br>
                                            <i class="mdi mdi-calendar-clock me-1"></i> {{ $client->created_at->format("d/m/y H:i") }}
                                        </div>
                                        <div class="col-4 text-end">
                                            @can('view', $client)
                                                <a wire:navigate href="{{ route('clients.show', $client) }}"
                                                   class="btn btn-sm btn-primary">
                                                    View Details
                                                </a>
                                            @endcan
                                        </div>
                                    </div>
                                </div>
                            </div>
                        @empty
                            <p class="text-center text-muted">No clients found.</p>
                        @endforelse
                    </div>

                    <div class="mt-3">
                        {{ $clients->links() }}
                    </div>
                </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="mb-3">
                            <x-ui.select :options="$leadSources" name="lead_source_id" label="Lead Source"/>
                        </div>
                        <div class="mb-3">
                            <x-ui.select :options="$urls" name="url" label="URL / Endpoint"/>
                        </div>
                        <div class="mb-3">
                            <x-ui.select :options="$tranches" name="tranche_id" label="Tranche"/>
                        </div>
                        <div class="row g-2">
                            <div class="col-6">
                                <div class="mb-3">
                                    <x-ui.select :options="$statuses" name="status_id" label="Status"/>
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="mb-3">
                                    <x-ui.select :options="$tags" name="tag_id" label="Tags"/>
                                </div>
                            </div>
                        </div>
                        <div class="mb-3">
                            <x-ui.select :options="[1 => 'High Priority', 2 => 'Low Priority']" name="priority"
                                         label="Priority"/>
                        </div>
                        <div class="row g-2">
                            <div class="col-6">
                                <x-ui.input type="date" name="date_from" label="From"/>
                            </div>
                            <div class="col-6">
                                <x-ui.input type="date" name="date_to" label="To"/>
                            </div>
                        </div>

                        <div class="mt-3">
                            <x-ui.select :options="$careSettings" name="care_setting_id" label="Care Setting"/>
                        </div>

                        <div class="mt-3">
                            <x-ui.select :options="$handlers" name="handler" label="Handler"/>
                        </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>
