<?php

function inoby_promo_banner()
{
  if (Inoby_Config::promo_banner()) {
    $promo_banners = rwmb_meta("mb-promo-group", ["object_type" => "setting"], "options");

    foreach ($promo_banners as $banner) {
      $code = $banner["mb-promo-code"] ?? "";
      $text = $banner["mb-promo-text"] ?? "";
      $ctatext = $banner["mb-promo-ctatext"] ?? "";
      $ctalink = $banner["mb-promo-ctalink"] ?? "";

      if ($text && $code):
        // toto vykresli template /rootcommerce/global/promo
        if (ESHOP_ENABLED) {
          rc_promo([
            "text" => "<p>" . $text . '</p><a href="' . $ctalink . '" class="btn-link">' . $ctatext . "</a>",
            "duration" => 1,
            "name" => $code,
          ]);
        }
      endif;
    }
  }
}

if (Inoby_Config::promo_banner()) {
  /**
   * META BOXES
   */

  add_filter("rwmb_meta_boxes", function ($meta_boxes) {
    $meta_boxes[] = [
      "id" => "mb-promo",
      "settings_pages" => "options",
      "tab"            => "promo",
      "title" => __("Promo lišta", "inoby"),
      "fields" => [
        [
          "group_title" => __("Promo lišta #{#}", "inoby"),
          "id" => "mb-promo-group",
          "type" => "group",
          "add_button" => __("Pridať Promo Banner", "inoby"),
          "clone" => true,
          "collapsible" => true,
          "sort_clone" => true,
          "default_state" => "expanded",
          "fields" => [
            [
              "id" => "mb-promo-code",
              "name" => esc_html__("Kód", "inoby"),
              "type" => "text",
              "placeholder" => "banner123456",
              "desc" => esc_html__(
                "Používa sa na identifikovanie proma, ktoré návštevník stránky už zatvoril. Po zmene tohoto kódu sa lišta znovu zobrazí všetkým návštevníkom stránky.",
                "rootscope"
              ),
              "required" => true,
            ],
            [
              "id" => "mb-promo-text",
              "name" => esc_html__("Text", "inoby"),
              "type" => "textarea",
              "sanitize_callback" => "none",
            ],
            [
              "id" => "mb-promo-ctatext",
              "name" => esc_html__("CTA tlačidlo - Text", "inoby"),
              "type" => "text",
            ],
            [
              "id" => "mb-promo-ctalink",
              "name" => esc_html__("CTA tlačidlo - Odkaz", "inoby"),
              "type" => "text",
            ],
          ],
        ],
      ],
    ];
    return $meta_boxes;
  });
}
