<? global $lang, $globelus_settings;?>
<? $positions_experience = \front\factory\Globelus::positions_experience();?>
<? if ( is_array( $this -> g_user['categories'] ) and count( $this -> g_user['categories'] ) ):?>
  <div id="main-alert">
    <? foreach ( $this -> g_user['categories'] as $category ):?>
      <?
        $categories_name .= \front\factory\Globelus::category_name( $category );
        if ( $category != end( $this -> g_user['categories'] ) )
          $categories_name .= ', ';
      ?>
    <? endforeach;?>
    <?= str_replace( '[KATEGORIA]', $categories_name, $lang['baza-kandydatow-komunikat'] );?>
  </div>
<? endif;?>
<div class="row">
  <div class="col-12 col-sm-4 col-md-3">
    <div class="advert-filtr">
      <form method="GET" action="/baza-cv/">
        <input type="hidden" name="filtr" value="true">
        <div class="title">Wyszukaj kandydata</div>
        <div class="content">
          <div class="subtitle"><?= ucfirst( $lang['slowo-kluczowe'] );?></div>
          <div class="subcontent" style="display: block;">
            <input type="text" name="k" class="form-control" value="<?= htmlspecialchars( $this -> keyword );?>" placeholder="<?= $lang['wpisz-szukane-slowo-kluczowe'];?>">
          </div>
          <div class="subtitle">Stanowisko</div>
          <div class="subcontent">
            <select class="form-control positions" name="id_position" placeholder="--- wybierz ---">
              <option value="">--- wybierz ---</option>
              <? if ( is_array( $this -> positions ) ): foreach ( $this -> positions as $position_single ):?>
                <option value="<?= $position_single['id']; ?>" <? if ( $this -> position_selected == $position_single['id'] ):?>selected="selected"<? endif;?>><?= htmlspecialchars( $position_single['name'] );?></option>
              <? endforeach; endif;?>
            </select>
          </div>
          <div class="subtitle toggle" content="categories"><?= ucfirst( $lang['kategorie'] );?></div>
          <div class="subcontent" id="subcontent-categories">
            <ul>
              <? if ( is_array( $this -> categories ) ): foreach ( $this -> categories as $category ):?>
                <li <? if ( ++$j > 5 ):?> style="display: none;"<? endif;?>>
                  <input class="checkbox" type="checkbox" name="c2[]" value="<?= $category['id'];?>" <? if ( in_array( $category['id'], $this -> categories_selected ) ):?>checked="checked"<? endif;?>><?= $category['name'];?>
                </li>
              <? endforeach; endif;?>
            </ul>
            <a href="#" class="btn btn-primary show-more" content="subcontent-categories">więcej</a>
          </div>
          <div class="subtitle toggle" content="countries"><?= ucfirst( $lang['kraj'] );?></div>
          <div class="subcontent" id="subcontent-countries">
            <ul>
              <? if ( is_array( $this -> countries ) ): foreach ( $this -> countries as $country ):?>
                <li <? if ( ++$i > 5 ):?> style="display: none;"<? endif;?>>
                  <input type="checkbox" name="c1[]" value="<?= $country['id'];?>" <? if ( in_array( $country['id'], $this -> countries_selected ) ):?>checked="checked"<? endif;?>><?= $country['name'];?>
                </li>
              <? endforeach; endif;?>
            </ul>
            <a href="#" class="btn btn-primary show-more" content="subcontent-countries">więcej</a>
          </div>
          <div class="subtitle toggle" content="voivodeships">Województwo (tylko Polska)</div>
          <div class="subcontent" id="subcontent-voivodeships">
            <ul>
              <? if ( is_array( $this -> voivodeships ) ): foreach ( $this -> voivodeships as $voivodeship ):?>
                <li <? if ( ++$k > 5 ):?> style="display: none;"<? endif;?>>
                  <input class="checkbox" type="checkbox" name="v[]" value="<?= $voivodeship['id'];?>" <? if ( in_array( $voivodeship['id'], $this -> voivodeships_selected ) ):?>checked="checked"<? endif;?>><?= $voivodeship['name'];?>
                </li>
              <? endforeach; endif;?>
            </ul>
            <a href="#" class="btn btn-primary show-more" content="subcontent-voivodeships">więcej</a>
          </div>
          <div class="subtitle toggle" content="availability"><?= ucfirst( $lang['dostepnosc'] );?></div>
          <div class="subcontent" id="subcontent-availability">
            <ul>
              <li>
                <input type="radio" name="a" value="0" <? if ( !$this -> time_selected ):?>checked="checked"<? endif;?>> <?= $lang['wszyscy'];?>
              </li>
              <?
                $status = \front\factory\Globelus::availability_status();
                if ( is_array( $status ) ): foreach ( $status as $key => $val ):?>
                  <li>
                    <input type="radio" name="a" value="<?= $key;?>" <? if ( $this -> availability_selected == $key ):?>checked="checked"<? endif;?>> <?= $val;?>
                  </li>
                <? endforeach; endif;
              ?>
            </ul>
          </div>
        </div>
        <input type="submit" class="btn btn-success pull-right mt10" value="<?= $lang['zatwierdz'];?>">
      </form>
    </div>
  </div>
  <div class="col-12 col-sm-8 col-md-9">
    <? if ( is_array( $this -> cv_list ) and count( $this -> cv_list ) ):?>
      <? foreach ( $this -> cv_list as $cv ):?>
        <div class="cv-entry <? if ( $cv['highlight'] ):?>highlight<? endif;?>">
          <? if ( $cv['highlight'] ):?><div id="highlight-text">Kandydat wyróżniony</div><? endif;?>
          <div class="row">
            <div class="col-12 col-sm-2">
              <div class="img">
                <? if ( file_exists( \front\factory\GlobelusCandidates::avatar_url( $cv['id'] ) ) ):?>
                  <img src="/avatar/<?= $cv['id'];?>/<?= \S::get_token();?>">
                <? else:?>
                  <img src="/images/no-avatar.png">
                <? endif;?>
              </div>
            </div>
            <div class="col-12 col-sm-10">
              <div class="row">
                <div class="col-12 col-sm-8">
                  <h2>
                    <a href="/kandydat/<?= $cv['id'];?>/<?= \S::seo( $cv['surname'] . ' ' . $cv['name'], true );?>/" title="Kandydat: <?= addslashes( $cv['surname'] . ' ' . $cv['name'] );?>"><?= $cv['surname'] . ' ' . $cv['name'];?></a>
                  </h2>
                </div>
                <div class="col-12 col-sm-4 sex-and-birth">
                  <?= $lang['plec'];?>:
                  <?
                    if ( $cv['sex'] == '1' )
                      echo $lang['mezczyzna'];
                    else if ( $cv['sex'] == '0' )
                      echo $lang['kobieta'];
                    else
                      echo '-';
                  ?>
                  <? if ( $cv['date_of_birth'] and $cv['date_of_birth'] != '1970-01-01' ):?>
                    , <?= $lang['wiek'];?>: <?= $age = \S::date_diff( $cv['date_of_birth'], date( 'Y-m-d H:i:s' ), 60 * 60 * 24 * 365.25 );?> <?= \front\factory\Globelus::age_word_translations( $age );?>
                  <? endif;?>
                </div>
              </div>
              <div class="info">
                <?= $lang['tel.'];?> <?= $cv['phone'];?>, <?= $lang['email'];?>: <a href="mailto:<?= $cv['email'];?>"><?= $cv['email'];?></a>
              </div>
              <div class="info-2">
                <?
                  if ( is_array( $cv['positions'] ) and count( $cv['positions'] ) ):
                    echo '<p class="mb0">Chętnie podejmę pracę na stanowisku:<br />';
                    foreach ( $cv['positions'] as $position ):
                      echo ' ● <strong>'; if ( !$position['id_position'] ) echo $position['position']; if ( $position['id_position'] ) echo \front\factory\Globelus::position_name( $position['id_position'] ); echo '</strong>'; if ( $position['experience'] ) echo ' - ' . $lang['moj-staz-na-tym-stanowisku'] . ' ' . $positions_experience[ $position['experience'] ];
                      if ( $position != end( $cv['positions'] ) )
                        echo '<br />';
                    endforeach;
                    echo '</p>';
                  endif;
                ?>
              </div>
              <div class="info-2">
                <?= $cv['sex'] ? $lang['ostatnio-szukal-pracy'] : $lang['ostatnio-szukala-pracy'];?>: <strong><?= $cv['last_refresh'] ? date( 'Y-m-d', strtotime( $cv['last_refresh'] ) ) : date( 'Y-m-d', strtotime( $cv['register_date'] ) );?></strong>
              </div>
              <div class="short-text">
                <?= \S::clear_advert_text( $advert['short_text'] );?>
              </div>
              <a href="/kandydat/<?= $cv['id'];?>/<?= \S::seo( $cv['surname'] . ' ' . $cv['name'], true );?>/" class="btn btn-success"><?= $lang['profil-kandydata'];?></a>
            </div>
          </div>
        </div>
      <? endforeach;?>
    <? else:?>
      <? if ( !is_array( $this -> g_user['categories'] ) or !count( $this -> g_user['categories'] ) ):?>
        <div class="mini-box">
          <div class="row">
            <div class="col-12">
              <form method="POST" action="/globelusCv/activate_access/" enctype="multipart/form-data" id="cv-access">
                <p>Obecnie nie posiadasz wykupionego dostępu do bazy kandydatów.<br/>Dostęp na <b>30 dni</b> to koszt <b>89</b> punktów.</p>
                <p class="text-red" style="font-weight: 600">Wybierz 3 kategorie i aktywuj dostęp.</p>
                <div class="row">
                  <?
                    $categories[0] = '--- brak przypisanej kategorii ---';
                    if ( is_array( $this -> categories ) and count( $this -> categories ) ): foreach ( $this -> categories as $category ):
                      echo '<div class="col-6">';
                        echo '<input type="checkbox" name="categories[]" class="checkbox cv-checkbox" value="' . $category['id'] . '"> ';
                        echo $category['name'];
                      echo '</div>';
                    endforeach; endif;
                  ?>
                </div>
                <p>&nbsp;</p>
                <p><button type="submit" class="btn btn-danger">Aktywuj dostep do bazy kandydatów</button></p>
              </form>
            </div>
          </div>
        </div>
      <? else:?>
        <div class="alert"><?= $lang['baza-cv-komunikat-brak-wynikow'];?>.</div>
      <? endif;?>
    <? endif;?>
    <?
      if ( \S::get( 'cp' ) == 1 )
      {
        $query_string = preg_replace( '/\/baza-cv/', '', $_SERVER['REQUEST_URI'] );
        $query_string = preg_replace( '/\//', '', $query_string );
      }
      else
        $query_string = preg_replace( '/\/baza-cv\/([0-9]*)\//', '', $_SERVER['REQUEST_URI'] );

      if ( ( $this -> cv_count / \front\factory\GlobelusCv::cv_list_limit ) > 1 ):
        echo \front\view\Globelus::pager(
                'baza-cv',
                ceil( $this -> cv_count / \front\factory\GlobelusCv::cv_list_limit ),
                $this -> current_page,
                $query_string
              );
      endif;
    ?>
  </div>
