<%args>
    $workflow_type => undef
    $role          => undef
    $profile       => undef
    $subject_style => undef
    $keygen        => undef
    $pkcs10        => undef
    $subject       => undef
    $CONFIG        => undef
</%args>

<& /service/open_form.mhtml &>

<& /lib/html/hidden.mhtml, 'name' => 'workflow_type', 'value' => $workflow_type &>
<& /lib/html/hidden.mhtml, 'name' => 'role',          'value' => $role &>
<& /lib/html/hidden.mhtml, 'name' => 'profile',       'value' => $profile &>
<& /lib/html/hidden.mhtml, 'name' => 'subject_style', 'value' => $subject_style &>
<& /lib/html/hidden.mhtml, 'name' => 'keygen',        'value' => $keygen &>
<& /lib/html/hidden.mhtml, 'name' => 'pkcs10',        'value' => $pkcs10 &>

<h1><% i18nGettext ('I18N_OPENXPKI_CLIENT_HTML_MASON_CREATE_CSR_GET_SUBJECT_TITLE') %></h1>
<p>
  <% i18nGettext('I18N_OPENXPKI_CLIENT_HTML_MASON_CREATE_CSR_GET_SUBJECT_DESCRIPTION') %>
</p>

<table>
% for (my $i=0; $i < scalar @{$CONFIG->{$subject_style}->{SUBJECT}}; $i++)
% {
    <tr>
%   ## scan RDN $i
%   for (my $k=0; $k < scalar @{$CONFIG->{$subject_style}->{SUBJECT}->[$i]}; $k++)
%   {
%     ## attribute $k of RDN $i
%     if ($k > 0) {
      <td>+</td>
%     }
      <td><div id="help">
%     # my $label = i18nGettext ($CONFIG->{$subject_style}->{SUBJECT}->[$i]->[$k]->{LABEL});
%     #  <a href="#dummy"><span><% $label %></span><span class="onOver"><% $descriptions{$label} %></span></a>
        <% i18nGettext ($CONFIG->{$subject_style}->{SUBJECT}->[$i]->[$k]->{LABEL}) %>
      </div></td>
      <td>=</td>
      <td>
%     if ($CONFIG->{$subject_style}->{SUBJECT}->[$i]->[$k]->{SELECT})
%     {
        <& '/lib/html/select.mhtml',
           'name'    => "SUBJECT_${i}_$k",
           'default' => $CONFIG->{$subject_style}->{SUBJECT}->[$i]->[$k]->{DEFAULT},
           'values'  => $CONFIG->{$subject_style}->{SUBJECT}->[$i]->[$k]->{SELECT} &>
%     } elsif (exists $CONFIG->{$subject_style}->{SUBJECT}->[$i]->[$k]->{VALUE}) {
        <& '/lib/html/hidden.mhtml',
           'name'  => "SUBJECT_${i}_$k",
           'value' => $CONFIG->{$subject_style}->{SUBJECT}->[$i]->[$k]->{VALUE} &>
        <& '/lib/html/safe.mhtml', 'data' => $CONFIG->{$subject_style}->{SUBJECT}->[$i]->[$k]->{VALUE} &>
%     } else {
        <& '/lib/html/input.mhtml',
           'name'  => "SUBJECT_${i}_$k",
           'value' => i18nGettext ($CONFIG->{$subject_style}->{SUBJECT}->[$i]->[$k]->{DEFAULT}),
           'max'   => $CONFIG->{$subject_style}->{SUBJECT}->[$i]->[$k]->{MAXIMUM},
           'min'   => $CONFIG->{$subject_style}->{SUBJECT}->[$i]->[$k]->{MINIMUM} &>
%     }
      </td>
%   }
    </tr>
% }
</table>

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

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

<%init>
    my @errors = ();
    my $ARGS   = {$m->request_args()};

    my $new_form = 1;
       $new_form = 0
           if (defined $subject or exists $ARGS->{"SUBJECT_0_0"});
    my $error_form = 0;
       $error_form = 1
           if (exists $ARGS->{"SUBJECT_0_0"});

    #####################################################
    ##    build a description for every form field     ##
    #####################################################

    my %descriptions = ();
    for (my $i=0; $i < scalar @{$CONFIG->{$subject_style}->{SUBJECT}}; $i++)
    {
        ## scan RDN $i
        for (my $k=0; $k < scalar @{$CONFIG->{$subject_style}->{SUBJECT}->[$i]}; $k++)
        {
            ## scan attribute $k of RDN $i
            $descriptions{i18nGettext ($CONFIG->{$subject_style}->{SUBJECT}->[$i]->[$k]->{LABEL})} =
                i18nGettext ($CONFIG->{$subject_style}->{SUBJECT}->[$i]->[$k]->{DESCRIPTION});
        }
    }
 
    ###########################################################
    ##     build a DN if some informations are available     ##
    ###########################################################

    if ($new_form or $error_form)
    {
        for (my $i=0; $i < scalar @{$CONFIG->{$subject_style}->{SUBJECT}}; $i++)
        {
            ## scan RDN $i
            for (my $k=0; $k < scalar @{$CONFIG->{$subject_style}->{SUBJECT}->[$i]}; $k++)
            {
                ## scan attribute $k of RDN $i

                ## build subject
                $subject .= "," if ($i > 0);
                $subject .= "+" if ($k > 0);
                $subject .= $CONFIG->{$subject_style}->{SUBJECT}->[$i]->[$k]->{NAME}.
                            "=".
                            $ARGS->{"SUBJECT_${i}_$k"};
            }
        }
    }

    #######################################
    ##     let it scan by the server     ##
    #######################################

    ## let's the server validate the subject
    if ($subject and not scalar @errors)
    {
        my $msg = $m->comp ('/service/create_csr/create_workflow.mhtml',
                            'workflow_type' => $workflow_type,
                            'role'          => $role,
                            'profile'       => $profile,
                            'subject_style' => $subject_style,
                            'keygen'        => $keygen,
                            'subject'       => $subject);
        ## perhaps we should put this into a component like get_error.mhtml?
        if (ref $msg and ref $msg eq "ARRAY" and
            $msg->[0]->{LABEL} ne 'I18N_OPENXPKI_SERVER_API_WORKFLOW_MISSING_REQUIRED_FIELDS')
        {
            ## this must be an error
            push @errors, @{$msg};
        }
    }

    ###############################
    ##     decide what to do     ##
    ###############################

    $subject = "" if (@errors);
    return $subject if (length $subject);

    ## ok we have to display the whole stuff
    @errors = () if ($new_form);
</%init>
