<div class="user-register box">
  <div class="content">
    <div class="box-title">
      <?= \S::lang('zaloz-konto-klienta');?>
    </div>
    <form method="POST" action="/" id="f-user-register">
      <input type="hidden" id="type" name="type" value="0">
      <? if ( $this -> firm_id ):?>
        <input type="hidden" id="firm_id" name="firm_id" value="<?= $this -> firm_id;?>">
      <? endif;?>
      <div class="form-group">
        <input type="email" required id="email" class="form-control" placeholder="<?= ucfirst( \S::lang('email') );?>">
      </div>
      <div class="form-group">
        <input type="password" required id="password" pattern="^\S{6,}$" class="form-control" placeholder="<?= ucfirst( \S::lang('haslo') );?> (<?= \S::lang('min');?> 6 <?= \S::lang('znakow');?>)">
      </div>
      <div class="form-group">
        <input type="password" required id="password-repeat" pattern="^\S{6,}$" class="form-control" oninput="check_match(this)" placeholder="<?= ucfirst( \S::lang('powtorz-haslo') );?>">
      </div>
      <div class="form-group agreement">
        <input type="checkbox" required class="checkbox" name="agreement" id="agreement">
        <label for="agreement">
          <?
            $text = str_replace( '[LINK-REGULAMIN]', '<a href="/regulamin">' . \S::lang('regulamin') . '</a>', \S::lang('rejestracja-akceptacja-regulaminu') );
            echo $text = str_replace( '[LINK-POLITYKA-PRYWATNOSCI]', '<a href="/polityka-prywatnosci">' . \S::lang('polityke-prywatnosci') . '</a>', $text );
          ?>
        </label>
      </div>
      <div class="form-group agreement-marketing">
        <input type="checkbox" name="agreement-marketing" class="checkbox" id="agreement-marketing"> <label for="agreement-marketing"><?= \S::lang('rejestracja-zgoda-na-marketing');?></label>
      </div>
      <div class="form-group buttons">
        <button type="submit" class="btn btn-success"><?= ucfirst( \S::lang('zarejestruj-sie') );?></button>
        <span id="login"><?= ucfirst( \S::lang('masz-juz-konto') );?>? <a href="/logowanie"><?= ucfirst( \S::lang('zaloguj-sie') );?></a></span>
      </div>
    </form>
  </div>
</div>
<script class="footer" type='text/javascript'>
  $( function()
  {
    $( 'body' ).on( 'submit', '#f-user-register', function(e)
    {
      e.preventDefault();

      if ( $( '#f-user-register #agremment-marketing' ).is( ':checked' ) )
        var agreement_marketing = true;
      else
        var agreement_marketing = false;

      $.ajax(
      {
        type: 'POST',
        cache: false,
        url: '/shopClient/signup',
        data:
        {
          email: $( '#f-user-register #email' ).val(),
          password: $( '#f-user-register #password' ).val(),
          agremment_marketing: agreement_marketing
        },
        beforeSend: function()
        {
          alert1 = $.alert(
          {
            title: '<?= ucfirst( \S::lang('informacja') );?>',
            content: '<?= \S::lang('prosze-czekac');?>',
            type: 'orange',
            closeIcon: true,
            closeIconClass: 'fas fa-close',
            typeAnimated: true,
            animation: 'opacity',
            useBootstrap: false,
            theme: 'modern',
            icon: 'fas fa-info',
            buttons:
            {
              confirm:
              {
                text: '<?= \S::lang('zamknij');?>',
                btnClass: 'btn-orange',
                keys: ['enter'],
                action: function() {}
              }
            }
          });
        },
        success: function( data )
        {
          alert1.close();
          response = jQuery.parseJSON( data );

          $.alert(
          {
            title: '<?= ucfirst( \S::lang('informacja') );?>',
            content: response.msg,
            type: 'orange',
            closeIcon: true,
            closeIconClass: 'fas fa-close',
            typeAnimated: true,
            animation: 'opacity',
            useBootstrap: false,
            theme: 'modern',
            autoClose: 'confirm|10000',
            icon: 'fas fa-info',
            buttons:
            {
              confirm:
              {
                text: '<?= \S::lang('zamknij');?>',
                btnClass: 'btn-orange',
                keys: ['enter'],
                action: function() {}
              }
            },
            onClose: function ()
            {
              if ( response.status === 'ok' )
                document.location.href = '/logowanie';
            }
          });
        }
      });

      return false;
    });
  });

  function check_match(input)
  {
    if ( input.value !== document.getElementById( 'password' ).value )
      input.setCustomValidity( '<?= \S::lang('podane-hasla-sa-rozne');?>');
    else
      input.setCustomValidity('');
  }
</script>
