<div>
    <x-title :breadcrumbs="[ ['link' => route('reports.tranche.index'), 'title' => 'Tranche DsarReport']]">
        Tranche Report
    </x-title>

    <!-- Stats Grid -->
    <div class="row mb-3">
        <div class="col-md-4 mb-3 mb-md-0">
            <div class="card shadow-sm border-0 text-center p-4">
                <span class="h2 font-weight-bold text-primary mb-1">{{ $stats['funded'] }}</span>
                <p class="text-muted text-uppercase small font-weight-bold mb-0">Funded Cases</p>
            </div>
        </div>
        <div class="col-md-4 mb-3 mb-md-0">
            <div class="card shadow-sm border-0 text-center p-4">
                <span class="h2 font-weight-bold text-success mb-1">{{ $stats['available'] }}</span>
                <p class="text-muted text-uppercase small font-weight-bold mb-0">Available To Fund</p>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card shadow-sm border-0 text-center p-4">
                <span class="h2 font-weight-bold text-warning mb-1">{{ $stats['pending'] }}</span>
                <p class="text-muted text-uppercase small font-weight-bold mb-0">Pending Availability</p>
            </div>
        </div>
    </div>

    <!-- Priority Breakdown -->
    @php
        $totalFunded = $stats['funded'];
        $highPct     = $totalFunded > 0 ? ($stats['high_priority'] / $totalFunded) * 100 : 0;
        $lowPct      = $totalFunded > 0 ? ($stats['low_priority']  / $totalFunded) * 100 : 0;
    @endphp
    <div class="row mb-4">
        <div class="col-md-6 mb-3 mb-md-0">
            <div class="card shadow-sm border-0 border-start border-danger border-4 bg-danger bg-opacity-10 p-4">
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <p class="text-danger text-uppercase small fw-bold mb-0">
                        <i class="mdi mdi-alert-octagon me-1"></i> High Priority Cases
                    </p>
                    <span class="h2 fw-bold text-danger mb-0">
                        {{ $stats['high_priority'] }}
                        <small class="fs-5 text-danger">({{ number_format($highPct, 1) }}%)</small>
                    </span>
                </div>
                <p class="text-muted small mb-0">Client is 68+ or has a terminal illness.</p>
            </div>
        </div>
        <div class="col-md-6">
            <div class="card shadow-sm border-0 p-4">
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <p class="text-muted text-uppercase small fw-bold mb-0">Low Priority Cases</p>
                    <span class="h4 fw-bold text-secondary mb-0">
                        {{ $stats['low_priority'] }}
                        <small class="text-muted">({{ number_format($lowPct, 1) }}%)</small>
                    </span>
                </div>
                <p class="text-muted small mb-0">All other funded cases.</p>
            </div>
        </div>
    </div>

    <!-- Tranches Table -->
    <div class="card shadow-sm border-0 rounded-lg overflow-hidden">
        <div class="card-header bg-white py-3 border-bottom-0 d-flex justify-content-between align-items-center">
            <h2 class="h6 font-weight-bold mb-0 text-secondary">Tranche List</h2>
            <input wire:model.live="search" type="text" placeholder="Search tranches..."
                   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.75rem; letter-spacing: 0.05em;">
                <tr>
                    <th class="px-4 border-top-0">Funder</th>
                    <th class="px-4 border-top-0">Tranche Name</th>
                    <th class="px-4 border-top-0">Total Funding</th>
                    <th class="px-4 border-top-0 text-center">Cases</th>
                    <th class="px-4 border-top-0 text-center" style="min-width: 180px">Priority (High / Low)</th>
                    <th class="px-4 border-top-0 text-center">Actions</th>
                </tr>
                </thead>
                <tbody class="bg-white">
                @foreach($tranches as $tranche)
                    @php
                        $total    = $tranche->clients_count;
                        $high     = $tranche->high_priority_count;
                        $low      = max(0, $total - $high);
                        $highPct  = $total > 0 ? ($high / $total) * 100 : 0;
                        $lowPct   = $total > 0 ? ($low  / $total) * 100 : 0;
                    @endphp
                    <tr>
                        <td class="px-4 align-middle font-weight-bold text-dark">{{ $tranche->funder_name }}</td>
                        <td class="px-4 align-middle text-muted">{{ $tranche->tranche_name }}</td>
                        <td class="px-4 align-middle font-weight-bold text-success">
                            £{{ number_format($tranche->amount_per_case * $tranche->clients_count, 2) }}
                        </td>
                        <td class="px-4 align-middle text-center">
                            <span class="badge bg-primary px-3 py-2">{{ $tranche->clients_count }}</span>
                        </td>
                        <td class="px-4 align-middle {{ $high > 0 ? 'bg-danger bg-opacity-10' : '' }}">
                            <div class="d-flex justify-content-between align-items-center small mb-1">
                                <span class="badge {{ $high > 0 ? 'bg-danger' : 'bg-light text-muted border' }} px-2 py-1">
                                    @if($high > 0)<i class="mdi mdi-alert-octagon me-1"></i>@endif
                                    {{ $high }} high
                                </span>
                                <span class="text-secondary">{{ $low }} low</span>
                            </div>
                            <div class="progress" style="height: 8px;" role="progressbar"
                                 aria-valuenow="{{ $highPct }}" aria-valuemin="0" aria-valuemax="100"
                                 title="{{ number_format($highPct, 1) }}% high / {{ number_format($lowPct, 1) }}% low">
                                <div class="progress-bar bg-danger" style="width: {{ $highPct }}%"></div>
                                <div class="progress-bar bg-secondary" style="width: {{ $lowPct }}%"></div>
                            </div>
                        </td>
                        <td class="px-4 align-middle text-center">
                            <a wire:navigate href="{{ route('reports.tranche.view', $tranche) }}"
                               class="btn btn-md">
                                <i class="mdi mdi-view-dashboard text-primary"></i>
                            </a>
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>
        <div class="card-footer bg-light border-0">
            <div class="d-flex justify-content-center">
                {{ $tranches->links() }}
            </div>
        </div>
    </div>
</div>