<?php

/**
 * Frontend output for the AUX Split Feature component
 * AUX documentation: https://aggieux.tamu.edu/?path=/docs/components-split-feature-about-split-features--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 Alejandro Viejo
 * @since 1.0.0
 */


use WP_AUX\Entities\AuxFramework\Partials\ImageEntity;
use WP_AUX\Entities\AuxFramework\Partials\CardEntity;

// Set variables to null
$split_feature_file =
$split_feature_card_layout =
$section_cards_media =
$section_cards_cta_style =
$the_cards =
$split_feature_card = null;

// Set component variables from user input
$splitFeatureBackgroundImage = @get_field('split_feature_file')['url'];
$card_slab_cards_type = get_field('card_slab_cards_type');
$split_feature_cards = get_field('split_feature_card');
$card_cta_type = aux_get_field('card_slab_cta_type');

// Sets cta classes based on user input
$section_cards_cta_style = get_field('section_cards_cta_style');
if ($section_cards_cta_style == 'links') {
  $splitFeatureCtaClass = 'link--cta-leading';
} elseif($section_cards_cta_style == 'buttons') {
  $splitFeatureCtaClass = 'btn btn--primary';
}

/////////////////// Setting the cards ///////////////////
// Setting featured cards
if ('featured-cards' === $card_slab_cards_type) {
  $cards = aux_get_field('split_feature_card');

  foreach ($cards as $card) {
    // Setting the featured card
    $cardEntity = CardEntity::create($card['card_heading'], $card['card_content'])
      ->setType('standard')
      ->setSuperheader($card['card_superheader'])
      ->setSubheader($card['card_subheader'])
      ->setIcon($card['card_icon'])
      ->setImage($card['card_image'])
      ->setHasIntro(true)
      ->setCtaType($card_cta_type)
      ->setCta($card['card_slab_cta_link'])
      ->setIsFeature(true)
      ->setHeadingLevel(2);

    $the_cards .= $cardEntity;
  }
}

// Setting linked cards
if ('linked-cards' === $card_slab_cards_type) {
  $cards = aux_get_field('split_feature_card');
  foreach ($cards as $card) {
    // Setting the featured card
    $cardEntity = CardEntity::create($card['card_heading'], $card['card_content'])
      ->setType('linked')
      ->setSuperheader($card['card_superheader'])
      ->setSubheader($card['card_subheader'])
      ->setIcon($card['card_icon'])
      ->setImage($card['card_image'])
      ->setHasIntro(true)
      ->setCtaType('link')
      ->setCta($card['card_slab_cta_link']);

    $the_cards .= $cardEntity;
  }
}

//Outputs split feature
echo <<<HTML
<div
  class="split-feature"
  style="background-image:url($splitFeatureBackgroundImage)"
>
  <div class="split-feature__container">
    $the_cards
  </div>
</div>
HTML;
