@extends('layouts.admin')

@section('content')
  @php
    $submittedResults = $useranswerdetails->where('status', 1)->count();
    $topScore = (float) ($useranswerdetails->max('totalpercentage') ?? 0);
    $topMarks = (float) ($useranswerdetails->max('totalmarks') ?? 0);
  @endphp

  <div class="card-header">
    <h4>Result Management for Scholarship</h4>
  </div>

  <div class="card-body">
    @include('admin.partials.flash-messages')

    <div class="portal-page-shell">
      <div class="portal-hero-card">
        <div class="portal-hero-main">
          <div class="portal-hero-copy">
            <h5>{{ $assignment->competitiontitle ?: (($contest?->contesttitile ?? 'Scholarship Result').' - '.($assignment->examdate?->format('d-m-Y') ?? 'No Date')) }}</h5>
            <p>Review subject-wise performance and distribute scholarship rewards only after the final ranking is verified.</p>
            <div class="portal-chip-list">
              <span class="portal-chip">{{ $contest?->contesttitile ?? 'Missing Contest' }}</span>
              <span class="portal-chip">{{ $assignment->board?->showname ?? 'No Board' }}</span>
              <span class="portal-chip">{{ $assignment->group?->groupname ?? 'No Group' }}</span>
              <span class="portal-chip">Exam {{ $assignment->examdate?->format('d-m-Y') ?? '-' }}</span>
              <span class="portal-chip">Rs. {{ number_format((float) ($contest?->totalamount ?? 0), 2) }} Prize Pool</span>
              <span class="portal-chip">{{ number_format((int) ($contest?->contestseat ?? 0)) }} Seats</span>
              <span class="portal-chip">{{ number_format((int) $assignment->gameplay_entries_count) }} Participants</span>
              <span class="portal-chip">{{ $paymentDistributed ? 'Rewards Distributed' : 'Distribution Pending' }}</span>
            </div>
            <div class="portal-hero-actions">
              <a href="{{ route('admin.scholarshipcompetition') }}" class="btn btn-outline-primary btn-sm">Back to Scholarship List</a>
              @if($paymentDistributed)
                <span class="btn btn-sm btn-success" style="cursor:default;">Payment Distributed</span>
              @else
                <form action="{{ route('admin.paymentdistribute', ['id' => $id]) }}" method="post" class="d-inline-block">
                  @csrf
                  <button type="submit" class="btn btn-sm btn-danger deletedata" data-confirm-message="Are you sure you want to distribute scholarship rewards? This action should be done only once.">Payment Distribute</button>
                </form>
              @endif
            </div>
          </div>
        </div>
      </div>

      <div class="portal-summary-grid">
        <div class="portal-summary-card">
          <span>Total Results</span>
          <strong>{{ $useranswerdetails->count() }}</strong>
        </div>
        <div class="portal-summary-card">
          <span>Submitted Results</span>
          <strong>{{ $submittedResults }}</strong>
        </div>
        <div class="portal-summary-card">
          <span>Top Percentage</span>
          <strong>{{ number_format($topScore, 2) }}%</strong>
        </div>
        <div class="portal-summary-card">
          <span>Top Marks</span>
          <strong>{{ number_format($topMarks, 2) }}</strong>
        </div>
        <div class="portal-summary-card">
          <span>Assignment Batch</span>
          <strong>1</strong>
        </div>
        <div class="portal-summary-card">
          <span>Rewarded Winners</span>
          <strong>{{ number_format((int) $distributedWinnerCount) }}</strong>
        </div>
      </div>

      <div class="portal-form-shell">
        <div class="table-responsive">
          <table class="table table-striped table-hover" id="tableExport" style="width:100%;">
            <thead>
              <tr>
                <th>Rank</th>
                <th>Student Name</th>
                <th>G.K. %</th>
                <th>Science %</th>
                <th>Hindi %</th>
                <th>English %</th>
                <th>Maths %</th>
                <th>Total Marks</th>
                <th>Student %</th>
                <th>Submitted Time</th>
              </tr>
            </thead>
            <tbody>
              @forelse($useranswerdetails as $answer)
                <tr>
                  <td>{{ $loop->iteration }}</td>
                  <td>{{ $answer->user?->name ?? 'No User' }}</td>
                  <td>{{ $answer->gkpercentage }}</td>
                  <td>{{ $answer->sciencepercentage }}</td>
                  <td>{{ $answer->hindipercentage }}</td>
                  <td>{{ $answer->englishpercentage }}</td>
                  <td>{{ $answer->mathspercentage }}</td>
                  <td>{{ $answer->totalmarks }}</td>
                  <td>{{ $answer->totalpercentage }}</td>
                  <td>{{ $answer->createdtime?->format('d-m-Y h:i:s A') ?? '-' }}</td>
                </tr>
              @empty
                <tr>
                  <td colspan="10" class="text-center text-muted">No result records found for this assignment.</td>
                </tr>
              @endforelse
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
@endsection
