<?php
  // Set up page title:
  $this->headTitle($this->translate('Checked Out Items'));

  // Set up breadcrumbs:
  $this->layout()->breadcrumbs = '<li><a href="' . $this->url('myresearch-home') . '">' . $this->transEsc('Your Account') . '</a></li> <li class="active">' . $this->transEsc('Checked Out Items') . '</li>';
?>

<div class="<?=$this->layoutClass('mainbody')?>">
  <h2><?=$this->transEsc('Your Checked Out Items')?></h2>
  <?php if ($paginator): ?>
      <?
	$anz = $paginator->getTotalItemCount();
	if ($anz >= 100) : ?>
	<!-- 	<h3 style="color:red">Konto wird nicht vollständig angezeigt!</h3> -->
	<?php endif; ?>
  <?endif; ?>
	
  <?=$this->flashmessages()?>

  <?=$this->context($this)->renderInContext('librarycards/selectcard.phtml', ['user' => $this->auth()->isLoggedIn()]); ?>

  <?php if (!empty($this->transactions)): ?>
    <?php if ($this->renewForm): ?>
    <form name="renewals" method="post" id="renewals">
      <div class="toolbar">
        <div class="checkbox">
          <label>
            <input type="checkbox" name="selectAll" class="checkbox-select-all"/>
            <?=$this->transEsc('select_page')?>
          </label>
          <input type="submit" class="btn btn-default" id="renewSelected" name="renewSelected" value="<?=$this->transEsc("renew_selected")?>" />
          <input type="submit" class="btn btn-default" id="renewAll" name="renewAll" value="<?=$this->transEsc('renew_all')?>" />
        </div>
      </div>
    <?php endif; ?>

    <?php if ($paginator): ?>
      <?
        $end = min(
          $paginator->getAbsoluteItemNumber($paginator->getItemCountPerPage()),
          $paginator->getTotalItemCount()
        );
        $transParams = [
          '%%start%%' => $this->localizedNumber($paginator->getAbsoluteItemNumber(1)),
          '%%end%%'   => $this->localizedNumber($end),
          '%%total%%' => $this->localizedNumber($paginator->getTotalItemCount())
        ];
      ?>
      <?=$this->translate('showing_items_of_html', $transParams); ?>
    <?php endif; ?>

    <?php foreach ($hiddenTransactions as $ilsDetails): ?>
      <?php if (isset($this->renewResult[$ilsDetails['item_id']])): ?>
        <?php $renewDetails = $this->renewResult[$ilsDetails['item_id']]; ?>
        <?php $prefix = isset($ilsDetails['title']) ? $ilsDetails['title'] : $ilsDetails['item_id']; ?>
        <?php if (isset($renewDetails['success']) && $renewDetails['success']): ?>
          <div class="alert alert-success"><?=$this->escapeHtml($prefix . ': ') . $this->transEsc('renew_success')?></div>
        <?php else: ?>
          <div class="alert alert-danger"><?=$this->escapeHtml($prefix . ': ') . $this->transEsc('renew_fail')?><?php if (isset($renewDetails['sysMessage'])): ?>: <?=$this->escapeHtml($renewDetails['sysMessage'])?><?php endif; ?></div>
        <?php endif; ?>
      <?php endif; ?>
      <?php if (isset($ilsDetails['renewable']) && $ilsDetails['renewable'] && isset($ilsDetails['renew_details'])): ?>
        <?php $safeId = preg_replace('/[^a-zA-Z0-9]/', '', $ilsDetails['renew_details']); ?>
        <input class="pull-left flip" type="hidden" name="renewAllIDS[]" value="<?=$this->escapeHtmlAttr($ilsDetails['renew_details'])?>" />
      <?php endif; ?>
    <?php endforeach; ?>

    <?php $i = 0; foreach ($this->transactions as $resource): ?>
      <?php $ilsDetails = $resource->getExtraDetail('ils_details'); ?>
      <div id="record<?=$this->escapeHtmlAttr($resource->getUniqueId())?>" class="result">
        <?php if ($this->renewForm): ?>
          <?php if (isset($ilsDetails['renewable']) && $ilsDetails['renewable'] && isset($ilsDetails['renew_details'])): ?>
            <?php $safeId = preg_replace('/[^a-zA-Z0-9]/', '', $ilsDetails['renew_details']); ?>
            <div class="checkbox">
              <label>
                <input class="checkbox-select-item" type="checkbox" name="renewSelectedIDS[]" value="<?=$this->escapeHtmlAttr($ilsDetails['renew_details'])?>" id="checkbox_<?=$safeId?>" />
              </label>
              <input type="hidden" name="selectAllIDS[]" value="<?=$this->escapeHtmlAttr($ilsDetails['renew_details'])?>" />
              <input type="hidden" name="renewAllIDS[]" value="<?=$this->escapeHtmlAttr($ilsDetails['renew_details'])?>" />
            </div>
          <?php endif; ?>
        <?php endif; ?>

        <?
          $coverDetails = $this->record($resource)->getCoverDetails('checkedout', 'small', $this->recordLinker($this->results)->getUrl($resource));
          $cover = $coverDetails['html'];
          $thumbnail = false;
          $thumbnailAlignment = $this->record($resource)->getThumbnailAlignment('account');
          if ($cover):
            ob_start(); ?>
            <div class="media-<?=$thumbnailAlignment ?> <?=$this->escapeHtmlAttr($coverDetails['size'])?>">
              <?=$cover ?>
            </div>
            <?php $thumbnail = ob_get_contents(); ?>
          <?php ob_end_clean(); ?>
        <?php endif; ?>
        <div class="media">
          <?php if ($thumbnail && $thumbnailAlignment == 'left'): ?>
            <?=$thumbnail ?>
          <?php endif ?>
          <div class="media-body">
            <?
              // If this is a non-missing Solr record, we should display a link:
              if (is_a($resource, 'VuFind\\RecordDriver\\SolrDefault') && !is_a($resource, 'VuFind\\RecordDriver\\Missing')) {
                $title = $resource->getTitle();
                $title = empty($title) ? $this->transEsc('Title not available') : $this->escapeHtml($title);
                echo '<a href="' . $this->recordLinker($this->results)->getUrl($resource) .
                  '" class="title">' . $title . '</a>';
              } else if (isset($ilsDetails['title']) && !empty($ilsDetails['title'])){
                // If the record is not available in Solr, perhaps the ILS driver sent us a title we can show...
                echo $this->escapeHtml($ilsDetails['title']);
              } else {
                // Last resort -- indicate that no title could be found.
                echo $this->transEsc('Title not available');
              }
            ?><br/>
            <?php $listAuthors = $resource->getPrimaryAuthors(); if (!empty($listAuthors)): ?>
              <?=$this->transEsc('by')?>:
              <a href="<?=$this->record($resource)->getLink('author', $listAuthors[0])?>"><?=$this->escapeHtml($listAuthors[0])?></a><?php if (count($listAuthors) > 1): ?>, <?=$this->transEsc('more_authors_abbrev')?><?php endif; ?><br/>
            <?php endif; ?>
            <?php if (!empty($ilsDetails['callnumber'])): ?>
              <strong><?=$this->transEsc('Call number')?>:</strong> <?=$this->escapeHtml($ilsDetails['callnumber'])?>
              <br />
            <?php endif; ?>
            <?php if (count($resource->getFormats()) > 0): ?>
              <?=$this->record($resource)->getFormatList() ?>
              <br/>
            <?php endif; ?>
            <?php if (!empty($ilsDetails['volume'])): ?>
              <strong><?=$this->transEsc('Volume')?>:</strong> <?=$this->escapeHtml($ilsDetails['volume'])?>
              <br />
            <?php endif; ?>

            <?php if (!empty($ilsDetails['publication_year'])): ?>
              <strong><?=$this->transEsc('Year of Publication')?>:</strong> <?=$this->escapeHtml($ilsDetails['publication_year'])?>
              <br />
            <?php endif; ?>

            <?php if (!empty($ilsDetails['institution_name']) && (empty($ilsDetails['borrowingLocation']) || $ilsDetails['institution_name'] != $ilsDetails['borrowingLocation'])): ?>
              <strong><?=$this->transEsc('location_' . $ilsDetails['institution_name'], [], $ilsDetails['institution_name'])?></strong>
              <br />
            <?php endif; ?>

            <?php if (!empty($ilsDetails['borrowingLocation'])): ?>
              <strong><?=$this->transEsc('Borrowing Location')?>:</strong> <?=$this->transEsc('location_' . $ilsDetails['borrowingLocation'], [], $ilsDetails['borrowingLocation'])?>
              <br />
            <?php endif; ?>

            <?php if (isset($ilsDetails['renew'])): ?>
		<?php if(isset($ilsDetails['renewlimit'])) : ?>
		    <?php if($ilsDetails['renewLimit'] > 0) : ?>
              		<strong><?=$this->transEsc('Renewed')?>:</strong> <?=$this->transEsc($ilsDetails['renew'])?>
	      		<strong>(<?=$this->transEsc('Last renewal')?>: </strong> <?=$this->transEsc($ilsDetails['lastreneweddate'])?>)
              		<?php if (isset($ilsDetails['renewLimit'])): ?>
                		/ <?=$this->transEsc($ilsDetails['renewLimit'])?>
              		<?php endif; ?>
		     <?php endif; ?>
		   <br />
		<?php endif ; ?>
            <?php endif; ?>

            <?php $showStatus = true; ?>

            <?php if (isset($this->renewResult[$ilsDetails['item_id']])): ?>
              <?php $renewDetails = $this->renewResult[$ilsDetails['item_id']]; ?>
              <?php if (isset($renewDetails['success']) && $renewDetails['success']): ?>
                <?php $showStatus = false; ?>
                <strong><?=$this->transEsc('Due Date')?>: <?=$this->escapeHtml($renewDetails['new_date'])?> <?php if (isset($renewDetails['new_time'])): ?><?=$this->escapeHtml($renewDetails['new_time'])?><?php endif; ?></strong>
                <div class="alert alert-success"><?=$this->transEsc('renew_success')?></div>
              <?php else: ?>
                <strong><?=$this->transEsc('Due Date')?>: <?=$this->escapeHtml($ilsDetails['duedate'])?><?php if (isset($ilsDetails['dueTime'])): ?> <?=$this->escapeHtml($ilsDetails['dueTime'])?><?php endif; ?></strong>
                <div class="alert alert-danger"><?=$this->transEsc('renew_fail')?><?php if (isset($renewDetails['sysMessage'])): ?>: <?=$this->escapeHtml($renewDetails['sysMessage'])?><?php endif; ?></div>
              <?php endif; ?>
            <?php else: ?>
	      <strong><?=$this->transEsc('Issue Date')?>: <?=$this->escapeHtml($ilsDetails['issuedate'])?></strong>
	      <br>
              <strong><?=$this->transEsc('Due Date')?>: <?=$this->escapeHtml($ilsDetails['duedate'])?><?php if (isset($ilsDetails['dueTime'])): ?> <?=$this->escapeHtml($ilsDetails['dueTime'])?><?php endif; ?></strong>
              <?php if ($showStatus): ?>
                <?php if (isset($ilsDetails['dueStatus']) && $ilsDetails['dueStatus'] == "overdue"): ?>
                  <div class="alert alert-danger"><?=$this->transEsc("renew_item_overdue")?></div>
                <?php elseif (isset($ilsDetails['dueStatus']) && $ilsDetails['dueStatus'] == "due"): ?>
                  <div class="alert alert-info"><?=$this->transEsc("renew_item_due")?></div>
                <?php endif; ?>
              <?php endif; ?>
            <?php endif; ?>

            <?php if ($showStatus && isset($ilsDetails['message']) && !empty($ilsDetails['message'])): ?>
              <div class="alert alert-info"><?=$this->transEsc($ilsDetails['message'])?></div>
            <?php endif; ?>
            <?php if (isset($ilsDetails['renewable']) && $ilsDetails['renewable'] && isset($ilsDetails['renew_link'])): ?>
              <a href="<?=$this->escapeHtmlAttr($ilsDetails['renew_link'])?>"><?=$this->transEsc('renew_item')?></a>
            <?php endif; ?>
          </div>
          <?php if ($thumbnail && $thumbnailAlignment == 'right'): ?>
            <?=$thumbnail ?>
          <?php endif ?>
        </div>
        <?=$resource->tryMethod('supportsCoinsOpenUrl')?'<span class="Z3988" title="'.$this->escapeHtmlAttr($resource->getCoinsOpenUrl()).'"></span>':''?>
      </div>
    <?php endforeach; ?>
    <?php if ($this->renewForm): ?></form><?php endif; ?>
    <?=$paginator ? $this->paginationControl($paginator, 'Sliding', 'Helpers/pagination.phtml') : ''?>
  <?php else: ?>
    <?=$this->transEsc('You do not have any items checked out')?>.
  <?php endif; ?>
</div>

<div class="<?=$this->layoutClass('sidebar')?>">
  <?=$this->context($this)->renderInContext("myresearch/menu.phtml", ['active' => 'checkedout'])?>
</div>
