<!doctype html>
<html lang="en">

<?php
include 'db.php';

// Backward-compatible migration for existing FAQ tables.
$section_column_check = $conn->query("SHOW COLUMNS FROM faqs LIKE 'section_title'");
if ($section_column_check && $section_column_check->num_rows === 0) {
    $conn->query("ALTER TABLE faqs ADD section_title VARCHAR(255) NOT NULL DEFAULT 'General' AFTER id");
}

$search = isset($_GET['search']) ? trim($_GET['search']) : '';
$where = "WHERE is_active = 1";

if ($search !== '') {
    $search_escaped = $conn->real_escape_string($search);
    $where .= " AND (section_title LIKE '%$search_escaped%' OR question LIKE '%$search_escaped%' OR answer LIKE '%$search_escaped%')";
}

$faq_sections = [];
$sql = "SELECT id, section_title, question, answer FROM faqs $where ORDER BY section_title ASC, id ASC";
$res = $conn->query($sql);
if ($res && $res->num_rows > 0) {
    while ($row = $res->fetch_assoc()) {
        $section_title = trim($row['section_title']) !== '' ? $row['section_title'] : 'General';
        if (!isset($faq_sections[$section_title])) {
            $faq_sections[$section_title] = [];
        }
        $faq_sections[$section_title][] = $row;
    }
}

$title = "FAQs | James Thompson Textiles";
$description = "Browse frequently asked questions about James Thompson Textiles products and services.";
$keywords = "JamesThompson Textiles LLC, James Thompson Textiles Greenwood Delaware, fabric supplier in Greenwood Delaware, textile manufacturer in Delaware, cotton fabric supplier Greenwood DE, jute fabric supplier Delaware, James Thompson Textiles FAQs, fabric wholesale questions Greenwood DE, textile manufacturing company Delaware, premium cotton fabrics supplier Delaware, eco-friendly textile manufacturer Greenwood, bulk fabric supplier Delaware, industrial fabric supplier Greenwood 19950, fabric production company Delaware, cotton fabric wholesale supplier in Greenwood Delaware, jute and cotton fabric manufacturer Delaware USA, where to buy bulk cotton fabric in Delaware, textile manufacturing company near Greenwood 19950, sustainable fabric supplier in Delaware, industrial cotton fabric supplier South Church Street Greenwood, James Thompson Textiles contact and business information";
$image = "https://jthompsontextiles.com/assets/images/others/share-img.png";
$current_url = "https://jthompsontextiles.com/all-faq";
include 'layouts/header.php';
?>

<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">

<body>
    <?php include 'layouts/navbar.php'; ?>

    <main id="content" class="wrapper layout-page">
        <section class="pb-16 pb-lg-18" data-animated-id="1">
            <div class="bg-body-secondary py-5">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb breadcrumb-site py-0 d-flex justify-content-center">
                        <li class="breadcrumb-item"><a class="text-decoration-none text-body" href="index">Home</a></li>
                        <li class="breadcrumb-item active pl-0 d-flex align-items-center" aria-current="page">Frequently Asked Questions</li>
                    </ol>
                </nav>
            </div>

            <div class="container">
                <div class="text-center pt-13 mb-12 mb-lg-15">
                    <div class="text-center">
                        <h2 class="fs-36px mb-11 mb-lg-14">Frequently Asked Questions</h2>
                    </div>

                    <form method="get" action="all-faq" style="max-width: 770px" class="mx-auto">
                        <div class="input-group">
                            <button type="submit" class="input-group-text bg-transparent px-6 border-0 position-absolute z-index-4 text-body-emphasis fs-5 start-0 top-0 bottom-0 m-auto">
                                <svg class="icon icon-magnifying-glass-light">
                                    <use xlink:href="#icon-magnifying-glass-light"></use>
                                </svg>
                            </button>
                            <input type="search" name="search" class="input-focus form-control ps-12" placeholder="What are you looking for?" value="<?= htmlspecialchars($search) ?>">
                        </div>
                    </form>
                </div>
            </div>

            <div class="container">
                <div class="card border-0 hover-zoom-in">
                    <div class="image-box-4"></div>
                </div>
            </div>

            <div class="container">
                <div class="pt-15 pt-lg-5">
                    <?php if (!empty($faq_sections)): ?>
                        <?php $section_index = 0; ?>
                        <?php foreach ($faq_sections as $section_title => $faqs): ?>
                            <div class="row <?= $section_index > 0 ? 'py-10' : '' ?>">
                                <div class="col-md-3">
                                    <h2 class="fs-3"><?= nl2br(htmlspecialchars($section_title)) ?></h2>
                                </div>
                                <div class="col-md-9">
                                    <div class="accordion accordion-flush" id="accordion-section-<?= $section_index ?>">
                                        <?php foreach ($faqs as $faq_index => $faq): ?>
                                            <?php
                                            $heading_id = "section-{$section_index}-heading-{$faq_index}";
                                            $collapse_id = "section-{$section_index}-collapse-{$faq_index}";
                                            $is_first_open = ($section_index === 0 && $faq_index === 0);
                                            ?>
                                            <div class="accordion-item pb-4 <?= $faq_index === 0 ? 'pt-11 pt-md-0' : 'pt-11' ?>">
                                                <h2 class="accordion-header" id="<?= $heading_id ?>">
                                                    <a class="product-info-accordion text-decoration-none <?= $is_first_open ? '' : 'collapsed' ?>"
                                                       href="#"
                                                       data-bs-toggle="collapse"
                                                       data-bs-target="#<?= $collapse_id ?>"
                                                       aria-expanded="<?= $is_first_open ? 'true' : 'false' ?>"
                                                       aria-controls="<?= $collapse_id ?>">
                                                        <span class="fs-18px"><?= htmlspecialchars($faq['question']) ?></span>
                                                    </a>
                                                </h2>
                                            </div>
                                            <div id="<?= $collapse_id ?>"
                                                 class="accordion-collapse collapse <?= $is_first_open ? 'show' : '' ?>"
                                                 aria-labelledby="<?= $heading_id ?>"
                                                 data-bs-parent="#accordion-section-<?= $section_index ?>">
                                                <div class="py-8">
                                                    <?= $faq['answer'] ?>
                                                </div>
                                            </div>
                                        <?php endforeach; ?>
                                    </div>
                                </div>
                            </div>
                            <?php $section_index++; ?>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <div class="row">
                            <div class="col-12 text-center py-10">
                                <h3 class="fs-4 mb-3">No FAQs found</h3>
                                <p class="mb-0">Try a different search term, or add FAQs from admin.</p>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </section>
    </main>

    <?php include 'layouts/footer.php'; ?>
</body>

</html>
