@php
  $chapterRecord = $chapterRecord ?? null;
  $formAction = $formAction ?? '#';
  $submitLabel = $submitLabel ?? 'Save';
  $heroTitle = $heroTitle ?? 'Subject Chapter Builder';
  $heroDescription = $heroDescription ?? 'Add subject-wise and class-wise chapter names for question entry.';

  $extractSelectedId = function (string $fieldName, $storedId): int {
      $oldValue = old($fieldName);

      if (is_string($oldValue) && str_contains($oldValue, '<=>')) {
          [$id] = explode('<=>', $oldValue, 2);
          return is_numeric($id) ? (int) $id : 0;
      }

      return is_numeric($storedId) ? (int) $storedId : 0;
  };

  $selectedGroupId = $extractSelectedId('classgroup', $chapterRecord?->classgroupid);
  $selectedSubjectId = $extractSelectedId('subject', $chapterRecord?->subjectid);
  $summaryCards = $chapterRecord
      ? [
          'Chapter ID' => '#'.$chapterRecord->id,
          'Subject' => $chapterRecord->subjectname ?: '-',
          'Class Group' => $chapterRecord->classgroupname ?: '-',
          'Sort Order' => $chapterRecord->chapterorder ?? '-',
          'Status' => 'Active',
      ]
      : [
          'Subjects' => 'All Subjects',
          'Scope' => 'Class-wise',
          'Usage' => 'Question Entry',
          'Mode' => 'Master Data',
      ];
@endphp

<div class="portal-page-shell">
  <div class="portal-hero-card">
    <div class="portal-hero-main">
      <div class="portal-hero-copy">
        <h5>{{ $heroTitle }}</h5>
        <p>{{ $heroDescription }}</p>
        <div class="portal-chip-list">
          <span class="portal-chip">All Subjects</span>
          <span class="portal-chip">Class-wise Chapters</span>
          <span class="portal-chip">Question Master</span>
        </div>
        <div class="portal-hero-actions">
          <a href="{{ route('admin.managemathchapter') }}" class="btn btn-outline-primary btn-sm">Back to Subject Chapters</a>
        </div>
      </div>
    </div>
  </div>

  <div class="portal-summary-grid">
    @foreach($summaryCards as $label => $value)
      <div class="portal-summary-card">
        <span>{{ $label }}</span>
        <strong>{{ $value }}</strong>
      </div>
    @endforeach
  </div>

  <form action="{{ $formAction }}" method="post" class="portal-form-shell">
    @csrf
    @if($chapterRecord)
      <input type="hidden" name="editid" value="{{ $chapterRecord->id }}">
    @endif

    <div class="portal-form-section">
      <h6>Chapter Details</h6>
      <p>Select the subject and class group first, then save the exact chapter title used in question modules.</p>
      <div class="portal-form-grid">
        <div class="form-group portal-form-group">
          <label for="subject">Subject</label>
          <select name="subject" id="subject" class="form-control" required>
            <option value="">Select Subject</option>
            @foreach($sudentsubject as $subject)
              @php($subjectValue = $subject->id.'<=>'.$subject->subjectname)
              <option value="{{ $subjectValue }}" @selected($selectedSubjectId === (int) $subject->id)>{{ $subject->subjectname }}</option>
            @endforeach
          </select>
        </div>

        <div class="form-group portal-form-group">
          <label for="classgroup">Class Group</label>
          <select name="classgroup" id="classgroup" class="form-control" required>
            <option value="">Select Class Group</option>
            @foreach($studentsgroup as $group)
              @php($groupValue = $group->id.'<=>'.$group->studentsgroupname)
              <option value="{{ $groupValue }}" @selected($selectedGroupId === (int) $group->id)>{{ $group->studentsgroupname }}</option>
            @endforeach
          </select>
        </div>

        <div class="form-group portal-form-group">
          <label for="chaptername">Chapter Name</label>
          <input type="text" class="form-control" id="chaptername" name="chaptername" value="{{ old('chaptername', $chapterRecord->chaptername ?? '') }}" maxlength="250" placeholder="Example: Algebra, Geometry, Mensuration" required>
        </div>

        <div class="form-group portal-form-group">
          <label for="chapterorder">Chapter Order</label>
          <input type="number" class="form-control" id="chapterorder" name="chapterorder" value="{{ old('chapterorder', $chapterRecord->chapterorder ?? '') }}" min="0" max="999" placeholder="Chapter order">
          <small class="portal-form-hint">Optional, but useful for ordering chapters inside question selection.</small>
        </div>
      </div>
    </div>

    <div class="portal-form-actions">
      <p class="portal-security-note">Each chapter is saved subject-wise and class-wise, so the same chapter name can be maintained separately across different subjects or class groups.</p>
      <button type="submit" class="btn btn-primary">{{ $submitLabel }}</button>
    </div>
  </form>
</div>
