
export class EventsUtils{
  static initEvents () {
    // event registration
    //TODO: entscheidung über dateicker treffen
    // $('.contact-form, .all-forms').find('.datepicker').datepicker({
    //   dateFormat: 'dd.mm.yy',
    //   changeYear: true,
    //   yearRange: "-100:+0"
    // });

    // event search filter
    var $evSearchFilter = $('.ev-search-filter'),
      $extendBtn = $evSearchFilter.find('.extend'),
      $extendText = $extendBtn.find('a'),
      $extendedSearch = $evSearchFilter.find('.extended-search'),
      $searchBtn = $evSearchFilter.find('.btn-wrapper'),
      // variables for reset
      $inputTypeNumber = $evSearchFilter.find('input[type="number"]'),
      $inputTypeText = $evSearchFilter.find('input[type="text"]'),
      $inputTypeSelect = $evSearchFilter.find('select');

    $extendBtn.on('click', function (e) {
      e.preventDefault();
      $extendedSearch.fadeToggle(200).attr('aria-hidden', function (i, attr) {
        return (attr === "true") ? 'false' : 'true';
      });
      /* data-detail-search-label and data-simple-search-label
      in source/_patterns/02-organisms/13-event-search/00-event-search-main.mustache */
      if ($extendText.text() === $evSearchFilter.data("detail-search-label")) {
        $extendText.text($evSearchFilter.data("simple-search-label"));
        $('#nurKostenfreie').focus();
      } else if ($extendText.text() === $evSearchFilter.data("simple-search-label")) {
        $extendText.text($evSearchFilter.data("detail-search-label"));
        $extendBtn.focus();
        $('html, body').animate({
          scrollTop: ($("#ev-search-filter-form").offset().top - 200)
        }, 500);
      }
      $searchBtn.toggle();
    });
    // TODO: datepicker
    // $evSearchFilter.find('.datepicker').datepicker({dateFormat: 'dd.mm.yy', changeMonth: false,
    //   changeYear: false});
    $evSearchFilter.on('click keydown', '.ev-search-btn, label.acc', function (e) {
      //if (!checkOnKeyDown(e)) return;
      $(this).toggleClass('active');

      if ($(e.target.parentElement).is('label')) {
        $(this).find('a').attr('aria-pressed', function (index, attr) {
          return (attr === "true") ? "false" : "true";
        }).parent().next('.ev-filter').attr('aria-hidden', function (index, attr) {
          return (attr === "true") ? "false" : "true";
        }).slideToggle(200);
      } else {
        $(this).attr('aria-pressed', function (index, attr) {
          return (attr === "true") ? "false" : "true";
        }).next('.ev-filter').attr('aria-hidden', function (index, attr) {
          return (attr === "true") ? "false" : "true";
        }).slideToggle(200);
      }

      $evSearchFilter.find('.extend');
      return false;
    });
    $evSearchFilter.on('click', '.reset', function (event) {
      event.preventDefault();
      const form = $(this).closest('form');

      form[0].reset();//.trigger('reset');
      form.find('.half-checked').removeClass('half-checked');

      $('html, body').animate({
        scrollTop: form.offset().top - 120
        //scrollTop: ($("#search-filter-form").offset().top - 80)
      }, 500);

      return;
      /*
      //Eingabefelder leeren
      $evSearchFilter.find('input:checkbox').removeAttr('checked');
      // todo: datepicker
      // $evSearchFilter.find('.datepicker').datepicker("setDate", "");
      $inputTypeNumber.val('');
      $inputTypeText.val('');
      $inputTypeSelect.val('');

      //Vorbelegungen wiederherstellen
      $inputTypeNumber.filter('[data-initialvalue]').each(function(){$(this).val($(this).attr('data-initialvalue'));});
      $inputTypeText.filter('[data-initialvalue]').each(function(){$(this).val($(this).attr('data-initialvalue'));});
      $inputTypeSelect.filter('[data-initialvalue]').each(function(){$(this).val($(this).attr('data-initialvalue'));});

      $('.ev-search-filter .ev-filter').attr("aria-hidden", "true").slideUp(200);
      $(".ev-search-filter .ev-search-btn, .ev-search-filter label.acc").attr("aria-pressed", "false").removeClass("active");
      $('html, body').animate({
        //scrollTop: ($("#search-filter-form").offset().top - 80)
      }, 500);
      */
    });
    var $abb;
    $evSearchFilter.on('click', 'input.acc', function () {
      $abb = $(this);
      $abb.nextAll('.ev-filter').find('input[type="checkbox"]').prop('checked', this.checked);
    });

    $evSearchFilter.on('click', 'input.abb', function(){
      $abb.prop('checked', false);
    });


    // more participants
    var $moreParticpants = $('.more-participants'),
      $formParticipants = $('.form-participants');
    $('#cf-participant').on('change', function (e) {
      $formParticipants.empty();
      var value = parseInt(e.target.value);
      if (value >= 1) {
        for (var i = 0; i < value; i++) {
          $moreParticpants.clone().each(function () {
            var $element = $(this);
            $element.find('input, select').each(function () {
              var $this = $(this);
              var id = $this.attr('id') + i;
              $this.attr('id', id);
              var name = $this.attr('name') + i;
              $this.attr('name', name);
              $this.closest('.input-wrapper').find('label').attr('for', id);
            });
          }).fadeIn(400).appendTo($formParticipants);
        }
      }
    });
    // billing information
    $('#cf-billing-address').on('change', function (e) {
      var value = parseInt(e.target.value),
        $billingAddress = $('.billing-address'),
        $billingInformation = $('.billing-information');
      if (value === 2) {
        $billingInformation.clone().fadeIn(400).appendTo($billingAddress);
      } else {
        $billingAddress.find('.billing-information').fadeOut(200, function () {
          $billingAddress.empty();
        });
      }
    });

    //msc: IHK-2653
    var expandedlist = $('[data-expanded-on-init="true"]');
    if (expandedlist && expandedlist.length > 0) {
      expandedlist.prev().toggleClass('active');
      expandedlist.toggle();
    }

    if($('[data-show-extended-search="true"]').length > 0) {
      $('.ev-search-filter').find('.extend').click();
    }
  };
}
