<!-- FAQ Tab -->
<div class="tab-pane fade" id="faq" role="tabpanel">
  <div class="row g-4">
    <!-- Basic Info -->
    <div class="col-md-12">
      <div class="card border shadow-sm">
        <div class="card-header bg-white">
          <h6 class="mb-0">
            <i class="fas fa-info-circle me-2"></i>Basic Information
          </h6>
        </div>
        <div class="card-body">
          <div class="row g-3">
            <div class="col-md-6">
              <label class="form-label fw-medium">Heading</label>
              <input type="text" class="form-control" id="faqHeading" value="<%= data.faq?.heading || '' %>"
                placeholder="e.g., Got Questions? We've Got Answers" />
            </div>
            <div class="col-md-6">
              <label class="form-label fw-medium">Subheading</label>
              <input type="text" class="form-control" id="faqSubheading" value="<%= data.faq?.subheading || '' %>"
                placeholder="e.g., Visa FAQs" />
            </div>
            <div class="col-md-12">
              <label class="form-label fw-medium">Description</label>
              <textarea class="form-control" id="faqDescription" rows="3"
                placeholder="Enter description"><%= data.faq?.description || '' %></textarea>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- FAQ Items -->
    <div class="col-md-12">
      <div class="card border shadow-sm">
        <div class="card-header bg-white d-flex justify-content-between align-items-center">
          <h6 class="mb-0">
            <i class="fas fa-question-circle me-2"></i>FAQ Items
          </h6>
        </div>
        <div class="card-body" id="faqItemsContainer">
          <%
            const faqItems = (data.faq && Array.isArray(data.faq.items) && data.faq.items.length === 5)
              ? data.faq.items
              : (data.faq && Array.isArray(data.faq.items) && data.faq.items.length > 0)
                ? (function () {
                    const clone = data.faq.items.slice(0, 5);
                    while (clone.length < 5) clone.push({});
                    return clone;
                  })()
                : [{}, {}, {}, {}, {}];
          %>
          <% faqItems.forEach(function(item, index) { %>
          <div class="card mb-3 bg-light border faq-item" data-index="<%= index %>">
            <div class="card-header d-flex justify-content-between align-items-center bg-white">
              <h6 class="card-title fw-bold mb-0">FAQ
                <span class="faq-item-label">
                  <%= index + 1 %>
                </span>
              </h6>
            </div>
            <div class="card-body">
              <div class="row g-3">
                <div class="col-md-12">
                  <label class="form-label fw-medium">Question</label>
                  <input
                    type="text"
                    class="form-control"
                    id="faqQuestion_<%= index %>"
                    value="<%= item.question || '' %>"
                    placeholder="Enter question"
                  />
                </div>
                <div class="col-md-12">
                  <label class="form-label fw-medium">Answer</label>
                  <textarea
                    class="form-control"
                    id="faqAnswer_<%= index %>"
                    rows="3"
                    placeholder="Enter answer"
                  ><%= item.answer || '' %></textarea>
                </div>
              </div>
            </div>
          </div>
          <% }); %>
        </div>
      </div>
    </div>

    <!-- CTA Button -->
    <div class="col-md-12">
      <div class="card border shadow-sm">
        <div class="card-header bg-white">
          <h6 class="mb-0">
            <i class="fas fa-mouse-pointer me-2"></i>CTA Button
          </h6>
        </div>
        <div class="card-body">
          <div class="row g-3">
            <div class="col-md-6">
              <label class="form-label fw-medium">Label</label>
              <input type="text" class="form-control" id="faqCtaLabel" value="<%= data.faq?.ctaButton?.label || '' %>"
                placeholder="e.g., contact us" />
            </div>
            <div class="col-md-6">
              <label class="form-label fw-medium">Link</label>
              <input type="text" class="form-control" id="faqCtaHref" value="<%= data.faq?.ctaButton?.href || '' %>"
                placeholder="/contact" />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<script>
  // Register scraper to collect FAQ data before form submit
  window.homeScrapers = window.homeScrapers || {};
  window.homeScrapers.faq = () => {
    const getVal = (id) => (document.getElementById(id)?.value || "").trim();

    const items = [];
    document.querySelectorAll(".faq-item").forEach((el, idx) => {
      const index = el.getAttribute("data-index") || idx;

      const question = getVal(`faqQuestion_${index}`);
      const answer = getVal(`faqAnswer_${index}`);

      if (question || answer) {
        items.push({ question, answer });
      }
    });

    return {
      heading: getVal("faqHeading"),
      subheading: getVal("faqSubheading"),
      description: getVal("faqDescription"),
      ctaButton: {
        label: getVal("faqCtaLabel"),
        href: getVal("faqCtaHref")
      },
      items
    };
  };
</script>