<?php
/**
 * YAWIK
 *
 * @filesource
 * @copyright https://yawik.org/COPYRIGHT.php
 * @license   MIT
 */
/**
 * generates the list of applications, received by a recruiter
 */
$renderedFormCheckBox = '';


/**
 * @todo: should we write a view helper? Or should we move some code into the contoller?
 */

$formCheckBox = $this->services("FormElementManager.Checkbox");
/**
 * FormCheckbox::render requires the attribute "name". The value of the attribute
 * is currently unused
 */
$formCheckBox->setAttributes(array('name' => 'unused','style'=>'margin-top:-4px'));
$renderedFormCheckBox = $this->formCheckBox()->renderBare($formCheckBox);

$columnLabel = $renderedFormCheckBox.'
<span class="dropdown btn-group" id="action">
             <a class="btn btn-default btn-xs dropdown-toggle table-action" data-toggle="dropdown" style="margin-top: -8px;">
             <i class="yk-icon yk-icon-spinner fa-spin action-loading" style="display: none;"></i>
			 <b class="caret"></b>
             </a>
             <ul id="applicationTableAction" class="dropdown-menu">
             <li>
               <a href="#RejectApplication">' .
        $this->translate('Reject') .
               '</a>
               </li>
             <li>
               <a href="#" id="application-multiple-move" title="' . $this->translate('Move application to Talent Pool') . '">' . $this->translate('Move') . '</a>
               </li>
             </ul>
           </span>
';


$columns = array(
        $columnLabel => array('class' => 'yk-list-action'),
        /*@translate*/ 'Applicant'
                    => array(),
        /*@translate*/ 'Career'
                    => array('class' => 'yk-career'),
        /*@translate*/ 'Attachments'
                    => array('class' => 'yk-attachment'),
        /*@translate*/ 'Job'
                    => array('class' => 'yk-job'),
        /*@translate*/ 'Status'
                    => array('sort' => 'status'),
        /*@translate*/ 'Rating'
                    => array('sort' => 'rating'),
        /*@translate*/ 'Modified'
                    => array('sort' => 'date'),
        /*@translate*/ 'Actions'
                    => array(),
);

if (false !== strpos($this->sort,'-')) {
    $sortBy  = substr($this->sort, 1);
    $sortDir = false;
} else {
    $sortBy  = $this->sort;
    $sortDir = true;
}

echo $this->paginationControl($this->applications, 'Sliding', 'pagination-control', array('lang' => $this->params('lang'), 'route' => 'lang/applications'));
?>
<table class="table table-striped table-bordered table-hover" id="cam-application-list">
<thead>
<tr>
<?php foreach ($columns as $label => $option):?>
    <th <?php echo isset($option['class'])?'class="'.$option['class'].'"':'';?>>
    <?php if (! isset($option['sort'])): echo $this->translate($label);
          else:
              if ($option['sort'] == $sortBy):
                  $icon = '<span class="pull-right text-right yk-icon yk-icon-sort' . ($sortDir ? '-desc' : '-asc') . '"></span>';
                  $dir = !$sortDir;
              else:
                  $dir = true;
                  $icon = '<span class="pull-right yk-icon yk-icon-sort"></span>';
              endif;
              $sortParam = array('sort' => ($dir ? '' : '-') . $option['sort']);
              $url = $this->url(
                  'lang/applications', array(),
                  array('query' => array_merge($this->params()->fromQuery(), $sortParam)),
                  true
              );
            ?>
            <a href="<?php echo $url?>"><?php echo $this->translate($label).$icon?></a>
    <?php endif; ?>
    </th>
<?php endforeach; ?>
</tr>
</thead>

<tbody>
<?php
/* @var  $application Applications\Entity\Application */
foreach ($this->applications as $application):
  $isUnread = $application->isUnreadBy($this->auth('id'));?>
  <tr<?php echo ($isUnread?' class="yk-new"':"")?>>
    <td>
        <?php
        $formCheckBox = $this->services("FormElementManager.Checkbox");
        $formCheckBox->setAttributes(array('id' => $application->getId(), 'name' => 'cb_' . $application->getId() ));
        echo $this->formCheckBox($formCheckBox);
        ?>
    </td>
    <td>
    <div class="portrait">
        <a href="<?php echo $this->url('lang/applications/detail', array('id' => $application->getId()), /* reuse matched params*/ true)?>">
    <?php if ($application->getContact()->getImage()):?>
    <img src="<?php echo $this->basePath($application->getContact()->getImage()->getUri()) ?>" />
    <?php else:?>
    <span class="yk-icon yk-icon-portrait fa-3x"></span>
    <?php endif?>
    </div>
       <?php echo $this->translate($this->salutation($application->getContact()->getGender()))?>
       <?php echo $application->getContact()->getDisplayName()?><br>
       <?php echo $application->getContact()->getCity()?>
    </td>
    <td class="yk-career">
    <?php if (count($application->getCv()->getEducations())>0):?>
      <?php echo $this->translate('education').': '.$this->period($application->getCv()->getEducations()).' '.$this->translate('years')?><br/>
    <?php endif;?>
    <?php if (count($application->getCv()->getEmployments())>0):?>
      <?php echo $this->translate('work experience').': '.$this->period($application->getCv()->getEmployments()).' '.$this->translate('years')?><br/>
    <?php endif;?>
    </td>
    <td class="yk-attachment">
     <?php if(count($application->getAttachments())>0):?>
     <ul class="media-list">
     <?php foreach ($application->getAttachments() as $attachment): /* @var Applications\Entity\Attachment $attachment */?>
       <li class="media">
        <i class="yk-icon yk-icon-paperclip pull-left"></i>
        <div class="media-body">
         <a href="<?php echo $this->basePath($attachment->getUri())?>">
         <?php echo $attachment->getMetadata()->getName() ?>
         </a>
        </div>
       </li>
     <?php endforeach;?>
     </ul>
     <?php else:?>
     <?php echo $this->translate('no attachments')?>
     <?php endif;?>
     <?php if(count($application->getProfiles())>0):?>
         <div>
     <?php foreach ($application->getProfiles() as $profile): /* @var \Auth\Entity\SocialProfiles\ProfileInterface $profile */?>
         <span class="btn btn-default btn-xs"><i class="yk-icon yk-icon-<?php echo strtolower($profile->getName()); ?>"></i></span>
     <?php endforeach;?>
         </div>
     <?php endif;?>
    </td>
    <td class="yk-job">
        <?= $this->jobUrl($application->getJob()); ?>
    </td>
    <td>
     <?php echo $this->translate($application->getStatus()->getName()) ?>
     <br/><small><?php echo is_object($application->getSubscriber())?$application->getSubscriber()->getName():'' ?></small>
    </td>
    <td><?php echo $this->rating($application->getRating())?></td>
    <td title="Created: <?php echo $this->dateFormat($application->getDateCreated(),'short');?>">
        <?php echo $this->dateFormat($application->getDateModified());?>
    </td>
    <td>
        <div class="btn-group">
            <a href="<?php echo $this->url('lang/applications/detail', array('id' => $application->getId()), true)?>" class="btn btn-mini btn-default" title="<?php echo $this->translate('edit')?>"><span class="yk-icon yk-icon-edit"></span></a>
        </div>
    </td>
</tr>

<?php endforeach?>
</tbody>

</table>
<?php echo $this->paginationControl($this->applications, 'Sliding', 'pagination-control', array('lang' => $this->params('lang'), 'route' => 'lang/applications'));?>
