<?php $__env->startSection('content'); ?>
  <?php
    $questionCount = $questionsmanagemnet->count();
    $chapterOptions = collect($mathchapters ?? []);
    $selectedChapter = (string) ($selectedChapter ?? '');
    $selectedChapterResolved = false;
    $activeFilters = collect([
      $selectedSubjectId > 0 ? 'Subject Filter On' : null,
      $selectedGroupId > 0 ? 'Class Filter On' : null,
      filled($selectedChapter) ? 'Chapter Filter On' : null,
    ])->filter()->values();
  ?>

  <div class="card-header">
    <h4>Manage Questions</h4>
    <a class="btn btn-primary float-right" href="<?php echo e(route('admin.addquestions')); ?>">Add Questions</a>
  </div>

  <div class="card-body">
    <?php echo $__env->make('admin.partials.flash-messages', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>

    <div class="portal-page-shell">
      <div class="portal-hero-card">
        <div class="portal-hero-main">
          <div class="portal-hero-copy">
            <h5>Question Modules</h5>
            <p>Manage subject-wise and class-wise question blocks before using them inside competition question sets.</p>
            <div class="portal-chip-list">
              <span class="portal-chip">Objective Questions</span>
              <span class="portal-chip">10 Slots per Module</span>
              <span class="portal-chip"><?php echo e($questionCount); ?> Records</span>
              <?php $__currentLoopData = $activeFilters; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $filterLabel): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <span class="portal-chip"><?php echo e($filterLabel); ?></span>
              <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </div>
          </div>
        </div>
      </div>

      <div class="portal-form-shell">
        <form action="<?php echo e(route('admin.managequestions')); ?>" method="get">
          <div class="portal-form-grid portal-form-grid-3">
            <div class="form-group portal-form-group">
              <label for="subject">Question Subject</label>
              <select name="subject" id="subject" class="form-control">
                <option value="">Select Subject</option>
                <?php $__currentLoopData = $sudentsubject; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $subject): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                  <option value="<?php echo e($subject->id); ?>" <?php if($selectedSubjectId === (int) $subject->id): echo 'selected'; endif; ?>><?php echo e($subject->subjectname); ?></option>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
              </select>
            </div>

            <div class="form-group portal-form-group">
              <label for="questiongoupsclass">Question Class Group</label>
              <select name="goupsclass" id="questiongoupsclass" class="form-control">
                <option value="">Select Class Group</option>
                <?php $__currentLoopData = $studentsgroup; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $group): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                  <option value="<?php echo e($group->id); ?>" <?php if($selectedGroupId === (int) $group->id): echo 'selected'; endif; ?>><?php echo e($group->studentsgroupname); ?></option>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
              </select>
            </div>

            <div class="form-group portal-form-group">
              <label for="chapter">Chapter</label>
              <select name="chapter" id="chapter" class="form-control">
                <option value="">All Chapters</option>
                <?php $__currentLoopData = $chapterOptions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $chapter): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                  <?php
                    $chapterMatchesFilters = ($selectedSubjectId === 0 || $selectedSubjectId === (int) $chapter->subjectid)
                      && ($selectedGroupId === 0 || $selectedGroupId === (int) $chapter->classgroupid);
                    $isSelectedChapterOption = ! $selectedChapterResolved
                      && $selectedChapter === $chapter->chaptername
                      && $chapterMatchesFilters;
                    $selectedChapterResolved = $selectedChapterResolved || $isSelectedChapterOption;
                  ?>
                  <option
                    value="<?php echo e($chapter->chaptername); ?>"
                    data-group-id="<?php echo e($chapter->classgroupid); ?>"
                    data-subject-id="<?php echo e($chapter->subjectid); ?>"
                    <?php if($isSelectedChapterOption): echo 'selected'; endif; ?>
                  >
                    <?php echo e($chapter->chaptername); ?> (<?php echo e($chapter->subjectname ?: 'No Subject'); ?> | <?php echo e($chapter->classgroupname ?: 'No Class'); ?>)
                  </option>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                <?php if(filled($selectedChapter) && ! $selectedChapterResolved): ?>
                  <option value="<?php echo e($selectedChapter); ?>" selected data-legacy="true">Legacy: <?php echo e($selectedChapter); ?></option>
                <?php endif; ?>
              </select>
              <small class="portal-form-hint">Chapter master se direct filter choose kijiye. Subject ya class select karne par list auto short ho jayegi.</small>
            </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="<?php echo e(route('admin.managequestions')); ?>">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>Question Details</th>
                <th>Question Subject</th>
                <th>Chapter</th>
                <th>Question Class Group</th>
                <th>Action</th>
              </tr>
            </thead>
            <tbody>
              <?php $__empty_1 = true; $__currentLoopData = $questionsmanagemnet; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $question): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                <tr>
                  <td><?php echo e($loop->iteration); ?></td>
                  <td><?php echo e($question->questionstitle); ?></td>
                  <td><?php echo e($question->questionsubjectname); ?></td>
                  <td><?php echo e($question->questionchaptername ?: '-'); ?></td>
                  <td><?php echo e($question->questiongroupname); ?></td>
                  <td>
                    <a class="btn btn-success btn-sm" data-toggle="tooltip" data-placement="top" title="Edit Question" href="<?php echo e(route('admin.editquestions', ['id' => $question->id])); ?>">
                      <i class="fas fa-pencil-alt"></i>
                    </a>
                    <form action="<?php echo e(route('admin.deletequestions', ['id' => $question->id])); ?>" method="post" class="d-inline-block">
                      <?php echo csrf_field(); ?>
                      <button type="submit" class="btn btn-danger deletedata" data-toggle="tooltip" data-placement="top" title="Delete Question" data-confirm-message="Are you sure you want to delete this question?">
                        <i class="fas fa-trash"></i>
                      </button>
                    </form>
                  </td>
                </tr>
              <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                <tr>
                  <td colspan="6" class="text-center text-muted">No question modules found for the selected filters.</td>
                </tr>
              <?php endif; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
  <script>
    (function () {
      var subjectSelect = document.getElementById('subject');
      var groupSelect = document.getElementById('questiongoupsclass');
      var chapterSelect = document.getElementById('chapter');

      if (!subjectSelect || !groupSelect || !chapterSelect) {
        return;
      }

      var syncChapterOptions = function () {
        var selectedSubjectId = subjectSelect.value || '';
        var selectedGroupId = groupSelect.value || '';

        Array.prototype.forEach.call(chapterSelect.options, function (option, index) {
          if (index === 0 || option.getAttribute('data-legacy') === 'true') {
            option.hidden = false;
            option.disabled = false;
            return;
          }

          var optionSubjectId = option.getAttribute('data-subject-id') || '';
          var optionGroupId = option.getAttribute('data-group-id') || '';
          var subjectMatches = selectedSubjectId === '' || optionSubjectId === selectedSubjectId;
          var groupMatches = selectedGroupId === '' || optionGroupId === selectedGroupId;
          var isVisible = subjectMatches && groupMatches;

          option.hidden = !isVisible;
          option.disabled = !isVisible;
        });

        var selectedOption = chapterSelect.options[chapterSelect.selectedIndex];
        if (selectedOption && selectedOption.value && (selectedOption.hidden || selectedOption.disabled)) {
          chapterSelect.value = '';
        }
      };

      subjectSelect.addEventListener('change', syncChapterOptions);
      groupSelect.addEventListener('change', syncChapterOptions);
      syncChapterOptions();
    })();
  </script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.admin', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\xampp\htdocs\examproject\examproject_new\resources\views/admin/questions/managequestions.blade.php ENDPATH**/ ?>