<?php
/**
 * YAWIK
 *
 * @filesource
 * @copyright https://yawik.org/COPYRIGHT.php
 * @license   MIT
 */
if (count($this->jobs)):
$isRecruiter = $this->acl()->isRole( Auth\Entity\User::ROLE_RECRUITER );
$snapshots = $this->services('repositories.Jobs/JobSnapshot');
?>
<table class="table table-striped table-bordered table-hover" id="jobs-list">
<thead>
<tr>
    <th><?php echo $this->translate('Date of receipt')?></th>
    <th><?php echo $this->translate('Title of the job')?></th>
    <th><?php echo $this->translate('Location')?></th>
    <th><?php echo $this->translate('Companyname')?></th>
    <th><?php echo $this->translate('Reference')?></th>
    <?php if ($isRecruiter):?>
       <th><?php echo $this->translate('Applications')?></th>
       <th><?php echo $this->translate('Status')?></th>
       <th><?php echo $this->translate('Action')?></th>
    <?php else: ?>
       <th><?php echo $this->translate('Action')?></th>
    <?php endif;?>
</tr>
</thead>

<tbody>
<?php foreach ($jobs as $job): /* @var  \Jobs\Entity\Job $job */
$isSnapshot = $job->hasSnapshotDraft();
 ?>
<tr>
    <td><?php if ($job->getDatePublishStart()): echo $this->dateFormat($job->getDatePublishStart(), 'short', 'none');
              elseif ($job->getDateCreated()): echo $this->dateFormat($job->getDateCreated(), 'short', 'none'); endif?>
        <?php if ($isRecruiter):?>
        <br>(<?php
            $displayName = '';
            $user = $job->getUser();
            if (isset($user)) {
                $displayName = $user->getInfo()->getDisplayName();
            }
            echo $displayName

            ?>)
        <?php endif ?></td>

    <td>
        <?php if ($isSnapshot): ?><span class="label label-warning"><?=$this->translate('Changed')?></span><?php
        elseif ( ($snapshot = $job->getLatestSnapshot()) ):
            $snapshotStatus = $snapshot->getSnapshotMeta()->getStatus();
            $snapshotAccepted = \Jobs\Entity\JobSnapshotStatus::ACCEPTED == $snapshotStatus; ?>

                <?= $snapshotAccepted ? '<span class="label label-success">'.$this->translate('Accepted').'</span>'
                    : sprintf('<a class="label label-danger" href="%s">%s</a>',
                        $this->jobUrl($snapshot, ['showPendingJobs' => $this->showPendingJobs, 'linkOnly' => true]),
                        $this->translate('Rejected')
                    ); ?>

        <?php endif ?>
        <?=$this->jobUrl($job,['showPendingJobs' => $this->showPendingJobs ]) ?>
        <br />
        <small><?=$job->getClassifications()->getProfessions()?></small> |
        <small><?=$job->getClassifications()->getEmploymentTypes()?></small>
    </td>
    <td><?php echo $this->formatLocation($job->getLocations())?></td>
    <td><?php echo $job->getCompany()?></td>
    <td><?php echo empty($job->getReference())? $job->getApplyId():$job->getReference() ?></td>

      <?php if ($isRecruiter):?>
          <td>
         <?php if ($job->getApplications()->count()>0):?>
            <a title="<?php echo $this->translate("total")?>" href="<?php echo $this->url( 'lang/applications', array(), array('query' => 'job=' . $job->getId()), true) ?>">
            <span class="badge"><?php echo $job->getApplications()->count() ;?></span>
            </a>
         <?php endif;?>
         <?php if ($job->getUnreadApplications()->count()):?>
            /
            <a title="<?php echo $this->translate("unread")?>"
               href="<?php echo $this->url( 'lang/applications', array(), array('query' => 'job=' . $job->getId() . '&unread=1'), true) ?>">
            <span class="badge"> <?php echo $job->getUnreadApplications()->count()?></span>
            </a>
             </td>
         <?php endif;?>
         <td>
         <?php
            $status = array();
            if (!empty($job->getStatus())) {
                $status[] = $this->translate($job->getStatus()->getName());
            }
            if ($job->isDraft()) {
                $status[] = $this->translate('draft');
            }
            echo implode('<br />', $status);
         ?>
         </td>
         <td class="btn-group btn-toolbar"><?php if ($this->acl($job, 'edit')): ?>
             <?php $org = $job->getOrganization(); if ($org && $org->getUser()):?>
             <a href="<?php echo $this->url('lang/jobs/assign-user', array(), array('query' => array('id' => $job->getId())), true)?>"
                data-id="<?php echo $job->getId() ?>"
                data-i18n='{"title":"<?php echo $this->escapeHtmlAttr(sprintf(
                                            $this->translate('Change responsible user for job \\"%s\\"'),
                                            $job->getTitle()
                                       ))?>"}'
                class="btn btn-default btn-xs assign-user-button"><span class="yk-icon yk-icon-user"></span></a>
                     <?php endif; ?>

         <a data-title="title"
            data-toggle="modal"
            class="btn btn-default btn-xs"
            data-target="#job-application-history"
            href="<?php echo $this->url( 'lang/jobs/history', array('id' => $job->getId() , 'lang' => $this->params('lang'))); ?>"
            title="<?php echo $this->translate('Status history')?>">
             <i class="yk-icon yk-icon-history"></i>
         </a>

         <a href="<?php echo $this->url('lang/jobs/manage', array('action' => 'edit'), array('query' => array('id' => $job->getId())), true) ?>" class="btn btn-default btn-xs">
             <span class="yk-icon yk-icon-edit"></span></a>
                 <?php if ($job->isDraft()):?>
         <a href="<?php echo $this->url('lang/jobs/manage', ['action' => 'delete'], ['query' => ['id' => $job->getId()]], true)?>"
            onclick="return window.confirm('<?=$this->translate('Are you sure to delete this job?')?>')"
            class="btn btn-danger btn-xs"><i class="yk-icon fa-trash"></i></a>
                    <?php endif ?>
         <?php endif; ?>
         </td>
      <?php else:?>
          <?= $this->urlApply($job)?>
      <?php endif ?>
     </td>
</tr>
<?php endforeach?>
</tbody>

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


<?php endif ?>
