<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action id="connect"></action>

<action id="update-all">

  <page url="https://{$server}/aDISWeb/app"> <template>
   <a>{connectPage:=@href}</a>
  </template> </page>
  <page url="{$connectPage}">
  <template>
    <a><span>Benutzerkonto</span>{loginForm:=@href}</a>
  </template>
  </page>

  <page url="https://{$server}/{$loginForm}">
    <template><form>{
      loginFormPost := form(., {"$Textfield": $username, "$Textfield$0": $password})
    }</form></template>
  </page>

  <page url="{$loginFormPost}">
    <post name="textButton" value="Anmeldung abschicken"/>
  
  
    <template>
    <t:switch>
    <a>Ausleihen zeigen oder verlängern<t:s>kontoPage := @href</t:s></a>
    <a>Ausleihe zeigen oder verlängern<t:s>kontoPage := @href</t:s></a>
    </t:switch>
      <!--<t:s>kontoPage := extract(@href, "(.*)&amp;requestCount", 1),
           requestCount := extract(@href, "requestCount=([0-9]+)", 1)
      </t:s>-->
    </template>
  </page>


  <page url="https://{$server}/{$kontoPage}">
    <template>
      <DIV class="aDISMaske"><!-- this page shows the books, but not if they are renewable. need to click on a button for that -->
      <FORM>
      {checkFormPost := (form(., jn:object((
        (.//input[(@type = "submit" and contains(@value, "verlängerbar"))])[1]/{string(@name): @value},
        (.//input[(@type = "checkbox" and not(exists(@checked)) and not(exists(@disabled))) ])/{string(@name): "on"}
        ))))} <!-- that's not fully correct because it contains two "clicked" submit buttons. And uses checkbox=& instead of checkbox=on& . And encodes " " as "%20" instead of "+" like Firefox -->
      
      <DIV class="clearfix" id="content">
        <TABLE class="rTable_table">
          <template:read var="delete-current-books()"/>
          {hasBooks := false()}
          <input type="checkbox" t:optional="true">{hasBooks := true()}</input>
        </TABLE>
      </DIV>

      </form>
      </div>
    </template>
  </page>

  <page url="{$checkFormPost}" test="$hasBooks">    
    <template>
      <DIV class="aDISMaske">
      <FORM>
      {extendFormPost := form(., jn:object((.//input[@type = "submit" and contains(@value, "arkierte") and contains(@value, "ngern")])[1]/{string(@name): @value})),
      extendFormPost.post := replace($extendFormPost.post, "[a-zA-Z0-9]+=on&", "")
      }
      
      <DIV class="clearfix" id="content">
      
      <DIV class="innen">
      <DIV class="fsgrp">
      <DIV class="rTable_div">
      <TABLE class="rTable_table">
      <t:loop>
      <TR>
      <TD>{book := object(),
           book.extendid := encode-for-uri(input/@name)
           }</TD>
      <TD>{book.duedate := parse-date(., "dd.mm.yyyy")}</TD>
      <TD>{book.category := extract(text(), "\[(.*)\]", 1),  
           book.title :=  if (contains(text(), "]")) then extract(text(), "(\] )(.*)", 2) else text()}<BR/>
          {preid := text()}<BR/>
          {book.id := concat($preid,":",text())}</TD>
      <TD>{message := if (contains(., "- Stand")) then extract(deep-text("   "), "(.*)- Stand", 1) else deep-text("   ")}
          <t:if test="contains(., 'nicht verl')"><t:read source="$message" var="book.status:problematic"/></t:if>
          <t:else><t:read source="$message" var="book.status:curious"/></t:else>
      </TD>
      </TR>
      </t:loop>
      </TABLE>
      </DIV>
      </DIV>
      </DIV>
      </DIV>
      </FORM>
      </DIV>

    </template>
  </page>
  
  
</action>

<action id="renew-list">
  <variable name="book-list">string-join(for $book in $renew-books return x"{$book.extendid}=on", "&amp;")</variable> 
  <page url="{$extendFormPost}">
    <post value="{$book-list}"/>
  </page>
  
  <call action="update-all"/>
</action>

</actions>
