<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 wire:navigate href="{{ route('reports.tranche.index') }}" class="btn btn-primary border shadow-sm">
                Back to Report
            </a>
        </x-slot:buttons>
    </x-title>

    @php
        $highPct = $total > 0 ? ($highCount / $total) * 100 : 0;
        $lowPct  = $total > 0 ? ($lowCount  / $total) * 100 : 0;
    @endphp
    <!-- Priority Summary -->
    <div class="row mb-3">
        <div class="col-md-6 mb-2 mb-md-0">
            <div class="card shadow-sm border-0 border-start border-danger border-4 bg-danger bg-opacity-10 p-3">
                <div class="d-flex justify-content-between align-items-center">
                    <span class="text-danger text-uppercase small fw-bold">
                        <i class="mdi mdi-alert-octagon me-1"></i> High Priority
                    </span>
                    <span class="h3 fw-bold text-danger mb-0">
                        {{ $highCount }}
                        <small class="fs-5 text-danger">({{ number_format($highPct, 1) }}%)</small>
                    </span>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="card shadow-sm border-0 p-3">
                <div class="d-flex justify-content-between align-items-center">
                    <span class="text-muted text-uppercase small fw-bold">Low Priority</span>
                    <span class="h5 fw-bold text-secondary mb-0">
                        {{ $lowCount }}
                        <small class="text-muted">({{ number_format($lowPct, 1) }}%)</small>
                    </span>
                </div>
            </div>
        </div>
    </div>

    <!-- 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->claim?->terminalIllness !== null);
                    @endphp
                    <tr class="{{ $isHighPriority ? 'table-danger' : '' }}">
                        <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">
                            @if($isHighPriority)
                                <span class="badge bg-danger px-2 py-1">
                                    <i class="mdi mdi-alert-octagon me-1"></i> High
                                </span>
                            @else
                                <span class="badge bg-light border text-muted px-2 py-1">Low</span>
                            @endif
                        </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)->subDay()->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>