<?php

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

use Drupal\Core\Render\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Html;

/**
 * Implements hook__preprocess_views_exposed_form().
 * Search modal form.
 */
function toc_wmr_preprocess_views_exposed_form__search_content__page_results(&$vars) {
  // Default was hidden, set to before.
  $vars['form']['search_api_fulltext']['#title_display'] = 'before';
  $vars['form']['search_api_fulltext']['#element_classes'] = 'col-md-8 mb-0 mt-3';
  $vars['form']['search_api_fulltext']["#label_attributes"]['class'][] = 'h2 text-white ffm';

  $vars['form']['actions']['#attributes']['class'][] = 'col-md-4 mt-3';
  $vars['form']['actions']['submit']['#attributes']['class'][] = 'w-100 align-self-end mb-2 ffm';
  $vars['form']['actions']['submit']['#size'] = 'lg';

  // The default function adds the 'addon' for all search boxes, unset that.
  if (!empty($vars['form']['search_api_fulltext']['#input_suffix'])) {
    unset($vars['form']['search_api_fulltext']['#input_suffix']);
  }
}

/**
 * Implements hook__preprocess_views_exposed_form().
 *
 * @throws \Exception
 */
function toc_wmr_preprocess_views_exposed_form__news_blocks__block_3(&$vars) {

  $filters_tabcordion_id = Html::getUniqueId("tabcordion");

  // Create a new submit button for embedding in the Categories filter.
  $first_item = _toc_master_views_filters_tabcordion_tab($vars['form']['field_blog_category_target_id'], $filters_tabcordion_id, '');
  unset($vars['form']['field_blog_category_target_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 ',
    ],
    '#settings' => [
      'pid' => $filters_tabcordion_id,
      'toggle' => 'chevron',
    ],
  ];

  // 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']['#attributes']['class'][] = 'mb-0';
  $vars['form']['actions']['reset']['#status'] = 'info';

  // 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'][] = 'discover-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_wmr_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_wmr_preprocess_fieldset__field_blog_category_target_id(&$vars) {
  toc_master_preprocess_fieldset__checkboxes($vars);
  _toc_wmr_clear_fieldset_radioboxes($vars);
}

/**
 * Implements hook_preprocess_fieldset__FIELD_ID().
 */
function toc_wmr_preprocess_fieldset__field_travel_inspiration_cat_target_id(&$vars) {
  toc_master_preprocess_fieldset__checkboxes($vars);
  _toc_wmr_clear_fieldset_radioboxes($vars);
}

/**
 * Implements hook_preprocess_fieldset__FIELD_ID().
 */
function toc_wmr_preprocess_fieldset__field_of_region_target_id(&$vars) {
  toc_master_preprocess_fieldset__checkboxes($vars);
  _toc_wmr_clear_fieldset_radioboxes($vars);
}

/**
 * Implements hook_preprocess_fieldset__FIELD_ID().
 */
function toc_wmr_preprocess_fieldset__field_of_category_target_id(&$vars) {
  toc_master_preprocess_fieldset__checkboxes($vars);
  _toc_wmr_clear_fieldset_radioboxes($vars);
}

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

/**
 * Implements hook__preprocess_views_exposed_form().
 *
 * @see toc_master_preprocess_views_exposed_form__news_blocks__travel_inspiration().
 */
function toc_wmr_preprocess_views_exposed_form__news_blocks__travel_inspiration(&$vars) {
  $filters_tabcordion_id = Html::getUniqueId("tabcordion");
  $form_original = $vars['form_original'];
  // We are just changing the outline.
  $new_button = $form_original['actions']['submit'];
  $new_button['#outline'] = TRUE;
  $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(
    $form_original['field_travel_inspiration_cat_target_id'],
    $filters_tabcordion_id,
    '',
    $first_button);

  $vars['filters_tabcordion']['#fields']['items'] = [$first_item];
}
