<?php

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

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

/**
 * Implements hook_form_views_exposed_form_alter().
 */
function toc_emr_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {

}

/**
 * Implements hook_form_views_exposed_form_alter().
 */
function _toc_emr_form_views_exposed_form_alter__destinations__listing(&$form, FormStateInterface $form_state, $form_id) {
  $form["#attributes"]['class'] = ['pu-4'];
}

/**
 * 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'] = 'default';
  $vars['form']['actions']['reset']['#outline'] = TRUE;
  $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'][] = 'mt-0 me-0';
  $new_button['#value'] = 'Filter by these categories';
  $new_button['#status'] = 'default';
  $new_button['#outline'] = TRUE;
  $new_button['#id'] = _toc_rndid_ifempty($new_button, '#id');
  $first_button = \Drupal::service('renderer')->render($new_button);
  // Create the first tab for our tabcordion.
  $prefix = "<div class='row'><div class='col-md-8'>";
  $suffix = "</div><div class='col-md-4'>$first_button</div></div>";
  $first_item = _toc_master_views_filters_tabcordion_tab($vars['form']['field_dest_er_category_target_id'], $filters_tabcordion_id, $prefix, $suffix);
  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']['#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';
}

/**
 * Implements hook__preprocess_views_exposed_form().
 * Search modal form.
 */
function toc_emr_preprocess_views_exposed_form__search_content__page_results(&$vars) {

  // Default was hidden, set to before.
  $vars['form']['search_api_fulltext']['#title_display'] = 'invisible';
  $vars['form']['search_api_fulltext']['#element_classes'] = 'col-md-8 mb-0 mt-3 me-2';
  $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']);
  }
}
