<?php
$cfg = Inoby_Config::component("posts-slider");
$post_types = $cfg["post_types"] ?? ["post"];

class Posts_Slider extends MBComponentBase {
  private $_pt = "post";

  public function __construct($class_name = "") {
    $this->_pt = substr($class_name, strlen(self::class) + 1);
    parent::__construct();
  }

  protected function init_options($options) {
    $options->enabled = $this->_pt ? true : false;
    if (!$options->enabled) {
      return;
    }
    $labels = get_post_type_object($this->_pt)->labels;
    $plural = $labels->name;
    $add_label = $labels->add_new_item;

    $options->id = "inoby-posts-slider-" . $this->_pt;
    $options->title = "Slider - $plural";
    $options->icon = "slides";
    $options->render_template = get_theme_file_path("components/posts-slider/template.php");
    $options->fields = [
      [
        "id" => "post_type",
        "type" => "hidden",
        "std" => $this->_pt,
      ],
      [
        "type" => "checkbox_list",
        "id" => "options",
        "name" => __("Nastavenia slidera", "inoby"),
        "options" => [
          "infinite" => "Nekonečný slider",
          "auto" => "Automatický slider",
          "arrows" => "Zobraziť navigačné šípky",
          "dots" => "Zobraziť navigačné bodky",
        ],
        "std" => ["infinite", "arrows"],
      ],
      [
        "group_title" => "{post}",
        "id" => "posts",
        "type" => "group",
        "clone" => true,
        "collapsible" => true,
        "sort_clone" => true,
        "add_button" => $add_label,
        "fields" => [
          [
            "id" => "post",
            "title" => $add_label,
            "type" => "post",
            "post_type" => $this->_pt,
            "field_type" => "select_advanced",
            "placeholder" => $add_label,
            "query_args" => [
              "post_status" => "publish",
              "posts_per_page" => -1,
            ],
            "ajax" => true,
          ],
        ],
      ],
    ];
  }
}

// toto vygeneruje slider pre kazdu zadanu post type
foreach ($post_types as $pt) {
  class_alias(Posts_Slider::class, Posts_Slider::class . "_$pt");
}
