<div>
    <x-title>Days In Status Report</x-title>

    <div class="card shadow-sm border-0 mb-3">
        <div class="card-body">
            <div class="row align-items-end">
                <div class="col-md-4">
                    <label for="leadSourceId" class="form-label fw-semibold">
                        Lead Source
                    </label>

                    <select
                        id="leadSourceId"
                        wire:model.live="leadSourceId"
                        class="form-select"
                    >
                        <option value="">All Lead Sources</option>

                        @foreach($leadSources as $leadSource)
                            <option value="{{ $leadSource->id }}">
                                {{ $leadSource->display_name }}
                            </option>
                        @endforeach
                    </select>
                </div>
            </div>
        </div>
    </div>

    <div class="card shadow-sm border-0">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="table-light">
                    <tr>
                        <th class="ps-4 py-3 border-0">Status Name</th>
                        <th class="text-center border-0">Today</th>
                        <th class="text-center border-0">2-7 Days</th>
                        <th class="text-center border-0">8-30 Days</th>
                        <th class="text-center border-0">31-60 Days</th>
                        <th class="text-center border-0">61-90 Days</th>
                        <th class="text-center border-0">90+ Days</th>
                        <th class="text-center border-0">Current Claims</th>
                        <th class="pe-4 text-end border-0">History Total</th>
                    </tr>
                    </thead>

                    <tbody>
                    @forelse($reportData as $row)
                        @php
                            $total = $row->range_today
                                + $row->range_week
                                + $row->range_month
                                + $row->range_60
                                + $row->range_90
                                + $row->range_old;

                            $baseFilters = array_filter([
                                'status_id' => $row->id,
                                'lead_source_id' => $leadSourceId,
                            ], fn ($value) => filled($value));
                        @endphp

                        <tr>
                            <td class="ps-4 fw-bold text-dark">
                                {{ $row->name }}
                            </td>

                            <td class="text-center">
                                @if($row->range_today > 0)
                                    <a
                                        href="{{ route('clients.index', array_merge($baseFilters, [
                                            'status_filter_type' => 'history',
                                            'days_range' => 'today',
                                        ])) }}"
                                        class="text-decoration-none"
                                    >
                                @endif

                                <span class="badge rounded-pill {{ $row->range_today > 0 ? 'bg-success-subtle text-success' : 'bg-light text-muted' }} px-3">
                                    {{ $row->range_today }}
                                </span>

                                @if($row->range_today > 0)
                                    </a>
                                @endif
                            </td>

                            <td class="text-center">
                                @if($row->range_week > 0)
                                    <a
                                        href="{{ route('clients.index', array_merge($baseFilters, [
                                            'status_filter_type' => 'history',
                                            'days_range' => 'week',
                                        ])) }}"
                                        class="text-decoration-none"
                                    >
                                @endif

                                <span class="badge rounded-pill {{ $row->range_week > 0 ? 'bg-info-subtle text-info' : 'bg-light text-muted' }} px-3">
                                    {{ $row->range_week }}
                                </span>

                                @if($row->range_week > 0)
                                    </a>
                                @endif
                            </td>

                            <td class="text-center">
                                @if($row->range_month > 0)
                                    <a
                                        href="{{ route('clients.index', array_merge($baseFilters, [
                                            'status_filter_type' => 'history',
                                            'days_range' => 'month',
                                        ])) }}"
                                        class="text-decoration-none"
                                    >
                                @endif

                                <span class="badge rounded-pill {{ $row->range_month > 0 ? 'bg-warning-subtle text-warning' : 'bg-light text-muted' }} px-3">
                                    {{ $row->range_month }}
                                </span>

                                @if($row->range_month > 0)
                                    </a>
                                @endif
                            </td>

                            <td class="text-center">
                                @if($row->range_60 > 0)
                                    <a
                                        href="{{ route('clients.index', array_merge($baseFilters, [
                                            'status_filter_type' => 'history',
                                            'days_range' => '60',
                                        ])) }}"
                                        class="text-decoration-none"
                                    >
                                @endif

                                <span class="badge rounded-pill {{ $row->range_60 > 0 ? 'bg-warning-subtle text-warning' : 'bg-light text-muted' }} px-3">
                                    {{ $row->range_60 }}
                                </span>

                                @if($row->range_60 > 0)
                                    </a>
                                @endif
                            </td>

                            <td class="text-center">
                                @if($row->range_90 > 0)
                                    <a
                                        href="{{ route('clients.index', array_merge($baseFilters, [
                                            'status_filter_type' => 'history',
                                            'days_range' => '90',
                                        ])) }}"
                                        class="text-decoration-none"
                                    >
                                @endif

                                <span class="badge rounded-pill {{ $row->range_90 > 0 ? 'bg-danger-subtle text-danger' : 'bg-light text-muted' }} px-3">
                                    {{ $row->range_90 }}
                                </span>

                                @if($row->range_90 > 0)
                                    </a>
                                @endif
                            </td>

                            <td class="text-center">
                                @if($row->range_old > 0)
                                    <a
                                        href="{{ route('clients.index', array_merge($baseFilters, [
                                            'status_filter_type' => 'history',
                                            'days_range' => 'old',
                                        ])) }}"
                                        class="text-decoration-none"
                                    >
                                @endif

                                <span class="badge rounded-pill {{ $row->range_old > 0 ? 'bg-dark-subtle text-dark' : 'bg-light text-muted' }} px-3">
                                    {{ $row->range_old }}
                                </span>

                                @if($row->range_old > 0)
                                    </a>
                                @endif
                            </td>

                            <td class="text-center fw-bold">
                                @if($row->current_claim_count > 0)
                                    <a
                                        href="{{ route('clients.index', $baseFilters) }}"
                                        class="text-decoration-none"
                                    >
                                @endif

                                <span class="badge rounded-pill {{ $row->current_claim_count > 0 ? 'bg-primary-subtle text-primary' : 'bg-light text-muted' }} px-3">
                                    {{ number_format($row->current_claim_count) }}
                                </span>

                                @if($row->current_claim_count > 0)
                                    </a>
                                @endif
                            </td>

                            <td class="pe-4 text-end fw-bold text-secondary">
                                {{ number_format($total) }}
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="9" class="text-center py-5 text-muted">
                                <i class="bi bi-inbox d-block mb-2 fs-2"></i>
                                No claims found for the selected lead source.
                            </td>
                        </tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>