<section id="rewad-declation-section">
    <div class="col-lg-12 my-5">
        <div class="card custom--card primary-bg">
            <div class="card-header">
                <h5 class="card-title">@lang('Network Rewards')</h5>
            </div>
            <div class="card-body p-0">
                <table class="deposite-table">
                    <thead class="custom--table">
                        <tr>
                            <th>@lang('Level')</th>
                            <th>@lang('Total Member')</th>
                            <th>@lang('Time Period')</th>
                            <th>@lang('Reward')</th>
                            <th>@lang('Time Remained')</th>
                        </tr>
                    </thead>
                    <tbody>
                        @php
                            $rewards = RewardLib::rewards();
                            $totalReward = 0;
                            $totalMember = 0;
                            $totalTime = 0;
                        @endphp
                        @foreach ($rewards as $key => $reward)
                            @php
                                $levelKey = "level{$key}";
                            @endphp
                            <tr>
                                <td>{{ $key }}</td>
                                <td>{{ $reward['member'] }}</td>
                                <td>{{ $reward['time'] }} @lang('Day')</td>
                                <td>{{ getAmount($reward['reward']) }}
                                    {{ __($general->cur_text) }}
                                </td>
                                @php
                                    $rank = $earnedRewards->filter(fn($item) => $item->rank == $key)->first();
                                @endphp
                                <td>
                                    @if ($rank)
                                        <span class="text--success">@lang('Achieved')</span>
                                        <span>{{ $rank->created_at->diffForHumans() }}</span>
                                    @else
                                        @if (auth()->user()->network_activated_at)
                                            @php
                                                $user = auth()->user();
                                                $activatedAt = $user->network_activated_at;
                                                $endedAt = $activatedAt->copy()->addDays($reward['time']);
                                            @endphp

                                            @if ($activatedAt <= now() && now() <= $endedAt)
                                                @php
                                                    $years = $endedAt->diff(now())->y;
                                                    $months = $endedAt->diff(now())->m;
                                                    $days = $endedAt->diff(now())->days;
                                                    $hours = $endedAt->diff(now())->h;
                                                    $minutes = $endedAt->diff(now())->i;
                                                @endphp


                                                @if ($days > 0)
                                                    {{ $days }} @lang('Days')
                                                @endif

                                                @if ($hours > 0)
                                                    {{ $hours }} @lang('Hours')
                                                @endif

                                                @if ($minutes > 0)
                                                    {{ $minutes }} @lang('Minutes')
                                                @endif
                                            @else
                                                <span class="text--danger">@lang('Expired')</span>
                                            @endif
                                        @else
                                            <span>@lang('Network is not fully activated yet.')</span>
                                        @endif
                                    @endif
                                </td>
                            </tr>
                            @php
                                $totalReward += $reward['reward'];
                                $totalMember += $reward['member'];
                                $totalTime += $reward['time'];
                            @endphp
                        @endforeach
                        <tr>
                            <th colspan="3" class="text-end">@lang('Total')</th>
                            <th>{{ showAmount($totalReward) }}
                                {{ __($general->cur_text) }}
                            </th>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</section>
