<?php
/**
 * Frontend output for the AUX Icon List - Multiple Lists component
 * AUX documentation: https://aggieux.tamu.edu/?path=/docs/components-icon-list-collections-icon-list-collection-multiple-lists-about-icon-list-collections-multiple-lists--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\IconListEntity;
use WP_AUX\Services\AuxCF\BlocksService;

$section = BlocksService::getSection('icon-list-collection');

$iconListMegaCol = aux_get_field('icon_list_multi_list_mega');
$listCount = !empty($iconListMegaCol) ? count($iconListMegaCol) : 0;

$theIconLists = '';
if ($iconListMegaCol) {
  $headingLevel = $section->hasIntro() ? 'h3' : 'h2';

  foreach ($iconListMegaCol as $iconListMega) {
    $iconListItems = $iconListMega['icon_list_multi_list'];

    // setting the icon list heading and bringing in the heading level
    $iconListHeadingText = $iconListMega['icon_list_multi_list_heading'];
    $iconListHeading = sprintf('<%s class="icon-list__heading">%s</%s>', $headingLevel, $iconListHeadingText, $headingLevel);

    // setting the icon list
    $iconList = IconListEntity::create();

    // Loop through top-level list items to add to icon list
    foreach ($iconListItems as $iconListItem) {
      $iconList->addItem(
        $iconListItem['icon_list_multi_list_icon'],
        $iconListItem['icon_list_multi_list_item_text'],
        $iconListItem['icon_list_multi_list_desc']
      );
    }

    // wrapping the output in a div + including heading
    $theIconLists .= sprintf('<div class="icon-list__item">%s%s</div>', $iconListHeading, $iconList);
  }
}

$section->setContent(<<<HTML
  <div class="icon-list__wrapper" data-cards="$listCount">
    $theIconLists
  </div>
HTML);

echo $section;
