<%args>
  $email       => undef
  $subject     => undef
  $issuer      => undef
  $cert_serial => undef
  $csr_serial  => undef
  $last        => undef
  $first       => undef
</%args>
<h1>
  <% i18nGettext('I18N_OPENXPKI_CLIENT_HTML_MASON_API_CERT_LIST_TITLE') %>
</h1>
<p>
  <% i18nGettext('I18N_OPENXPKI_CLIENT_HTML_MASON_API_CERT_LIST_DESCRIPTION') %>
</p>
<table id="table_list">
  <tr>
    <th><% i18nGettext('I18N_OPENXPKI_CERT_SERIAL') %></th>
    <th><% i18nGettext('I18N_OPENXPKI_CERT_SUBJECT') %></th>
    <th><% i18nGettext('I18N_OPENXPKI_CERT_EMAIL') %></th>
    <th><% i18nGettext('I18N_OPENXPKI_CERT_NOTBEFORE') %></th>
    <th><% i18nGettext('I18N_OPENXPKI_CERT_NOTAFTER') %></th>
    <th><% i18nGettext('I18N_OPENXPKI_CERT_ISSUER') %></th>
  </tr>
<%perl>
  ($first, $last) = (undef, undef);
  foreach my $serial (sort keys %list)
  {
    ## fill first and last marker
    $first = $serial if (not defined $first);
    $last  = $serial;

    ## create table columns
    print "  <tr>\n";
    print "    <td>\n";
</%perl>
        <& /lib/html/a.mhtml,
           'target' => "cert_info.html",
           'params' => [["identifier", $list{$serial}->{IDENTIFIER}]],
           'label'  => $serial &>
<%perl>
    print "    </td>\n";
    print "    <td>\n";
    print "      ".$list{$serial}->{SUBJECT}."\n";
    print "    </td>\n";
    print "    <td>\n";
    print "      ".$list{$serial}->{EMAIL}."\n";
    print "    </td>\n";
    print "    <td>\n";
    print "      ".join (" ", split "T", $list{$serial}->{NOTBEFORE})." UTC\n";
    print "    </td>\n";
    print "    <td>\n";
    print "      ".join (" ", split "T", $list{$serial}->{NOTAFTER})." UTC\n";
    print "    </td>\n";
    print "    <td>\n";
    print "      ".$list{$serial}->{ISSUER}."\n";
    print "    </td>\n";
  }
</%perl>
</table>

<table>
  <tr>
    <td>
      <& /lib/html/a.mhtml,
         'target' => "cert_list.html",
         'params' => [["email"       => $email],
                      ["subject"     => $subject],
                      ["issuer"      => $issuer],
                      ["cert_serial" => $cert_serial],
                      ["csr_serial"  => $csr_serial],
                      ["last"        => $first]],
         'label'  => '<== '.i18nGettext('I18N_OPENXPKI_LIST_PREV') &>
    </td>
    <td>
      <& /lib/html/a.mhtml,
         'target' => "cert_list.html",
         'params' => [["email"       => $email],
                      ["subject"     => $subject],
                      ["issuer"      => $issuer],
                      ["cert_serial" => $cert_serial],
                      ["csr_serial"  => $csr_serial],
                      ["first"        => $last]],
         'label'  => i18nGettext('I18N_OPENXPKI_LIST_NEXT').' ==>' &>
    </td>
  </tr>
</table>

<& /service/create_csr/print_errors.mhtml, 'errors' => \@errors &>

<%init>
  ## collect params
  my %fields = ();
  $fields{'EMAIL'}       = $email       if (defined $email and length $email);
  $fields{'SUBJECT'}     = $subject     if (defined $subject and length $subject);
  $fields{'ISSUER'}      = $issuer      if (defined $issuer and length $issuer);
  $fields{'CERT_SERIAL'} = $cert_serial if (defined $cert_serial and length $cert_serial);
  $fields{'CSR_SERIAL'}  = $csr_serial  if (defined $csr_serial and length $csr_serial);
  $fields{'LAST'}        = $last        if (defined $last and length $last);
  $fields{'FIRST'}       = $first       if (defined $first and length $first);
  $fields{'LIMIT'}       = 50;

  ## execute query
  my $msg  = $context->{client}->send_receive_command_msg (
                  "search_cert", \%fields);

  ## error or result ?
  my %list = ();
  my @errors = ();
  if (not exists $msg->{PARAMS} or ref $msg->{PARAMS} ne 'ARRAY')
  {
    push @errors, @{$msg->{LIST}};
  }
  else
  {
    foreach my $cert (@{$msg->{PARAMS}})
    {
      $list{$cert->{CERTIFICATE_SERIAL}}->{IDENTIFIER} = $cert->{IDENTIFIER};
      $list{$cert->{CERTIFICATE_SERIAL}}->{SUBJECT}    = $cert->{SUBJECT};
      $list{$cert->{CERTIFICATE_SERIAL}}->{EMAIL}      = $cert->{EMAIL};
      $list{$cert->{CERTIFICATE_SERIAL}}->{ISSUER}     = $cert->{ISSUER_DN};

      my $obj = DateTime->from_epoch(epoch => $cert->{NOTBEFORE});
      $list{$cert->{CERTIFICATE_SERIAL}}->{NOTBEFORE}  = OpenXPKI::DateTime::convert_date({DATE => $obj});
      $obj = DateTime->from_epoch(epoch => $cert->{NOTAFTER});
      $list{$cert->{CERTIFICATE_SERIAL}}->{NOTAFTER}   = OpenXPKI::DateTime::convert_date({DATE => $obj});
    }
  }
</%init>
<%once>
  ## FIXME: This is a damaged wrapper.
  ## FIXME: The wrapper should encapsulate the complete module.
  use OpenXPKI::DateTime;
  use DateTime;
</%once>
