<?php
/**
 * YAWIK
 *
 * @filesource
 * @copyright https://yawik.org/COPYRIGHT.php
 * @license   MIT
 */

if (count($jobs)):
$isRecruiter = $this->acl()->isRole( Auth\Entity\User::ROLE_RECRUITER );

?>
<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')?> / <?php echo $this->translate('Companyname') ?></th>
    <th><?php echo $this->translate('Name') ?></th>
    <th><?php echo $this->translate('Note') ?></th>
    <th><?php echo $this->translate('Status')?></th>
    <th class="col-md-1"><?php echo $this->translate('Action')?></th>
</tr>
</thead>

<tbody>
<?php /* @var Jobs\Entity\Job $job */?>
<?php foreach ($jobs as $job):

    $org = $job->getOrganization();
    $href = $job->getLink() ? $job->getLink() :$this->jobUrl($job, ['linkOnly' => true ]);

    $isSnapshot = $job->hasSnapshotDraft() && $job->getStatus()->is(\Jobs\Entity\StatusInterface::WAITING_FOR_APPROVAL);
    if ($isSnapshot):
        $job = $job->getLatestSnapshot();
    endif;
    ?>
<tr>
    <td>
        <?php if ($job->getDateCreated()): echo $this->dateFormat($job->getDateCreated(), 'short', 'none'); else: echo '&mdash;'; endif; ?>
        <br>
            <?php  if ($job->getDatePublishStart()): echo $this->dateFormat($job->getDatePublishStart(), 'short', 'none');
        endif?>
    </td>
    <td>
        <?php if ($isSnapshot): ?>
            <a href="<?=$this->url('lang/jobs/view', [], [ 'query' => [ 'snapshot' => $job->getSnapshotId() ]], true)?>">
              <span class="label label-warning"><?=$this->translate('Changed')?></span>
            </a>
        <?php endif ?>
        <a href="<?php echo $href ?>" target="_blank" rel="nofollow"><?php echo strip_tags($job->getTitle())?></a><br>
        <?php echo $job->getCompany() ?>
        <p>
            <small>
                <?=$job->getClassifications()->getProfessions()?><br>
                <?=$job->getClassifications()->getEmploymentTypes()?>
            </small>
        </p>
    </td>
    <td>
            <?php
            $displayName = '';
            $user = $job->getUser();
            if (isset($user)) {
                $displayName = $user->getInfo()->getDisplayName();
            }
            echo $displayName

            ?>
            <p><small><?=$job->getClassifications()->getProfessions()?><br>
             <?=$job->getClassifications()->getEmploymentTypes()?></small></p>
    </td>
    <td>
            <?php $note = $job->getMetaData('note');
                    if ($note):
                        ?><div class="note-box"><?=$note?></div><?php
                    else:
                        ?>&nbsp;<?php
                    endif;
            ?>
    </td>
    <td><?php echo $this->translate($job->getStatus()->getName()); ?></td>
    <td class="text-nowrap"><div class="btn-group btn-toolbar">
        <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 data-toggle="modal" class="btn btn-default btn-xs" data-target="#job-edit-popup"
           href="<?php echo $this->url('lang/admin/jobs', [ 'action' => 'edit', 'lang' => $this->params('lang') ], ['query' => [ 'id' => $job->getId() ]]); ?>"
           title="<?php echo $this->translate('Edit state') ?>">
            <i class="yk-icon fa-pencil-square-o"></i>
           </a>
        <a href="<?=$this->plugin('Jobs/AdminEditLink')->__invoke($job)?>" class="btn btn-default btn-xs" title="<?=$this->translate('Edit')?>">
            <i class="yk-icon yk-icon-edit"></i>
        </a>
            <?php if (\Jobs\Entity\StatusInterface::CREATED == $job->getStatus()->getName()): ?>
                <a href="<?php echo $this->url('lang/jobs/approval', array('action' => 'edit'), array('query' => array('id' => $job->getId())), true) ?>" class="btn btn-success btn-xs"><span class="yk-icon fa-check"></span></a>
            <?php endif ?>
                <?php if (\Jobs\Entity\StatusInterface::WAITING_FOR_APPROVAL == $job->getStatus()->getName()):

                    $snapshot = $isSnapshot ? $job : $this->services('repositories.Jobs/JobSnapshot')->findLatest($job->getId(), true);

                    if ($snapshot){
                      $query=['snapshot' => $snapshot->getSnapshotId()];
                    }else{
                      $query=[];
                    }
                    ?>
                    <a href="<?php echo $this->url('lang/jobs/approval', array('action' => 'edit'), array('query' => $query), true) ?>" class="btn btn-success btn-xs"><span class="yk-icon fa-check"></span></a>
                <?php endif ?>
                <a data-jobtitle="<?=$job->getTitle()?>" href="<?=$this->ajaxUrl('jobs.delete', ['id' => $job->getId()])?>" class="delete-link btn btn-danger btn-xs" title="<?=$this->translate('Delete')?>">
                    <i class="yk-icon yk-icon-delete"></i>
                </a>
        </div>
    </td>
</tr>
<?php endforeach?>
</tbody>

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



<?php endif ?>
