<div>
    <x-title>
        Tranche Details
        <p class="text-muted small mb-0">
            <span class="font-weight-bold text-primary">{{ $tranche->funder_name }}</span>
            <span class="mx-1">/</span> {{ $tranche->tranche_name }}
        </p>
        <x-slot:buttons>
            <button wire:click="exportToCsv" class="btn btn-success shadow-sm mr-2">
                <i class="fa fa-download mr-1"></i> Export CSV
            </button>
            <a href="{{ route('reports.tranche.index') }}" class="btn btn-primary border shadow-sm">
                Back to Report
            </a>
        </x-slot:buttons>
    </x-title>

    <!-- Table Card -->
    <div class="card shadow-sm border-0 rounded-lg overflow-hidden">
        <div class="card-header bg-white py-3 border-bottom-0 d-flex flex-column flex-md-row justify-content-between align-items-md-center">
            <div class="text-muted small mb-2 mb-md-0">
                Showing <span class="font-weight-bold text-dark">{{ $clients->total() }}</span> cases in this tranche
            </div>
            <input wire:model.live="search" type="text" placeholder="Search Client ID..."
                   class="form-control form-control-sm w-auto" style="min-width: 250px;">
        </div>

        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead class="bg-light text-secondary text-uppercase"
                       style="font-size: 0.7rem; letter-spacing: 0.05em;">
                <tr>
                    <th class="px-4 border-top-0">Client ID</th>
                    <th class="px-4 border-top-0">APP Number</th>
                    <th class="px-4 border-top-0">Priority</th>
                    <th class="px-4 border-top-0">Status</th>
                    <th class="px-4 border-top-0">Funded Amt</th>
                    <th class="px-4 border-top-0">Funded Date</th>
                    <th class="px-4 border-top-0">Redemption</th>
                </tr>
                </thead>
                <tbody class="bg-white">
                @foreach($clients as $client)
                    @php
                        $isHighPriority =
                            ($client->date_of_birth?->age >= 68)
                            || $client->terminalIllness !== null;
                    @endphp
                    <tr>
                        <td class="px-4 align-middle font-weight-bold text-dark">{{ $client->displayId }}</td>
                        <td class="px-4 align-middle">{{ $client->claim?->case_number ?? '-' }}</td>
                        <td class="px-4 align-middle">
                                <span class="badge badge-light border px-2 py-1 text-muted">
                                    {{ $isHighPriority ? 'High' : 'Low' }}
                                </span>
                        </td>
                        <td class="px-4 align-middle">
                                <span class="badge bg-info px-2 py-1">
                                    {{ $client->claim?->status?->display_name ?? 'Unknown' }}
                                </span>
                        </td>
                        <td class="px-4 align-middle font-weight-bold text-dark">
                            £{{ number_format($tranche->amount_per_case, 2) }}
                        </td>
                        <td class="px-4 align-middle text-muted small">{{$tranche->funded_date->format('d/m/Y') }}</td>
                        <td class="px-4 align-middle text-muted small">{{ $tranche->funded_date->addMonths(12)->format('d/m/Y') }}</td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>

        <div class="card-footer bg-light border-0">
            <div class="d-flex justify-content-center">
                {{ $clients->links() }}
            </div>
        </div>
    </div>
</div>