<?php

$cfg = Inoby_Config::component("img-with-text");

//IMG BLOCK
$image = mb_get_block_field("img");
$img_alt_text = mb_get_block_field("img-alt-text");
$img_position = mb_get_block_field("img-position");
$img_col = (mb_get_block_field("img-col") ?: $cfg["img-col"]) / 10 + 1;
$space_col = $cfg["space-col"];

//TEXT BLOCK
$text = mb_get_block_field("text");

//BUTTON BLOCK
$btn_text = mb_get_block_field("btn-text");
$btn_url = mb_get_block_field("btn-url");

$text_col = max(12 - $img_col - $space_col, 1);
$id = uniqid("inoby-img-with-text");

if ($image): ?>
  <div <?= inoby_block_attrs($attributes, [
    "id" => $id,
    "class" => "inoby-img-with-text",
  ]) ?>>
    <style>
      <?= "#{$id}" ?> .img-with-text-wrap {
        --img-with-text-img-col:<?= $img_col ?>;
        --img-with-text-text-col:<?= $text_col ?>;
      }
    </style>
    <div class="container">
      <div class="row <?php if ($img_position == "left") {
        echo "left";
      } else {
        echo "right";
      } ?>">
        <div class="col-12">
          <div class="img-with-text-wrap <?php if ($img_position == "left") {
            echo "left";
          } else {
            echo "right";
          } ?>">
            <div class="img-wrap">
              <?= isset($image)
                ? mb_inoby_picture(
                  $image,
                  $cfg["img-size"] ?? "v-4",
                  $cfg["bp-sizes"] ?? [],
                )
                : "" ?>
            </div>
            <div class="content-wrap">
              <div class="text-wrap">
                <?php echo isset($text) ? "<p>" . $text . "</p>" : ""; ?>
              </div>
              <div class="button-wrap">
                <?php if ($btn_url):
                  echo isset($btn_url) || $btn_text
                    ? '<a href="' .
                      $btn_url .
                      '" class="button">' .
                      $btn_text .
                      "</a>"
                    : "";
                endif; ?>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
<?php endif; ?>
