<? global $lang;?>
<div id="user-panel">
  <?= \Tpl::view( 'globelus/firms/_partials/menu', [
    'active' => 'ogloszenia'
  ] );?>
  <div class="mini-box fluid">
    <div class="title">
      <?= ucfirst( $lang['lista-odpowiedzi'] );?>
    </div>
    <div class="subtitle mb0">
      <?= ucfirst( $lang['lista-odpowiedzi-do-ogloszenia'] );?>: <strong><?= $this -> advert['title'];?></strong>
    </div>
  </div>
  <? if ( is_array( $this -> answers ) and count( $this -> answers ) ):?>
    <? foreach ( $this -> answers as $answer ):?>
      <div class="answer <? if ( !$answer['displayed'] ):?>nondisplayed<? else:?>displayed<? endif?>">
        <div class="row">
          <div class="col-12">
            <div class="img">
              <? if ( $answer['user_id'] and file_exists( \front\factory\GlobelusCandidates::avatar_url( $answer['user_id'] ) ) ):?>
                <img src="/avatar/<?= $answer['user_id'];?>/<?= \S::get_token();?>">
              <? else:?>
                <img src="/images/no-avatar.png">
              <? endif;?>
            </div>
            <div class="content">
              <div class="title">
                <span class="name"><?= $answer['surname'] . ' ' . $answer['name'];?></span>
                <a href="#" class="delete text-danger" answer-id="<?= $answer['id'];?>"><?= \S::lang( 'usun' );?></a>
                <span class="date-add"><?= date( 'Y-m-d H:i', strtotime( $answer['date_add'] ) );?></span>
              </div>
              <div class="info">
                <?= $lang['tel.'];?> <?= $answer['phone'];?> |
                <?= $lang['email'];?> <a href="mailto:<?= $answer['email'];?>"><?= $answer['email'];?></a> |
                <? if ( ( $answer['cv'] and file_exists( $answer['cv'] ) ) || ( !$answer['cv'] and $answer['user_id'] and file_exists( \front\factory\GlobelusCandidates::cv_url( $answer['user_id'] ) ) ) ):?>
                  <?
                    if ( !$answer['cv'] and $answer['user_id'] and file_exists( \front\factory\GlobelusCandidates::cv_url( $answer['user_id'] ) ) )
                      $cv_url = '/cv/' . $answer['user_id'] . '/' . \S::get_token() . '/';
                    else if ( $answer['cv'] and file_exists( $answer['cv'] ) )
                      $cv_url = '/cv-tmp/' . $answer['id'] . '/' . \S::get_token() . '/';
                  ?>
                  <a href="<?= $cv_url;?>" class="cv" answer-id="<?= $answer['id'];?>">
                    <i class="fa fa-file-text"></i> <?= $lang['pobierz-cv'];?>
                  </a>
                <? endif;?>
                <? if ( $answer['user_id'] ):?>
                  | <a href="/kandydat/<?= $answer['user_id'];?>/<?= ( !$answer['surname'] and !$answer['name'] ) ? 'u' : \S::seo( $answer['surname'] . ' ' . $answer['name'] );?>/" target="_blank" answer-id="<?= $answer['id'];?>" class="visit-user"><?= $lang['zobacz-profil-kandydata-w-bazie-kandydatow'];?></a>
                <? endif;?>
              </div>
              <div class="text">
                <?= nl2br( $answer['text'] );?>
              </div>
            </div>
          </div>
        </div>
      </div>
    <? endforeach;?>
  <? else:?>
    <div class="alert alert-danger mt20"><?= $lang['panel-pracodawcy-brak-odpowiedzi'];?></div>
  <? endif;?>
  <div class="row">
    <div class="col-12 mt20">
      <a href="/panel-pracodawcy/ogloszenia" class="btn btn-danger mr15"><?= ucfirst( $lang['wstecz'] );?></a>
    </div>
  </div>
</div>
<script class="footer" type="text/javascript">
  $( function() {
    $( 'body' ).on( 'click', '.cv', function(e) {
      e.preventDefault();
      var answer = $( this ).parents( '.answer' );
      var href = $( this ).attr( 'href' );
      var answer_id = $( this ).attr( 'answer-id' );

      $.ajax(
      {
        type: 'POST',
        cache: false,
        url: '/GlobelusFirms/set_answer_as_displayed/',
        data:
        {
          answer_id: answer_id
        },
        beforeSend: function() {},
        success: function( data )
        {
          var win = window.open( href, '_blank' );
          win.focus();
          answer.removeClass( 'nondisplayed' ).addClass( 'displayed' );
        }
      });
    });

    $( 'body' ).on( 'click', '.visit-user', function(e) {
      e.preventDefault();
      <? if ( !is_array( $this -> g_user['categories'] ) or !count( $this -> g_user['categories'] ) ):?>
        $.alert({
          title: 'Oferty pracy',
          icon: 'fa fa-exclamation',
          content: '<?= \S::lang( 'baza-cv-komunikat' );?>',
          closeIcon: true,
          closeIconClass: 'fa fa-close',
          typeAnimated: true,
          animation: 'opacity',
          columnClass: 'large',
          theme: 'modern',
          buttons:
          {
            confirm:
            {
              text: '<?= \S::lang( 'zamknij' );?>',
              btnClass: 'btn',
              keys: ['enter'],
              action: function() {
              }
            }
          }
        });
      <? else:?>
        var answer = $( this ).parents( '.answer' );
        var href = $( this ).attr( 'href' );
        var answer_id = $( this ).attr( 'answer-id' );

        $.ajax(
        {
          type: 'POST',
          cache: false,
          url: '/GlobelusFirms/set_answer_as_displayed/',
          data:
          {
            answer_id: answer_id
          },
          beforeSend: function() {},
          success: function( data )
          {
            var win = window.open( href, '_blank' );
            win.focus();
            answer.removeClass( 'nondisplayed' ).addClass( 'displayed' );
          }
        });
      <? endif;?>
    });

    $( 'body' ).on( 'click', '#user-panel .answer .delete', function()
    {
      var answer_id = $( this ).attr( 'answer-id' );
      $.alert(
      {
        title: '<?= ucfirst( \S::lang( 'potwierdz' ) );?>',
        icon: 'fa fa-exclamation',
        content: '<?= \S::lang( 'potwierdz-usuniecie-odpowiedzi' );?>',
        type: 'orange',
        closeIcon: true,
        closeIconClass: 'fa fa-close',
        typeAnimated: true,
        animation: 'opacity',
        columnClass: 'large',
        theme: 'modern',
        buttons:
        {
          confirm:
          {
            text: '<?= \S::lang( 'zatwierdz' );?>',
            btnClass: 'btn-danger',
            keys: ['enter'],
            action: function()
            {
              document.location.href="/GlobelusFirms/answer_delete/advert_id=<?= $this -> advert['id'];?>&answer_id=" + answer_id;
            }
          },
          cancel:
          {
            text: '<?= \S::lang( 'anuluj' );?>',
            action: function() {}
          }
        }
      });
      return false;
    });
  });
</script>