<?php
$all_cookie_categories = isset($args["all_cookie_categories"]) ? $args["all_cookie_categories"] : [];

$base_category = RC_Cookie_Consent::get_base_cookie_category();

$cookie_types = rwmb_meta("cookie-types", ["object_type" => "setting"], "options");
$cookie_sections = array_reduce(
  $cookie_types,
  function ($sections, $type) {
    $sections[$type["cookie-type"]] = [
      "title" => $type["cookie-type-title"]??"",
      "desc" => $type["cookie-type-desc"]??"",
    ];
    return $sections;
  },
  []
);
$title = rwmb_meta("cookies-settings-title", ["object_type" => "setting"], "options");
?>

<form id="cookie-settings" class="rc-form" data-settings=<?= json_encode(["customSubmit" => true]) ?>>
  <h3 class="title"><?= $title ?></h3>
  <div class="sections">
    <?php foreach ($all_cookie_categories as $category): ?>
    <?php $section = $cookie_sections[$category]; ?>
    <?php if (!empty($section)): ?>
    <div class="section checkbox">
      <?php
      $cookie_name = RC_Cookie_Consent::$cookie_prefix . $category;

      if ($category !== $base_category) {
        $disabled = "";
        $cookie_val = isset($_COOKIE[$cookie_name]) ? $_COOKIE[$cookie_name] : "0";
        $checked = $cookie_val ? "checked" : "";
      } else {
        $disabled = "disabled=true";
        $checked = "checked";
        $cookie_val = 1;
      }
      ?>
      <input id="cookie-category-<?= $category ?>" type="checkbox" class="validate" name="<?= $category ?>"
        <?= "$disabled $checked" ?> />
      <h5 class="title"><?= $section["title"] ?></h5>
      <div class="arrow"></div>
      <div class="desc">
        <p><?= $section["desc"] ?></p>
      </div>
    </div>
    <input type="hidden" name="<?= $category ?>" value="<?= $cookie_val ?>" />
    <?php endif; ?>
    <?php endforeach; ?>
    <input type="hidden" class="checkbox-buttons" />
  </div>
</form>