</div>
<link class="footer" rel="stylesheet" type="text/css" href="/plugins/selectize/css/selectize.bootstrap3.css">
<script class="footer" type="text/javascript" src="/plugins/selectize/js/selectize.js"></script>
<script class="footer" type="text/javascript">
  $( function()
  {
    $( '.positions' ).selectize({
      maxItems: 1
    });

    $( 'body' ).on( 'click', '#cv-access .btn', function( e )
    {
      e.preventDefault();

      <? if ( \front\factory\GlobelusFirms::get_firm_points( $this -> g_user['id'] ) < $globelus_settings['pkt-dostep-baza-kandydatow'] ):?>
        $.alert(
        {
          title: 'Dodstęp do bazy kandydatów',
          icon: 'fa fa-exclamation',
          content: 'Nie posiadasz wystarczającej ilości punktów aby aktywować dostęp do bazy kandydatów.',
          closeIcon: true,
          closeIconClass: 'fa fa-close',
          typeAnimated: true,
          animation: 'opacity',
          autoClose: 'confirm|10000',
          columnClass: 'large',
          theme: 'modern',
          buttons:
          {
            confirm:
            {
              text: '<?= \S::lang( 'zamknij' );?>',
              btnClass: 'btn-blue',
              keys: ['enter'],
              action: function() {}
            }
          }
        });
        return false;
      <? endif;?>

      var checked = $( "input.cv-checkbox:checked" ).length;
      if ( checked === 0 )
      {
        $.alert(
        {
          title: 'Dodstęp do bazy kandydatów',
          icon: 'fa fa-exclamation',
          content: 'Wybierz conajmniej jedną kategorię.',
          closeIcon: true,
          closeIconClass: 'fa fa-close',
          typeAnimated: true,
          animation: 'opacity',
          autoClose: 'confirm|10000',
          columnClass: 'large',
          theme: 'modern',
          buttons:
          {
            confirm:
            {
              text: '<?= \S::lang( 'zamknij' );?>',
              btnClass: 'btn-blue',
              keys: ['enter'],
              action: function() {}
            }
          }
        });
        return false;
      }

      if ( checked > 3 )
      {
        $.alert(
        {
          title: 'Dodstęp do bazy kandydatów',
          icon: 'fa fa-exclamation',
          content: 'Możesz wybrać maksymalnie <b>3 kategorie.<\/b>',
          type: 'blue',
          closeIcon: true,
          closeIconClass: 'fa fa-close',
          typeAnimated: true,
          animation: 'opacity',
          autoClose: 'confirm|10000',
          columnClass: 'large',
          theme: 'modern',
          buttons:
          {
            confirm:
            {
              text: '<?= \S::lang( 'zamknij' );?>',
              btnClass: 'btn-blue',
              keys: ['enter'],
              action: function() {}
            }
          }
        });
        return false;
      }

      $.alert(
      {
        title: 'Dodstęp do bazy kandydatów',
        icon: 'fa fa-exclamation',
        content: 'Dostęp do bazy kandydatów kosztuje <b><?= $globelus_settings['pkt-dostep-baza-kandydatow'];?> punktów<\/b>, zostaną one pobrane z Twojego konta.',
        closeIcon: true,
        closeIconClass: 'fa fa-close',
        typeAnimated: true,
        animation: 'opacity',
        columnClass: 'large',
        theme: 'modern',
        buttons:
        {
          cancel:
          {
            text: '<?= $lang['anuluj'];?>',
            action: function() {}
          },
          confirm:
          {
            text: '<?= $lang['zatwierdz'];?>',
            btnClass: 'btn-blue',
            keys: ['enter'],
            action: function()
            {
              $( '#cv-access' ).submit();
            }
          }
        }
      });
    });

    <? if ( $this -> categories_selected ):?>
      if ( $(document).width() >= 768 )
        $( '#subcontent-categories' ).show();
    <? endif;?>

    <? if ( $this -> voivodeships_selected ):?>
      if ( $(document).width() >= 768 )
        $( '#subcontent-voivodeships' ).show();
    <? endif;?>


    $( 'body' ).on( click_event, '.advert-filtr div.toggle', function(e)
    {
      var content_id = $( this ).attr( 'content' );
      if ( $( '#subcontent-' + content_id ).is( ':visible' ) )
      {
        $( '#subcontent-' + content_id ).hide();
        $( this ).children( 'i' ).removeClass( 'fa-chevron-up' ).addClass( 'fa-chevron-down' );
      }
      else
      {
        $( '#subcontent-' + content_id ).show();
        $( this ).children( 'i' ).removeClass( 'fa-chevron-down' ).addClass( 'fa-chevron-up' );
      }
      return false;
    });

    $( 'body' ).on( 'click', '.show-more', function(e)
    {
      e.preventDefault();
      var content_id = $( this ).attr( 'content' );
      $( '#' + content_id + ' li' ).show();
      $( this ).removeClass( 'show-more' ).addClass( 'show-less' ).html( 'mniej' );
    });

    $( 'body' ).on( 'click', '.show-less', function(e)
    {
      e.preventDefault();
      var content_id = $( this ).attr( 'content' );
      $( '#' + content_id + ' li' ).each( function( i )
      {
        if ( i > 4 )
          $( this ).hide();
      });
      $( this ).removeClass( 'show-less' ).addClass( 'show-more' ).html( 'więcej' );
    });
  });
</script>