<?php

/**
 * @file
 * Theming for individual forms by ID.
 */

use Drupal\Component\Utility\Html;

/**
 * Implements hook__preprocess_views_exposed_form().
 */
function toc_emr_preprocess_views_exposed_form__destinations__listing(&$vars) {
  $filters_tabcordion_id = Html::getUniqueId("tabcordion");
  // Preserve form for sub theme alters.
  $vars['form_original'] = $vars['form'];
  // Move Value to Text which is rendered with text|raw.
  $vars['form']['actions']['reset']['#text'] = '<i class="fas fa-times-circle me-2"></i>' . $vars['form']['actions']['reset']['#value'];
  $vars['form']['actions']['reset']['#size'] = 'sm';
  $vars['form']['actions']['reset']['#status'] = 'info';
  $vars['form']['actions']['reset']['#attributes']['class'][] = 'mb-0';

  // Create a new submit button for embedding in the Categories filter.
  $new_button = $vars['form']['actions']['submit'];
  $new_button['#attributes']['class'][] = 'mb-0 mt-3';
  $new_button['#value'] = 'Filter by these categories';
  $new_button['#id'] = _toc_rndid_ifempty($new_button, '#id');
  $first_button = \Drupal::service('renderer')->render($new_button);
  // Create the first tab for our tabcordion.
  $first_item = _toc_master_views_filters_tabcordion_tab($vars['form']['field_dest_er_category_target_id'], $filters_tabcordion_id, '', $first_button);
  unset($vars['form']['field_dest_er_category_target_id']);

  // Create a new reset in the tabcordion.
  $new_reset = $vars['form']['actions']['reset'];
  $new_reset['#id'] = _toc_rndid_ifempty($new_reset, '#id');
  // Create the tabcordion component.
  $vars['filters_tabcordion'] = [
    '#type' => 'pattern',
    '#id' => 'bs-accordion-items',
    '#variant' => 'aci_c',
    '#fields' => [
      'items' => [
        $first_item,
      ],
      'list_prefix' => '<i class="fas fa-sliders-h"></i> Showing ',
      'list_suffix' => $new_reset,
    ],
    '#settings' => [
      'pid' => $filters_tabcordion_id,
      'toggle' => 'chevron',
    ],
  ];

  // Finally, we are using some custom ajax submit triggers, so we need
  // a submit who has an attribute that will not change on ajax reload,
  // so that we can trigger it.
  $vars['form']['actions']['submit']['#status'] = 'info';
  $vars['form']['actions']['submit']['#attributes']['class'][] = 'destinations-ajax-submit-btn';
}

/**
 * Implements hook_preprocess_views_exposed_form().
 *
 * This view is also post-rendering it's form and limiting results using
 * _tm_views_limit_filter_options();
 *
 * @see _toc_master_views_post_render__offers_frontend_views__block_1()
 */
function toc_emr_preprocess_views_exposed_form__offers_frontend_views__block_1(&$vars) {
  unset($vars['form']['field_of_type_target_id']);
}

/**
 * Implements hook_preprocess_fieldset__FIELD_ID().
 */
function toc_emr_preprocess_fieldset__field_dest_er_category_target_id(&$vars) {
  toc_master_preprocess_fieldset__checkboxes($vars);
  _toc_emr_clear_fieldset_radioboxes($vars);
}

/**
 * Implements hook_preprocess_fieldset__FIELD_ID().
 */
function toc_emr_preprocess_fieldset__field_travel_inspiration_cat_target_id(&$vars) {
  toc_master_preprocess_fieldset__checkboxes($vars);
  _toc_emr_clear_fieldset_radioboxes($vars);
}

/**
 * Helper function. Hide the tags and margin/padding for fieldsets in filters.
 *
 * @param $vars
 *
 * @return void
 */
function _toc_emr_clear_fieldset_radioboxes(&$vars) {
  $vars['attributes']['class'][] = 'no-shd';
  $vars['attributes']['class'][] = 'no-tags';
  $vars['attributes']['class'][] = 'mb-0';
}
