<%args>
    $workflow_type => undef
    $role          => undef
    $profile       => undef
    $subject_style => undef
    $keygen        => undef
    $pkcs10        => undef
    $subject       => undef
    $subject_alt_name => 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 &>
<& /lib/html/hidden.mhtml, 'name' => 'subject',       'value' => $subject &>
<& /lib/html/hidden.mhtml, 'name' => 'FILLED_SUBJECT_ALT_NAME_FORM', 'value' => 1 &>

<h1><% i18nGettext ('I18N_OPENXPKI_CLIENT_HTML_MASON_CREATE_CSR_GET_SUBJECT_ALT_NAME_TITLE') %></h1>
<p>
  <% i18nGettext('I18N_OPENXPKI_CLIENT_HTML_MASON_CREATE_CSR_GET_SUBJECT_ALT_NAME_DESCRIPTION') %>
</p>

<table>
% for (my $i=0; $i < scalar @{$CONFIG->{$subject_style}->{SUBJECT_ALT_NAME}}; $i++)
% {
    <tr>
      <td>
        <& /lib/html/select.mhtml,
           'name'    => "SUBJECT_ALT_NAME_${i}_TYPE",
           'default' => [ $CONFIG->{$subject_style}->{SUBJECT_ALT_NAME}->[$i] ],
           'values'  => \@values &>
      </td>
      <td>
        <& /lib/html/input.mhtml, 'name' => "SUBJECT_ALT_NAME_${i}_VALUE" &>
      </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()};

    # try to build subject alt name
    if (not defined $subject_alt_name)
    {
        my $range = scalar @{$CONFIG->{$subject_style}->{SUBJECT_ALT_NAME}};
        my @list  = ();
        for (my $i=0; $i < $range; $i++)
        {
            ## check the field is filled
            next if (not exists $ARGS->{"SUBJECT_ALT_NAME_${i}_TYPE"});
            next if (not length $ARGS->{"SUBJECT_ALT_NAME_${i}_TYPE"});
            next if (not exists $ARGS->{"SUBJECT_ALT_NAME_${i}_VALUE"});
            next if (not length $ARGS->{"SUBJECT_ALT_NAME_${i}_VALUE"});
            my $type  = $ARGS->{"SUBJECT_ALT_NAME_${i}_TYPE"};
            my $value = $ARGS->{"SUBJECT_ALT_NAME_${i}_VALUE"};

            ## accept the value
            push @list, [ $type, $value ];
        }
        if ($ARGS->{"FILLED_SUBJECT_ALT_NAME_FORM"})
        {
            ## warnings we must be safe against \n truncation
            my $serializer = OpenXPKI::Serialization::Simple->new({SEPARATOR => "-"});
            $subject_alt_name = $serializer->serialize(\@list);
        }
    }

    ## now validate the input
    if (defined $subject_alt_name)
    {
        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,
                            'subject_alt_name' => $subject_alt_name);
        ## 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};
        }
    }
    return $subject_alt_name if (not @errors and defined $subject_alt_name);

    ## build hash for select fields and descriptions
    my %descriptions = ();
    my @values       = ();
    my @raw          = ("email", "DNS", "IP", "URI", "DirName", "GUID", "UPN", "RID");

    foreach my $key (@raw)
    {
        my $label = "I18N_OPENXPKI_CLIENT_HTML_MASON_CREATE_CSR_SUBJECT_ALT_NAME_".uc($key);
        $descriptions{i18nGettext($label)} = i18nGettext($label."_DESCRIPTION");
        push @values, {LABEL => i18nGettext($label), VALUE => $key};
    }

    ## ok we have to display the whole stuff
</%init>
<%once>
    ## fields for i18n scan

    ## I18N_OPENXPKI_CLIENT_HTML_MASON_CREATE_CSR_SUBJECT_ALT_NAME_EMAIL
    ## I18N_OPENXPKI_CLIENT_HTML_MASON_CREATE_CSR_SUBJECT_ALT_NAME_DNS
    ## I18N_OPENXPKI_CLIENT_HTML_MASON_CREATE_CSR_SUBJECT_ALT_NAME_IP
    ## I18N_OPENXPKI_CLIENT_HTML_MASON_CREATE_CSR_SUBJECT_ALT_NAME_URI
    ## I18N_OPENXPKI_CLIENT_HTML_MASON_CREATE_CSR_SUBJECT_ALT_NAME_DIRNAME
    ## I18N_OPENXPKI_CLIENT_HTML_MASON_CREATE_CSR_SUBJECT_ALT_NAME_GUID
    ## I18N_OPENXPKI_CLIENT_HTML_MASON_CREATE_CSR_SUBJECT_ALT_NAME_UPN
    ## I18N_OPENXPKI_CLIENT_HTML_MASON_CREATE_CSR_SUBJECT_ALT_NAME_RID

    ## I18N_OPENXPKI_CLIENT_HTML_MASON_CREATE_CSR_SUBJECT_ALT_NAME_EMAIL_DESCRIPTION
    ## I18N_OPENXPKI_CLIENT_HTML_MASON_CREATE_CSR_SUBJECT_ALT_NAME_DNS_DESCRIPTION
    ## I18N_OPENXPKI_CLIENT_HTML_MASON_CREATE_CSR_SUBJECT_ALT_NAME_IP_DESCRIPTION
    ## I18N_OPENXPKI_CLIENT_HTML_MASON_CREATE_CSR_SUBJECT_ALT_NAME_URI_DESCRIPTION
    ## I18N_OPENXPKI_CLIENT_HTML_MASON_CREATE_CSR_SUBJECT_ALT_NAME_DIRNAME_DESCRIPTION
    ## I18N_OPENXPKI_CLIENT_HTML_MASON_CREATE_CSR_SUBJECT_ALT_NAME_GUID_DESCRIPTION
    ## I18N_OPENXPKI_CLIENT_HTML_MASON_CREATE_CSR_SUBJECT_ALT_NAME_UPN_DESCRIPTION
    ## I18N_OPENXPKI_CLIENT_HTML_MASON_CREATE_CSR_SUBJECT_ALT_NAME_RID_DESCRIPTION
</%once>
