<?php

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

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

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

  // If the form is being used in a menu - set different classes and attributes.
  if (!empty($vars['form']['#attributes']['data-menu-attribute'])
    && $vars['form']['#attributes']['data-menu-attribute'] === 'mg-menu-search') {

    $vars['form']['search_api_fulltext']['#title'] = 'Search this site';
    $vars['form']['search_api_fulltext']['#title_display'] = 'before';
    $vars['form']['search_api_fulltext']['#element_classes'] = 'col-md-11 d-flex fle-row mb-2';
    $vars['form']['search_api_fulltext']["#label_attributes"]['class'][] = 'no-addon-sep col-md-3 h2 fs-5 text-black ffm d-flex justify-content-start align-items-center mb-0 mt-2';
    $vars['form']['search_api_fulltext']['#attributes']['class'][] = 'py-1 px-3 fs-5 min-h-0';
    $vars['form']['search_api_fulltext']['#wrapper_attributes']['class'][] = 'no-addon-sep';

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

    // The default function adds the 'addon' for all search boxes, unset that.
    if (!empty($vars['form']['search_api_fulltext']['#input_suffix'])) {
      $vars['form']['search_api_fulltext']['#input_suffix'] =
        '<button data-drupal-selector="edit-submit-search-content" class="button js-form-submit form-submit btn btn-default btn-lg mla no-addon-sep" type="submit" id="edit-submit-search-content--2" value="Search" data-persistent-id="edit-search-views-exposed-form-search-content" data-dashlane-label="true" data-dashlane-rid="15ec5ef945af53c8">
    <span class="sr-only">Search</span><i class="fas fa-chevron-right"></i>
  </button>';
    }
  } else {
    // Default was hidden, set to before.
    $vars['form']['search_api_fulltext']['#title_display'] = 'none';
    $vars['form']['search_api_fulltext']['#element_classes'] = 'col-md-8';
    $vars['form']['search_api_fulltext']["#label_attributes"]['class'][] = 'hs ffm';

    $vars['form']['actions']['#attributes']['class'][] = 'col-md-4 d-inline';
    $vars['form']['actions']['submit']['#attributes']['class'][] = 'w-100 align-self-end ffm';
    $vars['form']['actions']['submit']['#size'] = 'lg';
    $vars['form']['actions']['#attributes']['id'] = 'edit-actions-search';

    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) {
  // Revert all the chages from TOC Master, and use our local template.
  $vars['form'] = $vars['form_original'];
  // 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'][] = 'd-none';
}

/**
 * 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];
}

function toc_wmr_form_nre_stations_ldb_live_boards_finder_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (isset($form['callingAt'])) {
    $form['callingAt']['#input_prefix'] = $form['callingAt']['#title'] ?? '';
    $form['callingAt']['#title_display'] = 'invisible';
    $form['callingAt']['#element_classes'] = 'mt-3 solid-background';
  }
  $form['station']['#element_classes'] .= ' solid-background';
  unset($form['station']['#input_suffix']);
  $form['#attributes']['class'][] = 'mb-0 stations-autocomplete';
  if (($key = array_search('visually-hidden', $form['actions']['submit']['#attributes']['class'])) !== FALSE) {
    unset($form['actions']['submit']['#attributes']['class'][$key]);
  }
  $form['actions']['submit']['#attributes']['class'][] = 'w-100 bk-frm-fs py-2 rounded-3';
}
