<section style="padding: 32px 0;">
  <h1>Orders</h1>

  <form method="get" action="" class="row g-2 align-items-center" style="margin-bottom: 16px;">
    <div class="col-auto">
      <input type="text" name="q" value="<%= H(searchTerm) %>" class="form-control" placeholder="Search by email or jurisdiction #" />
    </div>
    <div class="col-auto">
      <button type="submit" class="button button-secondary">Search</button>
    </div>
    <% If Len(searchTerm) > 0 Then %>
    <div class="col-auto">
      <a href="?">Clear</a>
    </div>
    <% End If %>
  </form>

  <table class="table table-striped table-bordered">
    <thead>
      <tr>
        <th>Order ID</th>
        <th>Email</th>
        <th>Jurisdiction #</th>
        <th>Created</th>
        <th>Token Status</th>
        <th>Resend Link</th>
      </tr>
    </thead>
    <tbody>
    <% If orders.Count = 0 Then %>
      <tr><td colspan="6"><% If Len(searchTerm) > 0 Then %>No orders match your search.<% Else %>No orders found.<% End If %></td></tr>
    <% Else
        Dim orderKey, order, tokenStatus, continueUrl, mailtoBody, mailtoHref
        For Each orderKey In orders.Keys
            Set order = orders(orderKey)

            If Not IsNull(order("TokenUsedAt")) Then
                tokenStatus = "Used"
            ElseIf order("TokenExpiresAt") < Now() Then
                tokenStatus = "Expired"
            Else
                tokenStatus = "Active"
            End If

            continueUrl = "https://pe.kentcommunications.com/order/continue?token=" & MailtoEncode(order("Token"))
            mailtoBody = "Continue your order here: " & continueUrl
            mailtoHref = "mailto:" & order("Email") & _
                         "?subject=" & MailtoEncode("Continue your Purple Envelope order") & _
                         "&body=" & MailtoEncode(mailtoBody)
    %>
      <tr>
        <td><%= H(order("OrderID")) %></td>
        <td><%= H(order("Email")) %></td>
        <td><%= H(order("JurisdictionNumber")) %></td>
        <td><%= H(order("CreatedAt")) %></td>
        <td><%= H(tokenStatus) %></td>
        <td><a class="button button-secondary" href="<%= H(mailtoHref) %>">Email link</a></td>
      </tr>
    <%
        Next
      End If
    %>
    </tbody>
  </table>

  <% If pageCount > 1 Then
      Dim qsBase, pn
      qsBase = "?q=" & Server.URLEncode(searchTerm) & "&page="
  %>
  <nav aria-label="Orders pagination">
    <ul class="pagination">
      <li class="page-item<% If pageNum <= 1 Then %> disabled<% End If %>">
        <a class="page-link" href="<%= H(qsBase & (pageNum - 1)) %>">Previous</a>
      </li>
      <% For pn = 1 To pageCount %>
      <li class="page-item<% If pn = pageNum Then %> active<% End If %>">
        <a class="page-link" href="<%= H(qsBase & pn) %>"><%= pn %></a>
      </li>
      <% Next %>
      <li class="page-item<% If pageNum >= pageCount Then %> disabled<% End If %>">
        <a class="page-link" href="<%= H(qsBase & (pageNum + 1)) %>">Next</a>
      </li>
    </ul>
  </nav>
  <p><%= recordCount %> order<% If recordCount <> 1 Then %>s<% End If %> found.</p>
  <% End If %>
</section>
