@extends('layouts.admin')

@section('content')
  @php
    $totalCollected = $gameplayhistory->sum(fn ($history) => (float) ($history->playedamount ?? 0));
    $latestAppliedAt = $gameplayhistory->max('createtime');
    $seatsLeft = max((int) ($contest->contestseat ?? 0) - (int) ($contest->joined_count ?? 0), 0);
  @endphp

  <div class="card-header">
    <h4>Competition Applications</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>{{ $contest->contesttitile }}</h5>
            <p>Review all students who joined this competition along with joined time and participation count.</p>
            <div class="portal-chip-list">
              <span class="portal-chip">Prize Pool Rs. {{ number_format((float) ($contest->totalamount ?? 0), 2) }}</span>
              <span class="portal-chip">Join Amount Rs. {{ number_format((float) ($contest->joinamt ?? 0), 2) }}</span>
              <span class="portal-chip">Win {{ number_format((float) ($contest->winpertange ?? 0), 2) }}%</span>
            </div>
            <div class="portal-hero-actions">
              <a href="{{ route('admin.editcontest', ['id' => $contest->id]) }}" class="btn btn-outline-primary btn-sm">Edit Competition</a>
              <a href="{{ route('admin.managecontest') }}" class="btn btn-info btn-sm">Back to Competitions</a>
            </div>
          </div>
        </div>
      </div>

      <div class="portal-summary-grid">
        <div class="portal-summary-card">
          <span>Total Joined</span>
          <strong>{{ number_format((int) ($contest->joined_count ?? 0)) }}</strong>
        </div>
        <div class="portal-summary-card">
          <span>Seats Left</span>
          <strong>{{ number_format($seatsLeft) }}</strong>
        </div>
        <div class="portal-summary-card">
          <span>Total Collected</span>
          <strong>Rs. {{ number_format($totalCollected, 2) }}</strong>
        </div>
        <div class="portal-summary-card">
          <span>Latest Applied</span>
          <strong>{{ $latestAppliedAt ? $latestAppliedAt->format('d-m-Y h:i A') : '-' }}</strong>
        </div>
      </div>

      <div class="table-responsive">
        <table class="table table-striped table-hover" id="tableExport" style="width:100%;">
          <thead>
            <tr>
              <th>Competition Title</th>
              <th>Student Name</th>
              <th>Mobile Number</th>
              <th>Applied Time</th>
            </tr>
          </thead>
          <tbody>
            @foreach($gameplayhistory as $history)
              <tr>
                <td>
                  <strong>{{ $history->contestname ?: $contest->contesttitile }}</strong>
                  <div class="table-subtext">Entry Amount: Rs. {{ number_format((float) ($history->playedamount ?? 0), 2) }}</div>
                </td>
                <td>{{ $history->user?->name ?: ($history->username ?: '-') }}</td>
                <td>{{ $history->user?->mobileno ?: '-' }}</td>
                <td>{{ $history->createtime?->format('d-m-Y h:i:s A') ?? '-' }}</td>
              </tr>
            @endforeach
          </tbody>
        </table>
      </div>
    </div>
  </div>
@endsection
