@extends('layouts.admin')

@section('content')
  @php
    $activeStudentsCount = $usersdetals->where('status', 1)->count();
    $classGroupCount = $usersdetals
        ->pluck('studentProfile.classname')
        ->filter()
        ->unique()
        ->count();
  @endphp

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

    <div class="student-summary-grid">
      <div class="student-summary-card">
        <span>Total Students</span>
        <strong>{{ number_format($usersdetals->count()) }}</strong>
      </div>
      <div class="student-summary-card">
        <span>Active Students</span>
        <strong>{{ number_format($activeStudentsCount) }}</strong>
      </div>
      <div class="student-summary-card">
        <span>Class Groups</span>
        <strong>{{ number_format($classGroupCount) }}</strong>
      </div>
    </div>

    @if($usersdetals->isEmpty())
      <div class="student-empty-state">
        No students are available in the current institute or school scope yet.
      </div>
    @else
      <div class="table-responsive">
        <table class="table table-striped table-hover" id="tableExport" style="width:100%;">
          <thead>
            <tr>
              <th>Student</th>
              <th>Mobile Number</th>
              <th>Parents</th>
              <th>Class Group</th>
              <th>Status</th>
              <th>Action</th>
            </tr>
          </thead>
          <tbody>
            @foreach($usersdetals as $student)
              <tr>
                <td>
                  <strong>{{ $student->name }}</strong>
                  <div class="student-table-subtext">{{ $student->institutecode ?: 'Institute code not available' }}</div>
                </td>
                <td>
                  @if($student->mobileno)
                    <a href="tel:{{ $student->mobileno }}">{{ $student->mobileno }}</a>
                  @else
                    -
                  @endif
                </td>
                <td>
                  <div>{{ $student->studentProfile?->fathername ?: '-' }}</div>
                  <div class="student-table-subtext">{{ $student->studentProfile?->mothername ?: 'Mother name not available' }}</div>
                </td>
                <td>{{ $student->studentProfile?->classname ?: '-' }}</td>
                <td>
                  @if((int) $student->status === 1)
                    <span class="badge badge-success">Active</span>
                  @else
                    <span class="badge badge-danger">Blocked</span>
                  @endif
                </td>
                <td class="student-action-row">
                  <a class="btn btn-success btn-sm" data-toggle="tooltip" data-placement="top" title="Profile Details" href="{{ route('admin.userprofile', ['id' => $student->id]) }}">
                    <i class='fas fa-eye'></i>
                  </a>
                  <a class="btn btn-info btn-sm" data-toggle="tooltip" data-placement="top" title="User Play Details" href="{{ route('admin.studentscontest', ['id' => $student->id]) }}">
                    <i class='fas fa-users'></i>
                  </a>
                  <a class="btn btn-dark btn-sm" data-toggle="tooltip" data-placement="top" title="Change Password" href="{{ route('admin.changepassword', ['userid' => $student->id]) }}">
                    <i class='fas fa-key'></i>
                  </a>
                </td>
              </tr>
            @endforeach
          </tbody>
        </table>
      </div>
    @endif
  </div>

@endsection
