<?php include 'header.php'; ?>
 
<div class="container-fluid">
    <div class="row">
        <!-- Sidebar -->
       <div class="col-12 col-md-2 order-1 order-md-1 ">
          <h5 class="mb-3 spechead">Sub-Categories</h5>
      <button class="btn btn-primary d-md-none mb-2" type="button" data-bs-toggle="collapse" data-bs-target="#mobileSidebar">
 <h5> Select Subject</h5>
</button>

<!-- Sidebar -->
<div id="mobileSidebar" class="collapse d-md-block col-12 col-md-2">
            <?php
            $category_id = $_GET['category_id'] ?? '';
            $category_id = htmlspecialchars($category_id);

            if ($category_id) {
                $stmt = $conn->prepare("
                    SELECT DISTINCT sc.sub_cat_id, sc.sub_category
                    FROM short_questions sb
                    JOIN sub_category sc ON sb.sub_category_id = sc.sub_cat_id
                    WHERE sb.category_id = ?
                ");
                $stmt->bind_param("s", $category_id);
                $stmt->execute();
                $subcats = $stmt->get_result();
            }
            ?>

<div class="nav flex-column ">
    <ul class="mcqsside nav" >
            <?php if (!empty($subcats) && $subcats->num_rows > 0): ?>
                <?php while ($row = $subcats->fetch_assoc()): ?>
                        <li class="nav-item ">
                    <a href="view_short_questions.php?category_id=<?php echo htmlspecialchars($category_id); ?>&sub_category=<?php echo htmlspecialchars($row['sub_cat_id']); ?>"
               class=" mb-2 nav-link btn w-100 text-start btn-outline-primary <?= ($_GET['sub_category'] ?? '') == $row['sub_cat_id'] ? 'active' : '' ?>">
                <i class="bi bi-folder2-open"></i> <?php echo htmlspecialchars($row['sub_category']); ?>
            </a>
                      </li>
                <?php endwhile; ?>
                </ul>
            <?php else: ?>
                <div class="alert alert-info">No sub-categories found.</div>
            <?php endif; ?>
    </div>
            </div></div>
        <!-- Main Content -->
         <div class="col-12 col-md-7 order-1 order-md-2">
            <?php
            $sub_category = $_GET['sub_category'] ?? '';
            $sub_category = htmlspecialchars($sub_category);
            $page = max(1, (int)($_GET['page'] ?? 1));
            $limit = 5;
            $offset = ($page - 1) * $limit;

            function getSubCategoryName($conn, $id) {
                $stmt = $conn->prepare("SELECT sub_category FROM sub_category WHERE sub_cat_id = ?");
                $stmt->bind_param("s", $id);
                $stmt->execute();
                $stmt->bind_result($name);
                $stmt->fetch();
                return $name ?? 'Unknown';
            }

            if ($category_id && $sub_category) {
                // Get total count
                $stmt_count = $conn->prepare("SELECT COUNT(*) FROM short_questions WHERE category_id = ? AND sub_category_id = ?");
                $stmt_count->bind_param("ss", $category_id, $sub_category);
                $stmt_count->execute();
                $stmt_count->bind_result($total);
                $stmt_count->fetch();
                $stmt_count->close();

                // Fetch paginated questions
                $stmt = $conn->prepare("
                    SELECT question, answer, chapter FROM short_questions
                    WHERE category_id = ? AND sub_category_id = ?
                    LIMIT ?, ?
                ");
                $stmt->bind_param("ssii", $category_id, $sub_category, $offset, $limit);
                $stmt->execute();
                $questions = $stmt->get_result();
            }
            ?>

            <?php if (!empty($questions) && $questions->num_rows > 0): ?>
                <div class="page-header d-flex justify-content-between">
                    <span><?= getSubCategoryName($conn, $sub_category); ?></span>
                    <span>Page <?= $page; ?></span>
                </div>

                <?php $q_no = $offset + 1; ?>
                <?php while ($q = $questions->fetch_assoc()): ?>
                    <div class="question-card mb-3">
                           <?php
                          $chapter= htmlspecialchars($q['chapter']);
                         if($chapter > 0): ?>
            <span class="badge bg-primary float-end">Chapter: <?php echo $chapter; ?></span>
        <?php endif; ?>
                        <div class="question-title text-primary"><strong class="m-2">Q.No  <?= $q_no++; ?> :</strong> <?= htmlspecialchars($q['question']); ?></div>
                        <div class="question-answer"><strong class="m-2">Answer:</strong> <?= htmlspecialchars($q['answer']); ?></div>
                    </div>
                <?php endwhile; ?>

                <!-- Pagination -->
             
<!-- Pagination Controls -->
<?php if ($total > $limit): ?>
<?php $total_pages = ceil($total / $limit); ?>
<div class="d-flex justify-content-center mt-4">
    <nav aria-label="Page navigation">
        <ul class="pagination">
            <!-- Previous Button -->
           <?php if ($page > 1): ?>
                <li class="page-item">
                    <a class="page-link" href="?category_id=<?php echo $category_id; ?>&sub_category=<?php echo $sub_category; ?>&page=<?php echo $page - 1; ?>">Previous</a>
                </li>
            <?php endif; ?>

            <?php
            $ellipsis_shown = false;
            for ($i = 1; $i <= $total_pages; $i++) {
                if (
                    $i == 1 ||
                    $i == 2 ||
                    $i == $total_pages - 1 ||
                    $i == $total_pages ||
                    abs($i - $page) <= 1
                ) {
                    $ellipsis_shown = false;
                    ?>
                    <li class="page-item <?php echo ($i === $page) ? 'active' : ''; ?>">
                        <a class="page-link" href="?category_id=<?php echo $category_id; ?>&sub_category=<?php echo $sub_category; ?>&page=<?php echo $i; ?>"><?php echo $i; ?></a>
                    </li>
                <?php
                } elseif (!$ellipsis_shown) {
                    $ellipsis_shown = true;
                    ?>
                    <li class="page-item disabled"><span class="page-link">...</span></li>
                <?php
                }
            }
            ?>

            <!-- Next Button -->
            <?php if ($page < $total_pages): ?>
                <li class="page-item">
                    <a class="page-link" href="?category_id=<?php echo $category_id; ?>&sub_category=<?php echo $sub_category; ?>&page=<?php echo $page + 1; ?>">Next</a>
                </li>
            <?php endif; ?>
        </ul>
    </nav>
</div>
<?php else: ?>
    <div class="alert alert-info">No questions found for the selected criteria.</div>
<?php endif; ?>

         </ul>
    </nav>
  </div>
</div>


<?php else: ?>
    <div class="alert alert-info">No questions found for the selected criteria.</div>
<?php endif; ?>
            </div>
        <!-- Ads Section -->
       <div class="col-12 col-md-3 order-3">
            <div class="mt-4">
                <!-- Google AdSense -->
                <ins class="adsbygoogle"
                     style="display:block"
                     data-ad-client="ca-pub-2287119757042775"
                     data-ad-slot="4598557736"
                     data-ad-format="auto"
                     data-full-width-responsive="true"></ins>
                <script>
                    (adsbygoogle = window.adsbygoogle || []).push({});
                </script>
            </div>
            </div>
    </div>
</div>

<?php
$conn->close();
include 'footer.php';
?>
 
