<%args>
    $type   => undef
    $id     => undef
    $filled => undef
</%args>

<& /service/open_form.mhtml, action => 'change_csr_info.html' &>

<& /lib/html/hidden.mhtml, 'name' => 'type',   'value' => $type &>
<& /lib/html/hidden.mhtml, 'name' => 'id',     'value' => $id &>
<& /lib/html/hidden.mhtml, 'name' => 'filled', 'value' => '1' &>

<h1><% i18nGettext ('I18N_OPENXPKI_CLIENT_HTML_MASON_ACTIVITY_CHANGE_CSR_INFO_TITLE') %></h1>
<p>
  <% i18nGettext('I18N_OPENXPKI_CLIENT_HTML_MASON_ACTIVITY_CHANGE_CSR_INFO_DESCRIPTION') %>
</p>

<table>
% foreach my $key (keys %info)
% {
    <tr>
      <td><% i18nGettext ($key) %></td>
      <td>
        <& /lib/html/input.mhtml, 'type' => 'text', 'name' => "info_$key", value => $info{$key} &>
      </td>
    </tr>
% }
</table>

<& /service/send_form.mhtml &>
<& /service/close_form.mhtml &>

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

<%init>
    my @errors = ();
    my %info   = ();
    my $text   = "";
    my $serializer = OpenXPKI::Serialization::Simple->new({SEPARATOR => "-"});

    ## load the workflow and extract the additional infos
    my $msg = $context->{client}->send_receive_command_msg
              (
                  "get_workflow_info",
                  {
                      "WORKFLOW" => $type,
                      "ID"       => $id
                  }
              );
    $text = $msg->{PARAMS}->{WORKFLOW}->{CONTEXT}->{cert_info};
    ## serialized stuff
    %info   = %{$serializer->deserialize($text)};

    if ($filled)
    {
        ## this is the second step - so we have a filled form

        ## build the DN
        my $ARGS   = {$m->request_args()};
        foreach my $key (keys %info)
        {
            ## change info in hash
            $info{$key} = $ARGS->{"info_$key"};
        }

        ## serialize info
        ## warnings we must be safe against \n truncation
        $text = $serializer->serialize(\%info);

        ## fix the workflow
        my $msg = $context->{client}->send_receive_command_msg (
                      "execute_workflow_activity",
                      {WORKFLOW => $type,
                       ID       => $id,
                       ACTIVITY => "I18N_OPENXPKI_WF_ACTION_CHANGE_CSR_INFO",
                       PARAMS   => {"cert_info" => $text}});
        if (exists $msg->{SERVICE_MSG} and
            $msg->{SERVICE_MSG} eq "ERROR")
        {
            @errors = $m->comp ('/lib/get_deep_error.mhtml', 'msg' => $msg);
        }
        else
        {
            return $m->comp ('/service/workflow/show_instance.html',
                             'msg'  => $msg,
                             'type' => $type,
                             'id'   => $id);
        }
    }

    ## ok we have to display the whole stuff
</%init>
