<?php
/**
 * Split Text Block Template.
 *
 * @param array      $block      The block settings and attributes.
 * @param string     $content    The block inner HTML (empty).
 * @param bool       $is_preview True during AJAX preview.
 * @param int|string $post_id    The post ID this block is saved to.
 */

$is_preview = !empty($is_preview);

$id = 'muy-split-text-' . $block['id'];
if (!empty($block['anchor'])) {
  $id = $block['anchor'];
}

$className = 'muy-split-text';
if (!empty($block['className'])) {
  $className .= ' ' . $block['className'];
}
if (!empty($block['align'])) {
  $className .= ' align' . $block['align'];
}

$block_data = (isset($block['data']) && is_array($block['data'])) ? $block['data'] : [];

$title = get_field('split_text_title');
if ($title === null || $title === false || $title === '') {
  $title = $block_data['split_text_title'] ?? $block_data['field_6893a1b2c4d5f'] ?? '';
}

$subtitle = get_field('split_text_subtitle');
if ($subtitle === null || $subtitle === false || $subtitle === '') {
  $subtitle = $block_data['split_text_subtitle'] ?? $block_data['field_6893a1b2c4d60'] ?? '';
}

$ratio = get_field('split_text_ratio');
if (empty($ratio)) {
  $ratio = $block_data['split_text_ratio'] ?? $block_data['field_6893a1b2c4d61'] ?? '30-70';
}

$ratios = [
  '30-70' => ['30%', '70%'],
  '70-30' => ['70%', '30%'],
  '50-50' => ['50%', '50%'],
];

if (!isset($ratios[$ratio])) {
  $ratio = '30-70';
}

$left_width = $ratios[$ratio][0];
$right_width = $ratios[$ratio][1];

$className .= ' is-ratio-' . $ratio;
?>

<div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>" style="--split-left: <?php echo esc_attr($left_width); ?>; --split-right: <?php echo esc_attr($right_width); ?>;">
  <div class="split-text-inner">
    <div class="split-text-meta">
      <?php if ($title) : ?>
        <h2 class="split-text-title"><?php echo esc_html($title); ?></h2>
      <?php elseif ($is_preview) : ?>
        <h2 class="split-text-title is-placeholder"><?php echo esc_html__('Titolo', 'muy-lindo'); ?></h2>
      <?php endif; ?>

      <?php if ($subtitle) : ?>
        <p class="split-text-subtitle"><?php echo wp_kses_post($subtitle); ?></p>
      <?php elseif ($is_preview) : ?>
        <p class="split-text-subtitle is-placeholder"><?php echo esc_html__('Sottotitolo', 'muy-lindo'); ?></p>
      <?php endif; ?>
    </div>
    <div class="split-text-content">
      <InnerBlocks />
    </div>
  </div>
</div>
