<html>
<template:meta default-text-matching="regex"/>
<head>
{
(:declare function local:getpage($u){ this function cannot be declared here, because it would override an existing function and then crash when something still references the old function
  (extract($u, "anzPos=([0-9]+)", 1)[.], "1")[1]
};:)
nextPage := get('nextPage', ()),
startUrl := get('startUrl', ''),
touchpoint := get('touchpoint', false())
}
</head>
<body>
   <div id="main"> 
     <t:element id="middle"> 
      {
      let $account-links := .//a[contains(@href, "methodToCall=showAccount") and extract(.., "[0-9]+")[. > "0"]]
      return (
        orderedUrl := ($account-links[ contains(., "typ=ordered") or matches(., "Bestellungen|Requests") ])[1]/@href, 
        requestedUrl := ($account-links[ contains(., "typ=requested") or matches(., "Vormerkungen|Reservations") ])[1]/@href
      )
      }
      <t:if test="$touchpoint">
        <ul id="account-nav"/>
        <script>{let $temp := extract(., 'typ="([^"]+)', 1) return switch ($temp) 
          case "loaned" return    ( mode := 1, if (empty($nextPage)) then vl:delete-current-books() else () ) 
          case "ordered" return   ( mode := 2 )
          case "requested" return ( mode := 3 )
          default return vl:raise-internal("Unbekannter Medienlistentyp: " || $temp)
          }</script>
      </t:if>
        
  <div t:condition="@id = ('tab-content','account-data')">
  
    <t:if test="not($touchpoint)">
      <div class="box">
        <template:switch>
          <div class="box-header">
            <template:switch>
              <h2>Ausleihen|Loans
                 <t:s>mode := 1</t:s>
                 <t:if test="empty($nextPage)"><template:read var="delete-current-books()"/></t:if></h2>
              <h2>Bestellungen|Requests<t:s>mode := 2</t:s></h2>
              <h2>Vormerkungen|Reservations<t:s>mode := 3</t:s></h2>
            </template:switch>
          </div>
          
          <h1>Ausleihen|Loans
             <t:s>mode := 1</t:s>
             <t:if test="empty($nextPage)"><template:read var="delete-current-books()"/></t:if></h1>
          <h1>Bestellungen|Requests<t:s>mode := 2</t:s></h1>
          <h1>Vormerkungen|Reservations<t:s>mode := 3</t:s></h1>
          
        </template:switch>
      </div>
    </t:if>
    <!--<fieldset>-->

    {
     $nextPage := (),
     $default-book := map:merge((switch ($mode) 
       case 3 return {"statusId": "ordered", "cancelable": false()}
       case 2 return {"statusId": "provided", "cancelable": false()}
       default return {"statusId": "critical"},
       {"_onPage": let $page := base-uri() 
                   return if (contains($page, "anzPos")) then $page
                   else request-combine($page, "anzPos=1")?url, "_onPageStart": $startUrl})
     )
    }

