{{-- Bảng xếp hạng dùng chung (Marketing / Telesales / CarePage). Tham số: - $items : mảng user đã sắp xếp giảm dần theo gross_revenue (mỗi phần tử có id, full_name, avatar, gross_revenue) - $metricLabel : nhãn chỉ số xếp hạng (mặc định "Doanh thu") Bố cục: 3 thẻ tóm tắt -> bục vinh danh top 3 -> danh sách đầy đủ (cuộn dọc, không giới hạn số người nên không vỡ layout). --}} @php $metricLabel = $metricLabel ?? 'Doanh thu'; // sortByDesc()->toArray() giữ key gốc => đánh lại chỉ số 0..n-1 để thứ hạng đúng $list = array_values((array) $items); $myId = \Illuminate\Support\Facades\Auth::id(); $topRevenue = count($list) ? max(1, (int) $list[0]['gross_revenue']) : 1; $totalRevenue = array_sum(array_map(function ($i) { return (int) $i['gross_revenue']; }, $list)); $myRank = null; foreach ($list as $idx => $row) { if ($row['id'] == $myId) { $myRank = $idx + 1; break; } } // Chữ cái đầu của từ cuối trong tên, dùng làm avatar khi user chưa có ảnh $initialOf = function ($name) { $words = preg_split('/\s+/u', trim((string) $name)); return mb_strtoupper(mb_substr(end($words) ?: '?', 0, 1)); }; // Thứ tự hiển thị bục: hạng 2 - hạng 1 - hạng 3 $podium = array_filter([1 => $list[1] ?? null, 0 => $list[0] ?? null, 2 => $list[2] ?? null]); $medal = [0 => 'gold', 1 => 'silver', 2 => 'bronze']; @endphp @if(count($list))
Nhân sự xếp hạng{{ count($list) }}
Tổng {{ mb_strtolower($metricLabel) }}{{ number_format($totalRevenue) }} VNĐ
Hạng của bạn{{ $myRank ? '#'.$myRank : '—' }}@if($myRank) / {{ count($list) }}@endif
@foreach($podium as $idx => $row)
@if($idx === 0) @endif
@if(!empty($row['avatar'])) @else {{ $initialOf($row['full_name']) }} @endif {{ $idx + 1 }}
{{ $row['full_name'] }}
{{ number_format((int) $row['gross_revenue']) }} VNĐ
#{{ $idx + 1 }}
@endforeach
HạngNhân sự{{ $metricLabel }}
    @foreach($list as $idx => $row) @php $pct = round(((int) $row['gross_revenue']) / $topRevenue * 100, 1); @endphp
  1. {{ $idx + 1 }}
    @if(!empty($row['avatar'])) @else {{ $initialOf($row['full_name']) }} @endif
    {{ $row['full_name'] }}@if($row['id'] == $myId) Bạn@endif
    {{ number_format((int) $row['gross_revenue']) }}{{ $pct }}% so với #1
  2. @endforeach
@else
Chưa có dữ liệu xếp hạng trong khoảng thời gian này
@endif