@extends('layouts.admin')

@section('content')
  @php
    $chapterCount = $mathchapters->count();
  @endphp

  <div class="card-header">
    <h4>Manage Subject Chapters</h4>
    <a class="btn btn-primary float-right" href="{{ route('admin.addmathchapter') }}">Add Subject Chapter</a>
  </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>Subject Chapter Master</h5>
            <p>Maintain subject-wise and class-wise chapter names so question entry always maps through a clean master list.</p>
            <div class="portal-chip-list">
              <span class="portal-chip">{{ $chapterCount }} Chapters</span>
              <span class="portal-chip">Class-wise Mapping</span>
              <span class="portal-chip">Subject-wise Mapping</span>
              <span class="portal-chip">{{ $totalQuestionModules ?? 0 }} Question Modules</span>
              <span class="portal-chip">{{ $totalQuestionSlots ?? 0 }} Filled Questions</span>
            </div>
          </div>
        </div>
      </div>

      <div class="portal-form-shell">
        <form action="{{ route('admin.managemathchapter') }}" method="get">
          <div class="portal-form-grid">
            <div class="form-group portal-form-group">
              <label for="group">Class Group</label>
              <select name="group" id="group" class="form-control">
                <option value="">All Class Groups</option>
                @foreach($studentsgroup as $group)
                  <option value="{{ $group->id }}" @selected($selectedGroupId === (int) $group->id)>{{ $group->studentsgroupname }}</option>
                @endforeach
              </select>
            </div>

            <div class="form-group portal-form-group">
              <label for="subject">Subject</label>
              <select name="subject" id="subject" class="form-control">
                <option value="">All Subjects</option>
                @foreach($sudentsubject as $subject)
                  <option value="{{ $subject->id }}" @selected($selectedSubjectId === (int) $subject->id)>{{ $subject->subjectname }}</option>
                @endforeach
              </select>
            </div>

            <div class="form-group portal-form-group">
              <label>&nbsp;</label>
              <div class="portal-link-row">
                <button type="submit" class="btn btn-primary">Search</button>
                <a class="btn btn-outline-danger" href="{{ route('admin.managemathchapter') }}">Clear All</a>
              </div>
            </div>
          </div>
        </form>
      </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>#</th>
                <th>Chapter Name</th>
                <th>Subject</th>
                <th>Class Group</th>
                <th>Question Modules</th>
                <th>Filled Questions</th>
                <th>Order</th>
                <th>Action</th>
              </tr>
            </thead>
            <tbody>
              @forelse($mathchapters as $chapter)
                <tr>
                  <td>{{ $loop->iteration }}</td>
                  <td>{{ $chapter->chaptername }}</td>
                  <td>{{ $chapter->subjectname ?: '-' }}</td>
                  <td>{{ $chapter->classgroupname }}</td>
                  <td>{{ $chapter->question_module_count ?? 0 }}</td>
                  <td>{{ $chapter->question_slot_count ?? 0 }}</td>
                  <td>{{ $chapter->chapterorder ?? '-' }}</td>
                  <td>
                    <a
                      class="btn btn-info btn-sm"
                      data-toggle="tooltip"
                      data-placement="top"
                      title="View Questions"
                      href="{{ route('admin.managequestions', ['subject' => $chapter->subjectid, 'goupsclass' => $chapter->classgroupid, 'chapter' => $chapter->chaptername]) }}"
                    >
                      <i class="fas fa-eye"></i>
                    </a>
                    <a class="btn btn-success btn-sm" data-toggle="tooltip" data-placement="top" title="Edit Subject Chapter" href="{{ route('admin.editmathchapter', ['id' => $chapter->id]) }}">
                      <i class="fas fa-pencil-alt"></i>
                    </a>
                    <form action="{{ route('admin.deletemathchapter', ['id' => $chapter->id]) }}" method="post" class="d-inline-block">
                      @csrf
                      <button type="submit" class="btn btn-danger deletedata" data-toggle="tooltip" data-placement="top" title="Delete Subject Chapter" data-confirm-message="Are you sure you want to delete this subject chapter?">
                        <i class="fas fa-trash"></i>
                      </button>
                    </form>
                  </td>
                </tr>
              @empty
                <tr>
                  <td colspan="8" class="text-center text-muted">No subject chapters found for the selected filter.</td>
                </tr>
              @endforelse
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
@endsection
