@extends('layouts.front')

@section('content')
@php
    $scoreEntries = $gameplayhistory->getCollection();
    $bestRank = $scoreEntries->pluck('winning_rank')->filter(fn ($rank) => (int) $rank > 0)->min();
    $totalWinningAmount = (float) $scoreEntries->sum('winning_amount');
    $uniqueCompetitions = $scoreEntries->pluck('contestname')->filter()->unique()->count();
@endphp
<div class="jel-portal-page">
    @include('front.partials.flash-messages')

    <section class="jel-history-intro">
        <span class="jel-portal-kicker">Score Table</span>
        <h1>Your reward-based scoring results.</h1>
        <p>Ranking, join amount aur winning reward ke sath result table yahan available hai. Har row ek completed reward-linked entry ko represent karti hai.</p>

        <div class="jel-result-summary-grid jel-history-summary-grid">
            <article>
                <span>Total Rows</span>
                <strong>{{ $gameplayhistory->total() }}</strong>
            </article>
            <article>
                <span>Page Winning Total</span>
                <strong>&#8377;{{ number_format($totalWinningAmount, 2) }}</strong>
            </article>
            <article>
                <span>Best Rank</span>
                <strong>{{ $bestRank ?: 'Pending' }}</strong>
            </article>
            <article>
                <span>Competitions</span>
                <strong>{{ $uniqueCompetitions }}</strong>
            </article>
        </div>

        <div class="jel-inline-actions">
            <a class="jel-portal-button jel-portal-button-muted" href="{{ route('scholershipresult') }}">Scholarship Results</a>
            <a class="jel-portal-button jel-portal-button-secondary" href="{{ route('withdrawhistory') }}">Withdraw History</a>
        </div>
    </section>

    <section class="jel-table-card">
        <div class="jel-history-card-head">
            <div>
                <span class="jel-portal-kicker">Scoring Result</span>
                <h2>Result Exam Score Table</h2>
            </div>
            <span class="jel-history-count">{{ $gameplayhistory->total() }} Total Rows</span>
        </div>

        <div class="jel-table-wrap">
            <table class="jel-data-table jel-data-table-stack">
                <thead>
                    <tr>
                        <th>User Rank</th>
                        <th>User Name</th>
                        <th>Competition Name</th>
                        <th>Join Amount</th>
                        <th>Win Amount</th>
                        <th>Applied Date</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($gameplayhistory as $gameplayhistorydata)
                        <tr>
                            <td data-label="User Rank">{{ $gameplayhistorydata->winning_rank }}</td>
                            <td data-label="User Name">{{ $gameplayhistorydata->username }}</td>
                            <td data-label="Competition Name">{{ $gameplayhistorydata->contestname }}</td>
                            <td data-label="Join Amount">&#8377;{{ number_format((float) $gameplayhistorydata->playedamount, 2) }}</td>
                            <td data-label="Win Amount">&#8377;{{ number_format((float) $gameplayhistorydata->winning_amount, 2) }}</td>
                            <td data-label="Applied Date">{{ optional($gameplayhistorydata->createtime)->format('d M Y, h:i A') }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="jel-empty-table-cell">No scoring result available yet.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        @if ($gameplayhistory->hasPages())
            <div class="jel-simple-pagination">
                @if ($gameplayhistory->onFirstPage())
                    <span class="is-disabled">Previous</span>
                @else
                    <a href="{{ $gameplayhistory->previousPageUrl() }}">Previous</a>
                @endif

                <span>Page {{ $gameplayhistory->currentPage() }} of {{ $gameplayhistory->lastPage() }}</span>

                @if ($gameplayhistory->hasMorePages())
                    <a href="{{ $gameplayhistory->nextPageUrl() }}">Next</a>
                @else
                    <span class="is-disabled">Next</span>
                @endif
            </div>
        @endif
    </section>
</div>
@endsection
