@extends('layouts.admin')    
@section('content')
  @php
    $contestType = $contest_type ?? 'paid';
    $contestTypeLabel = $contest_type_label ?? ($contestType === 'free' ? 'Free Exam' : 'Paid Competition');
    $isFreeModule = $contestType === 'free';
    $moduleTitle = $module_title ?? 'Manage Competition';
    $moduleNote = $module_note ?? 'From here you can create contests, update reward slabs, remove unused contests and inspect joined students.';
    $addRouteName = $add_route_name ?? 'admin.addcontest';
    $editRouteName = $edit_route_name ?? 'admin.editcontest';
  @endphp
  <div class="card-header">
    <h4>{{ $moduleTitle }}</h4>
    <a class="btn btn-primary float-right" href="{{ route($addRouteName) }}">{{ $isFreeModule ? 'Add Free Exam' : 'Add Competition' }}</a>
  </div>
  <div class="card-body">
    @include('admin.partials.flash-messages')

    <div class="contest-summary-grid">
      <div class="contest-summary-card">
        <span>{{ $isFreeModule ? 'Total Free Exams' : 'Total Competitions' }}</span>
        <strong>{{ number_format($matchcobntests->count()) }}</strong>
      </div>
      <div class="contest-summary-card">
        <span>Total Prize Pool</span>
        <strong>₹{{ number_format((float) $matchcobntests->sum('totalamount'), 2) }}</strong>
      </div>
      <div class="contest-summary-card">
        <span>Total Seats</span>
        <strong>{{ number_format((float) $matchcobntests->sum('contestseat')) }}</strong>
      </div>
      <div class="contest-summary-card">
        <span>{{ $isFreeModule ? 'Scholarship Rule' : 'Total Joined Entries' }}</span>
        <strong>{{ $isFreeModule ? 'Top 3 / class, Top 10 final' : number_format((float) $matchcobntests->sum('joined_count')) }}</strong>
      </div>
    </div>

    <div class="student-list-note">
      <strong>{{ $contestTypeLabel }} overview:</strong> {{ $moduleNote }}
    </div>

    <div class="table-responsive">
      <table class="table table-striped table-hover" id="tableExport" style="width:100%;">
        <thead>
          <tr>
            <th>{{ $isFreeModule ? 'Free Exam Title' : 'Contest Title' }}</th>
            <th>Type</th>
            <th>Prize Pool</th>
            <th>Join Amount</th>
            <th>Total Seats</th>
            <th>Per User Entry</th>
            <th>Joined</th>
            <th>Seats Left</th>
            @if($isFreeModule)
            <th>Scholarship Rule</th>
            @endif
            <th>Action</th>
          </tr>
        </thead>
        <tbody>
          @foreach($matchcobntests as $contest)
            @php
              $seatsLeft = max((int) $contest->contestseat - (int) $contest->joined_count, 0);
            @endphp
            <tr>
              <td>
                <strong>{{ $contest->contesttitile }}</strong>
                <div class="table-subtext">
                  {{ $isFreeModule ? 'Daily class-wise scholarship pool ready' : 'Win %: '.$contest->winpertange.'%' }}
                </div>
              </td>
              <td>{{ $contest->contest_type_label }}</td>
              <td>&#8377;{{ number_format((float) $contest->totalamount, 2) }}</td>
              <td>&#8377;{{ number_format((float) $contest->joinamt, 2) }}</td>
              <td>{{ number_format((float) $contest->contestseat) }}</td>
              <td>{{ $contest->userentery }}</td>
              <td>{{ number_format((float) $contest->joined_count) }}</td>
              <td>{{ number_format((float) $seatsLeft) }}</td>
              @if($isFreeModule)
              <td>Top 3 per class/day, final top 10 overall</td>
              @endif
              <td class="contest-actions">
                <a class="btn btn-success btn-sm" data-toggle="tooltip" data-placement="top" title="Edit {{ $contestTypeLabel }}" href="{{ route($editRouteName, ['id' => $contest->id]) }}">
                  <i class='fas fa-pencil-alt'></i>
                </a>
                <form action="{{ route('admin.deletecontest', ['id' => $contest->id]) }}" method="post" class="d-inline-block">
                  @csrf
                  <button type="submit" class="btn btn-danger btn-sm deletedata" data-toggle="tooltip" data-placement="top" title="Delete {{ $contestTypeLabel }}" data-confirm-message="Are you sure you want to delete this {{ strtolower($contestTypeLabel) }}?">
                    <i class='fas fa-trash'></i>
                  </button>
                </form>
                <a class="btn btn-info btn-sm" data-toggle="tooltip" data-placement="top" title="Competition User Details" href="{{ route('admin.applieddetails', ['id' => $contest->id]) }}">
                  <i class='fas fa-users'></i>
                </a>
              </td>
            </tr>
          @endforeach
        </tbody>
      </table>
    </div>
  </div>
@endsection
