<div>
    <x-title>
        CPF Cases

        @can('pension-cases.create')
            <x-slot:buttons>
                <a href="{{ route('pension-cases.create') }}" class="btn btn-success waves-effect waves-light">
                    <i class="mdi mdi-plus me-2"></i> New CPF Case
                </a>
            </x-slot:buttons>
        @endcan
    </x-title>

    <div class="row">

        {{-- MAIN TABLE --}}
        <div class="col-12 col-md-9">
            <div class="card shadow-sm">
                <div class="card-body">

                    {{-- CONTROLS --}}
                    <div class="row align-items-end mb-3 g-2">
                        <div class="col-12 col-md-5">
                            <label class="form-label">Search</label>
                            <input class="form-control"
                                   wire:model.live.debounce.300ms="search"
                                   placeholder="Case #, NI, client name, reference…">
                        </div>
                        <div class="col-6 col-md-2">
                            <label class="form-label">Open / Closed</label>
                            <select class="form-select" wire:model.live="openClosed">
                                <option value="all">All</option>
                                <option value="open">Open</option>
                                <option value="closed">Closed</option>
                            </select>
                        </div>
                        <div class="col-6 col-md-2">
                            <label class="form-label">Vulnerable</label>
                            <select class="form-select" wire:model.live="vulnerable">
                                <option value="">Any</option>
                                <option value="1">Vulnerable</option>
                            </select>
                        </div>
                        <div class="col-6 col-md-2">
                            <label class="form-label">Per Page</label>
                            <select class="form-select" wire:model.live="perPage">
                                <option value="25">25</option>
                                <option value="50">50</option>
                                <option value="100">100</option>
                            </select>
                        </div>
                    </div>

                    {{-- TABLE --}}
                    <div class="table-responsive">
                        <table class="table table-centered table-striped table-nowrap mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th>Case #</th>
                                    <th>Client</th>
                                    <th>Type</th>
                                    <th>Owner</th>
                                    <th>Status</th>
                                    <th>Open/Closed</th>
                                    <th class="text-end"></th>
                                </tr>
                            </thead>
                            <tbody>
                            @forelse($cases as $case)
                                <tr>
                                    <td>
                                        <strong>{{ $case->case_number ?? $case->displayId }}</strong>
                                        @if($case->sf_case_id)
                                            <div class="text-muted small">{{ $case->sf_case_id }}</div>
                                        @endif
                                        @if($case->vulnerable_client_flag)
                                            <span class="badge bg-danger ms-1" title="Vulnerable client">V</span>
                                        @endif
                                    </td>
                                    <td>
                                        @if($case->client)
                                            <a href="{{ route('clients.show', $case->client) }}" class="text-dark">
                                                {{ $case->client->name_first }} {{ $case->client->name_last }}
                                            </a>
                                        @else
                                            <span class="text-muted">—</span>
                                        @endif
                                    </td>
                                    <td>
                                        @if($case->claim_type)
                                            @php
                                                $typeColour = match($case->claim_type) {
                                                    'bank'  => 'bg-primary',
                                                    'fos'   => 'bg-warning text-dark',
                                                    'fscs'  => 'bg-info text-dark',
                                                    default => 'bg-secondary',
                                                };
                                            @endphp
                                            <span class="badge {{ $typeColour }}">{{ strtoupper($case->claim_type) }}</span>
                                        @else
                                            <span class="text-muted">—</span>
                                        @endif
                                    </td>
                                    <td>{{ $case->case_owner ?? '—' }}</td>
                                    <td>
                                        @if($case->status)
                                            <span class="badge"
                                                  style="background-color: {{ $case->status->colour ?? '#6c757d' }}">
                                                {{ $case->status->display_name }}
                                            </span>
                                        @else
                                            <span class="text-muted">—</span>
                                        @endif
                                    </td>
                                    <td>
                                        @if($case->is_open)
                                            <span class="badge bg-success">Open</span>
                                        @else
                                            <span class="badge bg-secondary">Closed</span>
                                        @endif
                                    </td>
                                    <td class="text-end">
                                        <a href="{{ route('pension-cases.view', $case) }}"
                                           class="text-secondary me-2" title="View">
                                            <i class="mdi mdi-eye"></i>
                                        </a>
                                        @can('edit', $case)
                                            <a href="{{ route('pension-cases.edit', $case) }}"
                                               class="text-primary" title="Edit">
                                                <i class="mdi mdi-pencil"></i>
                                            </a>
                                        @endcan
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="7" class="text-center text-muted py-4">
                                        No pension cases found.
                                    </td>
                                </tr>
                            @endforelse
                            </tbody>
                        </table>
                    </div>

                    <div class="mt-3">
                        {{ $cases->links() }}
                    </div>

                </div>
            </div>
        </div>

        {{-- FILTERS SIDEBAR --}}
        <div class="col-12 col-md-3">
            <div class="card shadow-sm">
                <div class="card-body">
                    <h5 class="card-title mb-3">Filters</h5>

                    <div class="mb-3">
                        <label class="form-label">Claim Type</label>
                        <select class="form-select form-select-sm" wire:model.live="claimType">
                            <option value="">All Types</option>
                            <option value="bank">Bank</option>
                            <option value="fos">FOS</option>
                            <option value="fscs">FSCS</option>
                        </select>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Status</label>
                        @if($claimType && $statuses->isNotEmpty())
                            <select class="form-select form-select-sm" wire:model.live="statusId">
                                <option value="">Any Status</option>
                                @foreach($statuses as $s)
                                    <option value="{{ $s->id }}">{{ $s->display_name }}</option>
                                @endforeach
                            </select>
                        @else
                            <select class="form-select form-select-sm" disabled>
                                <option>Select a type first</option>
                            </select>
                        @endif
                    </div>

                    <button class="btn btn-sm btn-outline-secondary w-100 mt-1"
                            wire:click="$set('search', ''); $set('openClosed', 'all'); $set('claimType', ''); $set('statusId', null); $set('vulnerable', '')">
                        Clear Filters
                    </button>
                </div>
            </div>
        </div>

    </div>
</div>
