<?
  // 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>

  <? if ($paginator): ?>
      <?
        $anz = $paginator->getTotalItemCount();
        if ($anz >= 100) : ?>
                <h3 style="color:red">Konto wird nicht vollständig angezeigt!</h3>
        <? endif; ?>
  <?endif; ?>


  <?=$this->flashmessages()?>

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

  <? if (!empty($this->transactions)): ?>
    <? 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>
    <? endif; ?>

    <? 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); ?>
    <? endif; ?>

    <? foreach ($hiddenTransactions as $ilsDetails): ?>
      <? if (isset($this->renewResult[$ilsDetails['item_id']])): ?>
        <? $renewDetails = $this->renewResult[$ilsDetails['item_id']]; ?>
        <? $prefix = isset($ilsDetails['title']) ? $ilsDetails['title'] : $ilsDetails['item_id']; ?>
        <? if (isset($renewDetails['success']) && $renewDetails['success']): ?>
          <div class="alert alert-success"><?=$this->escapeHtml($prefix . ': ') . $this->transEsc('renew_success')?></div>
        <? else: ?>
          <div class="alert alert-danger"><?=$this->escapeHtml($prefix . ': ') . $this->transEsc('renew_fail')?><? if (isset($renewDetails['sysMessage'])): ?>: <?=$this->escapeHtml($renewDetails['sysMessage'])?><? endif; ?></div>
        <? endif; ?>
      <? endif; ?>
      <? if (isset($ilsDetails['renewable']) && $ilsDetails['renewable'] && isset($ilsDetails['renew_details'])): ?>
        <? $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'])?>" />
      <? endif; ?>
    <? endforeach; ?>

    <? $i = 0; foreach ($this->transactions as $resource): ?>
      <? $ilsDetails = $resource->getExtraDetail('ils_details'); ?>
      <div id="record<?=$this->escapeHtmlAttr($resource->getUniqueId())?>" class="result">
        <? if ($this->renewForm): ?>
          <? if (isset($ilsDetails['renewable']) && $ilsDetails['renewable'] && isset($ilsDetails['renew_details'])): ?>
            <? $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>
          <? endif; ?>
        <? endif; ?>

        <?
          $coverDetails = $this->record($resource)->getCoverDetails('checkedout', 'small', $this->recordLink()->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>
            <? $thumbnail = ob_get_contents(); ?>
          <? ob_end_clean(); ?>
        <? endif; ?>
        <div class="media">
          <? if ($thumbnail && $thumbnailAlignment == 'left'): ?>
            <?=$thumbnail ?>
          <? 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->recordLink()->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/>
            <? $listAuthors = $resource->getPrimaryAuthors(); if (!empty($listAuthors)): ?>
              <?=$this->transEsc('by')?>:
              <a href="<?=$this->record($resource)->getLink('author', $listAuthors[0])?>"><?=$this->escapeHtml($listAuthors[0])?></a><? if (count($listAuthors) > 1): ?>, <?=$this->transEsc('more_authors_abbrev')?><? endif; ?><br/>
            <? endif; ?>
            <? if (!empty($ilsDetails['callnumber'])): ?>
              <strong><?=$this->transEsc('Call number')?>:</strong> <?=$this->escapeHtml($ilsDetails['callnumber'])?>
              <br />
            <? endif; ?>
            <? if (count($resource->getFormats()) > 0): ?>
              <?=$this->record($resource)->getFormatList() ?>
              <br/>
            <? endif; ?>
            <? if (!empty($ilsDetails['volume'])): ?>
              <strong><?=$this->transEsc('Volume')?>:</strong> <?=$this->escapeHtml($ilsDetails['volume'])?>
              <br />
            <? endif; ?>

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

            <? 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 />
            <? endif; ?>

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

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

            <? $showStatus = true; ?>

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

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

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