<?php
declare(strict_types=1);

use WP_AUX\Entities\Search\SearchSidebarEntity;
use WP_AUX\Services\AuxFramework\ElementClassesService;

$searchSidebar = SearchSidebarEntity::instance();
$sections = $searchSidebar->getSections();
?>
<div class="filters filters--sidebar show-for-medium">
  <?php echo $searchSidebar->getTitleContent(); ?>
  <form action="<?php echo $searchSidebar->getUrl(); ?>">
    <div class="search search--slim">
      <?php echo $searchSidebar->getHeadingContent(); ?>
      <div class="search__form">
        <label for="sidebar-search" class="sr-only">Search</label>
        <?php echo $searchSidebar->getStringField(); ?>
      </div>
    </div>

    <?php
    foreach ($sections as $section) {
      $fieldsetClasses = ElementClassesService::buildClasses('fieldset');
      $fieldsetClasses->addModifier($section['filter_type']);
      ?>
      <fieldset name="" class="<?php echo $fieldsetClasses; ?>">
        <legend><?php echo $section['heading']; ?></legend>
        <?php
        foreach ($section['filter'] as $field) {
          if (in_array($section['filter_type'], ['checkbox', 'radio'])) {
            foreach ($field['options'] as $optionValue => $option) {
              $inputFieldId = 'search-' . $field['name'] . '-' .$optionValue;

              $inputField = sprintf(
                '<input type="%s" name="%s" value="%s" id="%s"%s>',
                $section['filter_type'],
                $field['input_name'],
                $optionValue,
                $inputFieldId,
                $option['selected'] ? ' checked="checked"' : '',
              );
              ?>
              <div class="fieldset__input">
                <?php echo $inputField; ?>
                <label for="<?php echo $inputFieldId; ?>"><?php echo $option['label']; ?></label>
              </div>
              <?php
            }
          } else {
            $inputFieldId = 'search-' . $field['name'];
            ?>
            <div class="fieldset__input">
              <label class="" for="<?php echo $inputFieldId; ?>"><?php echo $field['label']; ?></label>
              <select name="<?php echo $field['input_name']; ?>" id="<?php echo $inputFieldId; ?>">
                <option value="any"></option>
                <?php
                foreach ($field['options'] as $optionValue => $option) {
                  echo sprintf(
                    '<option value="%s"%s>%s</option>',
                    $optionValue,
                    $option['selected'] ? ' selected="selected"' : '',
                    $option['label']
                  );
                }
                ?>
              </select>
            </div>
            <?php
          }
        }
        ?>
      </fieldset>
      <?php
    }
    ?>
    <div class="filters__submit">
      <?php
      if ($searchSidebar->hasSections()) {
        ?>
        <input type="reset" value="Clear Filters">
        <?php
      }
      ?>
      <input type="submit" value="Submit">
    </div>
  </form>
</div>
