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

<& /service/open_form.mhtml, action => 'change_csr_subject_style.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_SUBJECT_STYLE_TITLE') %></h1>
<p>
  <% i18nGettext('I18N_OPENXPKI_CLIENT_HTML_MASON_ACTIVITY_CHANGE_CSR_SUBJECT_STYLE_DESCRIPTION') %>
</p>
<p>
<& /lib/html/select.mhtml,
   'name'    => "subject_style",
   'default' => [ $subject_style ],
   'values'  => %types &>
</p>
<& /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 @roles  = ();

    if (not $filled)
    {
        ## load the style from the workflow
        my $msg = $context->{client}->send_receive_command_msg
                  (
                      "get_workflow_info",
                      {
                          "WORKFLOW" => $type,
                          "ID"       => $id
                      }
                  );
        my $role       = $msg->{PARAMS}->{WORKFLOW}->{CONTEXT}->{cert_role};
        $subject_style = $msg->{PARAMS}->{WORKFLOW}->{CONTEXT}->{cert_subject_style};

        ## read all styles from the server
        my $msg = $context->{client}->send_receive_command_msg (
                       "get_cert_subject_profiles",
                       {PROFILE => $role});
        my %types = %{$msg->{PARAMS}};

        ## if there is only one option then there is no choice
        if (scalar (keys %types) == 1)
        {
            $subject_style = join "", keys %types;
        }
    }

    if ($filled or scalar (keys %types) == 1)
    {
        ## let it scan by the server
        my $msg = $context->{client}->send_receive_command_msg (
                      "execute_workflow_activity",
                      {WORKFLOW => $type,
                       ID       => $id,
                       ACTIVITY => "I18N_OPENXPKI_WF_ACTION_CHANGE_CSR_SUBJECT_STYLE",
                       PARAMS   => {"cert_subject_style" => $subject_style}});
        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 ask the user
    my %descriptions = ();
    foreach my $type (sort keys %types)
    {
        $descriptions{i18nGettext($types{$type}->{LABEL})} = 
            i18nGettext($types{$type}->{DESCRIPTION});
    }
</%init>
