@extends('layouts.admin')

@section('content')
  @php
    $assignmentCount = $studentsgroup->sum(fn ($group) => $group->assignments->count());
  @endphp

  <div class="card-header">
    <h4>Competition Mapping Details</h4>
  </div>
  <div class="card-body">
    @include('admin.partials.flash-messages')

    <div class="competition-summary-grid">
      <div class="competition-summary-card">
        <span>Visible Groups</span>
        <strong>{{ number_format($studentsgroup->count()) }}</strong>
      </div>
      <div class="competition-summary-card">
        <span>Total Assignments</span>
        <strong>{{ number_format($assignmentCount) }}</strong>
      </div>
    </div>

    @forelse($studentsgroup as $group)
      <div class="competition-block">
        <div class="competition-block-header">
          <h5>{{ $group->studentsgroupname }} Group</h5>
          <span>{{ number_format($group->assignments->count()) }} competition{{ $group->assignments->count() === 1 ? '' : 's' }}</span>
        </div>

        <div class="table-responsive">
          <table class="table table-striped table-hover" id="tableExport{{ $group->id }}" style="width:100%;">
            <thead>
              <tr>
                <th>S.No.</th>
                <th>Competition Name</th>
                <th>Class Group</th>
                <th>Join Amount</th>
                <th>Start Date</th>
                <th>End Date</th>
                <th>Exam Date</th>
                <th>Result Date</th>
                <th>Action</th>
              </tr>
            </thead>
            <tbody>
              @forelse($group->assignments as $assignment)
                <tr>
                  <td>{{ $loop->iteration }}</td>
                  <td>{{ $assignment->compitetionname }}</td>
                  <td>{{ $assignment->groupclassname }}</td>
                  <td>Rs. {{ number_format((float) ($assignment->contest?->joinamt ?? 0), 2) }}</td>
                  <td>{{ $assignment->startdate?->format('d-m-Y') ?? '-' }}</td>
                  <td>{{ $assignment->enddate?->format('d-m-Y') ?? '-' }}</td>
                  <td>{{ $assignment->examdate?->format('d-m-Y') ?? '-' }}</td>
                  <td>{{ $assignment->resultdate?->format('d-m-Y') ?? '-' }}</td>
                  <td>
                    <a class="btn btn-success btn-sm" data-toggle="tooltip" data-placement="top" title="User Competition Details" href="{{ route('admin.competitionstudentsdetails', ['id' => $assignment->id]) }}">
                      <i class='fas fa-eye'></i>
                    </a>
                  </td>
                </tr>
              @empty
                <tr>
                  <td colspan="9" class="competition-empty-row">No competition assignments mapped in this group yet.</td>
                </tr>
              @endforelse
            </tbody>
          </table>
        </div>
      </div>
    @empty
      <div class="competition-empty-state">
        No competition mapping found for the current student scope.
      </div>
    @endforelse
  </div>

@endsection
