@extends('layouts.admin')

@section('content')
  @php
    $competitionCount = $competitionAssignments->count();
    $totalPrizePool = $competitionAssignments->sum(fn ($assignment) => (float) ($assignment->contest?->totalamount ?? 0));
    $distributedCount = count($distributedAssignmentIds);
    $totalParticipants = $competitionAssignments->sum('gameplay_entries_count');
  @endphp

  <div class="card-header">
    <h4>Manage 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>Scholarship Competitions</h5>
            <p>Review scholarship competitions, open result rankings, and distribute rewards only after final verification.</p>
            <div class="portal-chip-list">
              <span class="portal-chip">{{ $competitionCount }} Competitions</span>
              <span class="portal-chip">Rs. {{ number_format((float) $totalPrizePool, 2) }} Prize Pool</span>
              <span class="portal-chip">{{ $totalParticipants }} Participants</span>
              <span class="portal-chip">{{ $distributedCount }} Distributed</span>
            </div>
          </div>
        </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>Assignment Title</th>
                <th>Contest Title</th>
                <th>Board / Group</th>
                <th>Exam Date</th>
                <th>Total Amount</th>
                <th>Join Amount</th>
                <th>Total Seat</th>
                <th>Participants</th>
                <th>Reward Status</th>
                <th>Action</th>
              </tr>
            </thead>
            <tbody>
              @forelse($competitionAssignments as $assignment)
                @php($contest = $assignment->contest)
                @php($isDistributed = in_array((int) $assignment->id, $distributedAssignmentIds, true))
                <tr>
                  <td>{{ $assignment->competitiontitle ?: (($contest?->contesttitile ?? 'Competition').' - '.($assignment->examdate?->format('d-m-Y') ?? 'No Date')) }}</td>
                  <td>{{ $contest?->contesttitile ?? 'Missing Contest' }}</td>
                  <td>{{ $assignment->board?->showname ?? 'No Board' }} / {{ $assignment->group?->groupname ?? 'No Group' }}</td>
                  <td>{{ $assignment->examdate?->format('d-m-Y') ?? '-' }}</td>
                  <td>Rs. {{ number_format((float) ($contest?->totalamount ?? 0), 2) }}</td>
                  <td>Rs. {{ number_format((float) ($contest?->joinamt ?? 0), 2) }}</td>
                  <td>{{ number_format((int) ($contest?->contestseat ?? 0)) }}</td>
                  <td>{{ number_format((int) $assignment->gameplay_entries_count) }}</td>
                  <td>
                    <span class="badge {{ $isDistributed ? 'badge-success' : 'badge-warning' }}">
                      {{ $isDistributed ? 'Distributed' : 'Pending' }}
                    </span>
                  </td>
                  <td>
                    <a class="btn btn-info btn-sm" data-toggle="tooltip" data-placement="top" title="Competition Result Details" href="{{ route('admin.resultdetails', ['id' => $assignment->id]) }}">
                      <i class="fas fa-eye"></i>
                    </a>
                  </td>
                </tr>
              @empty
                <tr>
                  <td colspan="10" class="text-center text-muted">No scholarship competitions found.</td>
                </tr>
              @endforelse
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
@endsection
