<?php

$post_id = get_the_ID();

$wrapper_attributes = apply_filters(
  "inoby_accommodation_card_wrapper_attributes",
  [
    "class" => "masonry-item",
    "wpbs-post-id" => get_the_ID(),
  ],
  $post_id
);

$default_price = wpbs_get_calendar_meta(wpbs_get_current_post_calendar_id(), "default_price", true);
$facilities = rwmb_meta("accommodation_facilities", "", $wrapper_attributes["wpbs-post-id"]);
$accommodation_default_price_before = rwmb_meta("accommodation_default_price_before", "", $wrapper_attributes["wpbs-post-id"]);
$accommodation_default_price_after = rwmb_meta("accommodation_default_price_after", "", $wrapper_attributes["wpbs-post-id"]);
$accommodation_main_color = rwmb_meta("accommodation_main_color", "", $wrapper_attributes["wpbs-post-id"]);
$label = rwmb_meta("accommodation_label", "", $wrapper_attributes["wpbs-post-id"]);
if (empty($accommodation_default_price_after)) {
  $accommodation_default_price_after = " za objekt";
}

$images = rwmb_meta("accommodation_banner_img", ["limit" => 1, "size" => "card"], get_the_ID());
$image = reset($images);

$attrs = join(
  " ",
  array_map(function ($key) use ($wrapper_attributes) {
    if (is_bool($wrapper_attributes[$key])) {
      return $wrapper_attributes[$key] ? $key : "";
    }
    return $key . '="' . $wrapper_attributes[$key] . '"';
  }, array_keys($wrapper_attributes))
);
?>

<div <?= $attrs ?>>
  <a href="<?php the_permalink(); ?>">
    <div class="card"
      style="box-shadow: 0 -5px 0 <?php if (!empty($accommodation_main_color)) {
        echo $accommodation_main_color;
      } else {
        echo "transparent";
      } ?>;">
      <div class="image"
        style="background: url('<?= $image["url"] ?? "" ?>'); background-size: cover; background-position: center;">
        <?php if (!empty($label)) {
          echo '<div class="label">' . $label . "</div>";
        } ?>
        <div class="not-avaliable">
          <?= __("V tomto termíne už obsadené", "inoby") ?>
        </div>
        <?php do_action("inoby_accommodation_card_before_image", $post_id); ?>
      </div>
      <div class="content">
        <div class="content-top">
          <h3><?php the_title(); ?></h3>
          <?php the_excerpt(); ?>
        </div>
        <div class="content-bottom">
          <?php
          if (!empty($facilities)) {
            echo '<div class="facilities-wrap">';
            foreach ($facilities as $facility) {
              if ($facility["accommodation_facilities_featured"] ?? false) {
                echo '<div class="facility-item">';
                echo wp_get_attachment_image($facility["accommodation_facilities_icon"][0], "icon");
                echo $facility["accommodation_facilities_text"];
                echo "</div>";
              }
            }
            echo "</div>";
          }

          if (!empty($default_price)) {
            echo '<div class="content-footer">';
            echo '<div class="default-price">' .
              "$accommodation_default_price_before $default_price " .
              get_woocommerce_currency_symbol() .
              " <span>" .
              $accommodation_default_price_after .
              "</span></div>";
            echo '<div class="button">' . __("Zobraziť viac", "inoby") . "</div>";
            echo "</div>";
          } else {
            echo '<div class="button">' . __("Zobraziť viac", "inoby") . "</div>";
          }
          ?>
        </div>
      </div>
    </div>
  </a>
</div>