<?php
/**
 * Frontend output for the AUX Q&A Collection component
 * AUX documentation: https://aggieux.tamu.edu/?path=/docs/components-q-a-collection-about-q-a-collections--docs
 *
 * Outline:
 * 1. Set variables to null
 * 2. Get fields from the post, if they have values, wrap in html
 * 3. Build the component html
 * 4. Sanitize and output
 *
 * @author Hailey Sanders
 * @since 1.0.0
 */

use WP_AUX\Entities\AuxFramework\Partials\QaPairEntity;
use WP_AUX\Services\AuxCF\BlocksService;

$qaPairs = aux_get_field('qa_pair');
$qaPairCount = 0;

// Loop through cards to get HTML for each
$qaPairHtml = '';
if ($qaPairs) {
  $qaPairCount = count($qaPairs);
  $centered = aux_get_field('qa_pair_alignment') === 'center';

  foreach ($qaPairs as $qaPair) {
    $qaPairHtml .= QaPairEntity::create(
        $qaPair['qa_pair_question'],
        $qaPair['qa_pair_answer']
      )
        ->setLink($qaPair['qa_pair_link'])
        ->setShowLabels(get_field('qa_pair_labels'))
        ->setCentered($centered);
  }
}

$section = BlocksService::getSection('qa-card-collection',true,true)
  ->setContent(<<<HTML
    <div class="qa-card-collection__wrapper" data-cards="$qaPairCount">
      $qaPairHtml
    </div>
  HTML);

echo $section;
