<? global $lang;?>
<div id="newsletter">
  <div class="title"><?= ucfirst( $lang['newsletter'] );?></div>
  <div id="text"><?= $lang['newsletter-text'];?></div>
  <form method="POST" data-toggle="validator" role="form" id="newsletter-form">
    <div class="form-group input">
      <input type='email' id="email" name="email" class="form-control" required placeholder="<?= ucfirst( $lang['email'] );?>">
      <a href="#" class="btn btn-danger" id="newsletter-signin"><span class="text">Zapisz się</span></a>
    </div>
    <div class="form-group agreement">
      <input type="checkbox" id="agreement" /> <span id="first" class="bold"><?= $lang['newsletter-zgoda-1'];?></span> <span id="second" class="hidden"><?= $lang['newsletter-zgoda-2'];?></span><a href="#" id="toggle-data"><?= $lang['rozwin'];?></a>
    </div>
  </form>
</div>
<script class="footer" type="text/javascript">
  $( function()
  {
    $( 'body' ).on( click_event, '#newsletter-form #toggle-data', function()
    {
      var second = $( this ).parents( '.form-group' ).children( '#second' );

      if ( second.hasClass( 'hidden' ) )
      {
        second.removeClass( 'hidden' );
        $( this ).html( '<?= $lang['zwin'];?>' );
      }
      else
      {
        second.addClass( 'hidden' );
        $( this ).html( '<?= $lang['rozwin'];?>' );
      }
      return false;
    });

    $( 'body' ).on( click_event, '#newsletter-form #newsletter-signin', function(e)
    {
      e.preventDefault();
      if ( !$( '#newsletter-form #agreement' ).is( ':checked' ) )
      {
        $.alert(
        {
          title: '<?= ucfirst( $lang['blad'] );?>',
          content: '<?= $lang['newsletter-zgoda-komunikat'];?>',
          type: 'blue',
          closeIcon: true,
          closeIconClass: 'fas fa-times',
          typeAnimated: true,
          animation: 'opacity',
          useBootstrap: false,
          theme: 'modern',
          autoClose: 'confirm|10000',
          buttons:
          {
            confirm:
            {
              text: '<?= $lang['zamknij'];?>',
              btnClass: 'btn-blue',
              keys: ['enter'],
              action: function() {}
            }
          }
        });
        return false;
      }

      var email   = $.trim( $( '#newsletter-form #email' ).val() );
      var token   = $.trim( $( '#newsletter-form #token' ).val() );

      if ( email === '' )
      {
        $.alert(
        {
          title: '<?= ucfirst( $lang['blad'] );?>',
          content: '<?= $lang['newsletter-prosze-uzupelnic-email'];?>',
          type: 'orange',
          closeIcon: true,
          closeIconClass: 'fas fa-times',
          typeAnimated: true,
          animation: 'opacity',
          useBootstrap: false,
          theme: 'modern',
          autoClose: 'confirm|10000',
          buttons:
          {
            confirm:
            {
              text: '<?= $lang['zamknij'];?>',
              btnClass: 'btn-blue',
              keys: ['enter'],
              action: function() {}
            }
          }
        });
        return false;
      }

      $.ajax(
      {
        type: 'POST',
        cache: false,
        url: '/newsletter/signin',
        data:
        {
          email: email
        },
        beforeSend: function()
        {
          alert1 = $.alert(
          {
            title: '<?= $lang['informacja'];?>',
            content: '<?= $lang['prosze-czekac'];?>',
            type: 'orange',
            closeIcon: true,
            closeIconClass: 'fas fa-times',
            typeAnimated: true,
            animation: 'opacity',
            useBootstrap: false,
            theme: 'modern',
            buttons:
            {
              confirm:
              {
                text: '<?= $lang['zamknij'];?>',
                btnClass: 'btn-blue',
                keys: ['enter'],
                action: function() {}
              }
            }
          });
        },
        success: function( data )
        {
          response = jQuery.parseJSON( data );

          $( '.jqibox' ).remove();

          if ( response.status === 'ok' )
            var msg = '<?= $lang['newsletter-potwierdzenie-dodania'];?>';
          else
            var msg = '<?= $lang['newsletter-potwierdzenie-dodania-blad'];?>';

          alert1.close();

          $.alert(
          {
            title: '<?= $lang['informacja'];?>',
            content: msg,
            type: 'orange',
            closeIcon: true,
            closeIconClass: 'fas fa-times',
            typeAnimated: true,
            animation: 'opacity',
            useBootstrap: false,
            theme: 'modern',
            autoClose: 'confirm|10000',
            buttons:
            {
              confirm:
              {
                text: '<?= $lang['zamknij'];?>',
                btnClass: 'btn-blue',
                keys: ['enter'],
                action: function() {}
              }
            }
          });
        }
      });
    });
  });
</script>