<div class="user-register box">
  <div class="content">
    <div class="box-title">
      <?= \Shared\Helpers\Helpers::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( \Shared\Helpers\Helpers::lang('email') );?>">
      </div>
      <div class="form-group">
        <input type="password" required id="password" pattern="^\S{6,}$" class="form-control" placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang('haslo') );?> (<?= \Shared\Helpers\Helpers::lang('min');?> 6 <?= \Shared\Helpers\Helpers::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( \Shared\Helpers\Helpers::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">' . \Shared\Helpers\Helpers::lang('regulamin') . '</a>', \Shared\Helpers\Helpers::lang('rejestracja-akceptacja-regulaminu') );
            echo $text = str_replace( '[LINK-POLITYKA-PRYWATNOSCI]', '<a href="/polityka-prywatnosci">' . \Shared\Helpers\Helpers::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"><?= \Shared\Helpers\Helpers::lang('rejestracja-zgoda-na-marketing');?></label>
      </div>
      <div class="form-group buttons">
        <button type="submit" class="btn btn-success"><?= ucfirst( \Shared\Helpers\Helpers::lang('zarejestruj-sie') );?></button>
        <span id="login"><?= ucfirst( \Shared\Helpers\Helpers::lang('masz-juz-konto') );?>? <a href="/logowanie"><?= ucfirst( \Shared\Helpers\Helpers::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( \Shared\Helpers\Helpers::lang('informacja') );?>',
            content: '<?= \Shared\Helpers\Helpers::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: '<?= \Shared\Helpers\Helpers::lang('zamknij');?>',
                btnClass: 'btn-orange',
                keys: ['enter'],
                action: function() {}
              }
            }
          });
        },
        success: function( data )
        {
          alert1.close();
          response = jQuery.parseJSON( data );

          $.alert(
          {
            title: '<?= ucfirst( \Shared\Helpers\Helpers::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: '<?= \Shared\Helpers\Helpers::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( '<?= \Shared\Helpers\Helpers::lang('podane-hasla-sa-rozne');?>');
    else
      input.setCustomValidity('');
  }
</script>
