<div>
    <x-title>Historic Results</x-title>

    @php
        $fmtCell = function (array $row, $value) {
            if ($value === null) {
                return ['—', 'text-muted'];
            }
            return match ($row['type']) {
                'pct'   => [number_format($value, 1) . '%', 'text-dark'],
                'days'  => [number_format($value, 1) . 'd', 'text-dark'],
                'stock' => [number_format($value), 'text-dark fw-semibold'],
                default => $value > 0 ? [number_format($value), 'text-dark'] : ['—', 'text-muted'],
            };
        };

        $tabs = [
            'day'   => ['title' => 'Day',   'hint' => 'last 30 days',   'data' => $day],
            'week'  => ['title' => 'Week',  'hint' => 'last 26 weeks',  'data' => $week],
            'month' => ['title' => 'Month', 'hint' => 'last 24 months', 'data' => $month],
        ];
    @endphp

    <style>
        .hist-table { font-size: .78rem; border-collapse: separate; border-spacing: 0; }
        .hist-table th, .hist-table td { white-space: nowrap; }
        .hist-table thead th { position: sticky; top: 0; z-index: 2; background: #f1f3f5; }
        .hist-table .sticky-col { position: sticky; left: 0; z-index: 1; background: #fff; box-shadow: 1px 0 0 rgba(0,0,0,.08); }
        .hist-table thead th.sticky-col { z-index: 3; background: #f1f3f5; }
        .hist-table tbody tr:hover td { background: #f8f9fa; }
        .hist-table tbody tr:hover .sticky-col { background: #f8f9fa; }
        .hist-num { font-variant-numeric: tabular-nums; }
        .hist-scroll { max-height: 72vh; overflow: auto; }
    </style>

    {{-- Filter --}}
    <div class="d-flex flex-wrap align-items-center justify-content-between my-3 gap-2">
        <small class="text-muted">Metric “Leads In” and the full pipeline, day-by-day / week-by-week / month-by-month.</small>
        <div class="d-flex align-items-center gap-2">
            <i class="mdi mdi-filter-variant text-muted"></i>
            <select wire:model.live="leadSourceId" class="form-select form-select-sm shadow-sm" style="min-width:220px;">
                <option value="">All Lead Sources</option>
                @foreach ($leadSources as $source)
                    <option value="{{ $source->id }}">{{ $source->display_name }}</option>
                @endforeach
            </select>
        </div>
    </div>

    {{-- Tabs --}}
    <ul class="nav nav-tabs" id="historicTabs" role="tablist">
        @foreach ($tabs as $key => $tab)
            <li class="nav-item" role="presentation">
                <button class="nav-link {{ $loop->first ? 'active' : '' }}" id="tab-{{ $key }}-btn"
                        data-bs-toggle="tab" data-bs-target="#tab-{{ $key }}" type="button" role="tab">
                    {{ $tab['title'] }} <span class="text-muted small">· {{ $tab['hint'] }}</span>
                </button>
            </li>
        @endforeach
    </ul>

    <div class="tab-content">
        @foreach ($tabs as $key => $tab)
            <div class="tab-pane fade {{ $loop->first ? 'show active' : '' }}" id="tab-{{ $key }}" role="tabpanel">
                <div class="card shadow-sm border-0" style="border-radius:0 0 .375rem .375rem;">
                    <div class="hist-scroll">
                        <table class="table table-sm table-hover mb-0 hist-table hist-num">
                            <thead>
                                <tr>
                                    <th class="sticky-col ps-3 py-2 text-muted fw-semibold">Metric</th>
                                    @foreach ($tab['data']['labels'] as $label)
                                        <th class="text-center py-2 px-2 text-muted fw-normal">{{ $label }}</th>
                                    @endforeach
                                    <th class="py-2 px-3 text-muted fw-semibold" style="white-space:normal;min-width:220px;">How it&rsquo;s calculated</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($tab['data']['rows'] as $row)
                                    <tr>
                                        <td class="sticky-col ps-3 text-dark">
                                            {{ $row['label'] }}
                                            @if ($row['sub'])
                                                <span class="text-muted">{{ $row['sub'] }}</span>
                                            @endif
                                            @if ($row['type'] === 'stock')
                                                <i class="mdi mdi-clock-outline text-muted" title="Point-in-time (5pm snapshot)"></i>
                                            @endif
                                        </td>
                                        @foreach ($row['values'] as $value)
                                            @php [$text, $cls] = $fmtCell($row, $value); @endphp
                                            <td class="text-center px-2 {{ $cls }}">{{ $text }}</td>
                                        @endforeach
                                        <td class="px-3 text-muted" style="white-space:normal;">{{ $row['note'] }}</td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        @endforeach
    </div>

    <p class="text-muted small mt-2">
        <i class="mdi mdi-clock-outline me-1"></i>
        Rows marked with a clock are point-in-time counts read from the daily 5pm snapshot, so they populate from when snapshots began and fill in going forward.
    </p>
</div>