<!--    <template:if test="$allBooksAway=''">-->
    <t:switch prioritized="true">
      <table class="data"> 
        <tr>
          <th scope="col"></th>
        </tr>
  
        <template:loop>      
  
        <tr>
          {book := $default-book,
           let $selectionInputs := td[1]//input
           where $selectionInputs
           let $sel := $selectionInputs[starts-with(@name, "selectedMediaListentries")]
           where empty($sel/@disabled)
           let $id := $selectionInputs[starts-with(@name, "mediaNrEntries") ]
           return $book.actionID := $id/@value}
          <t:if test="$touchpoint">
            <td/><td><img/></td>
          </t:if>
          <t:else>
            <th scope="row"></th>
          </t:else>
          <td>
            <t:switch> 
              <strong>{$book.title}</strong>
              <b>{$book.title}</b>
            </t:switch>
            <br/>
            {$book.author := matched-text()}<br/>
            {$book.id := extract(matched-text(), "^[^/]*")}
            <a template:optional="true">
              {$book.actionURI := resolve-html(.)}
              <span>{$book.status}</span>
            </a>
            <span template:optional="true">{$book.status}</span>
            <a template:optional="true">
              {book.actionURI := resolve-html(.),
               book.cancelable := $mode eq 3
              }
            </a>
            {
              if ($book.actionURI or $book.actionID) then (
                if ($mode = (2,3)) then () 
                else $book.statusId := "curious"
              ) else if (not($book.status)) then
                $book.status := (br[last()]/following-sibling::text())[last()]
              else ()              
            }
           </td>

           <template:switch>        
             <td>{
               switch ($mode)
                 case 2 
                 case 3 return  ( (:  bestellt | ... br Vormerkung :)
                   book.status := ./text()[1],
                   book.issuedate := parse-date(extract(., '\d{2}\.\d{2}\.\d{4}', 0), 'dd.mm.yyyy|'), 
                   book.cancelable := boolean($book.actionURI)
                 )
                 default return 
                 let $dates := extract(., '((\d{2}\.\d{2}\.\d{4}) *-)? *(\d{2}\.\d{2}\.\d{4})', (1,3)) 
                 return (
                   if ($dates[1]) then book.issuedate:=parse-date($dates[1], 'dd.mm.yyyy') else (),
                   if ($dates[2]) then book.duedate:=parse-date($dates[2], 'dd.mm.yyyy') else vl:raise("DATUM KANN NICHT GELESEN WERDEN: " || .)
                 )
             }</td>
           </template:switch>
         </tr>
                
        </template:loop>      
                 
        </table>
      
      
      
      
      
      
        <div class="data">
          <div class="tableheader row d-flex">
          </div>
          <div class="row d-flex">
            <div class="account-display-title">
              {$book:=$default-book}
              <t:element t:condition="self::strong or self::b">{$book.title}</t:element><br/>
              {$book.author := matched-text()}<br/>
              {let $mt := matched-text()
               return ( 
                 $book.id := extract($mt, "^[^/]*"), 
                 $book.libraryLocation := substring-after($mt, "/")
               )}<br/>?
              <span>{$book.status}</span>?
              <a>{$book.actionURI := resolve-html(.), 
                  if ($mode = (2,3)) then () else book.statusId := "curious"
              }</a>?
            </div>
            <div class="account-display-state">{
              switch ($mode) 
                case 2 case 3 return ( 
                   book.status := inner-text(),
                   book.issuedate := parse-date(extract(., '\d{2}\.\d{2}\.\d{4}', 0), 'dd.mm.yyyy|'), 
                   book.cancelable := boolean($book.actionURI)
                 )
                default return 
                 let $dates := extract(., '((\d{2}\.\d{2}\.\d{4}) *-)? *(\d{2}\.\d{2}\.\d{4})', (1,3)) 
                 return (
                   if ($dates[1]) then book.issuedate:=parse-date($dates[1], 'dd.mm.yyyy') else (),
                   if ($dates[2]) then book.duedate:=parse-date($dates[2], 'dd.mm.yyyy') else vl:raise("DATUM KANN NICHT GELESEN WERDEN: " || .)
                 )
            }</div>
          </div>*
        </div>
        



        <div class="container-fluid"> <!-- bremen -->
          <div class="tableheader row border-bottom py-2">
            <div class="d-none"> </div>    
            <div class="col-md-1"> </div>
            <div class="col-md-7">Ti</div>    
            <div class="d-none">Le</div>
          </div>
          <div class="row">
            {book := $default-book,
             let $selectionInputs := .//input
             where $selectionInputs
             let $sel := $selectionInputs[starts-with(@name, "selectedMediaListentries")]
             where empty($sel/@disabled)
             let $id := $selectionInputs[starts-with(@name, "mediaNrEntries") ]
             return ($book.actionID := $id/@value, $book.statusId := "normal")}
            <div></div>
            <div class="col-2"><img/>?</div>
            <div class="col-8 col-md-7 my-2">    
             <span class="d-block">
              <strong>{$book.title}</strong>
             </span>
             <span class="d-block">{$book.author}</span> <!--????-->
                  
             <span class="d-block">
              <span>{$book.status}</span>
              </span>
             </div>
                  
             <div class="d-none d-md-block col-md-3 my-2">{
               let $dates := extract(., '((\d{2}\.\d{2}\.\d{4}) *-)? *(\d{2}\.\d{2}\.\d{4})', (1,3)) 
               return (
                 if ($dates[1]) then book.issuedate:=parse-date($dates[1], 'dd.mm.yyyy') else (),
                 if ($dates[2]) then book.duedate:=parse-date($dates[2], 'dd.mm.yyyy') else vl:raise("DATUM KANN NICHT GELESEN WERDEN: " || .)
               )      
              }</div>
          </div>+
        </div>

       
        
        
        <div>Ausgewähltes Unterkonto enthält keine Daten|Selected sub-account does not contain any data</div>

        <div class="container-fluid"><div class="row"><div class="alert message-info"></div></div></div>
      </t:switch>
      
      
      
      
      
      
      
      
      
      
      
      
      
        
          <!--    </template:if>-->
    <!-- </fieldset> -->
      <t:element t:condition="matches(@class, 'box-right|pagination')">
        { $nextPage :=  (
            let $pagelinks := .//a/@href[contains(., "anzPos=")]
            return 
              if (exists($pagelinks)) then 
                let $curAnzPos := (extract(base-uri(), "anzPos=([0-9]+)", 1)[.], "1")[1]
                let $links := $pagelinks[(extract(., "anzPos=([0-9]+)", 1)[.], "1")[1] >  $curAnzPos]/string()
                return min($links)
              else
                let $account-form := form(ancestor::form[1])
                let $account-form-params := request-decode($account-form)?params
                let $size := $account-form-params?size[.] ! xs:integer(.)
                let $startPos := $account-form-params?startPos[.] ! xs:integer(.)
                let $countPos := $account-form-params?countPos[.] ! xs:integer(.) otherwise 10
                where exists($size) and exists($startPos) and ($startPos + $countPos le $size)
                return request-combine($account-form, {"methodToCall": "pos", "anzPos": $startPos + $countPos})
         )
        }
      </t:element>?
   </div>
  </t:element>
 </div>
</body>
</html>
  
