<?php
/**
 * Frontend output for the AUX Contact Card Collection component
 * AUX documentation: https://aggieux.tamu.edu/?path=/docs/components-contact-cards-directories-contact-card-collection-about-contact-card-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\ContactCardEntity;
use WP_AUX\Services\AuxCF\BlocksService;

$contactCards = aux_get_field('contact_card');
$contactCardsCount = !empty($contactCards) ? count($contactCards) : 0;

$section = BlocksService::getSection('profile-collection');
$section->addComponentModifier('inline');

if (get_field('contact_card_collection_has_feature_switch')) {
  $section->addComponentModifier('has-feature');
}

// Loop through cards to get HTML for each
$contactCardsHtml = '';
if ($contactCards) {
  foreach ($contactCards as $contactCard) {
    $contactCardsHtml .= ContactCardEntity::create(
      $contactCard['contact_card_name'],
      $contactCard['contact_card_title'],
      $contactCard['contact_card_org']
    )->setContactInfo(
      $contactCard['contact_card_email'],
      $contactCard['contact_card_phone'],
      $contactCard['contact_card_profile_link']
    )->setAdminInfo(
      $contactCard['contact_card_admin_name'],
      $contactCard['contact_card_admin_title'],
      $contactCard['contact_card_admin_email'],
      $contactCard['contact_card_admin_phone']
    )->setImage(
      $contactCard['contact_card_image']
    )->setLayout('sidebar')
    ->setHasIntro($section->hasIntro());
  }
}

$section->setContent(<<<HTML
  <div class="profile-collection__wrapper" data-cards="$contactCardsCount">
    $contactCardsHtml
  </div>
HTML);

echo $section;
