<& /Admin/Elements/Header, Title => $title &>
<& /Elements/Tabs &>
<& /Elements/ListActions, actions => \@results &>

<&| /Widgets/TitleBox, title => '', class => 'container w-75' &>
<form action="<%RT->Config->Get('WebPath')%>/Admin/FormTools/Create.html" name="CreateFormTools" method="post" enctype="multipart/form-data" class="mx-auto max-width-sm">
  <&| /Elements/LabeledValue, Label => loc('Name') &>
    <input name="Description" class="form-control" value="<% $ARGS{Description} // '' %>"/>
  </&>

  <& /Elements/LabeledValue, Label => loc('Queue'), LabelTooltip => loc('Tickets will be created in this queue'),
                             RawValue => $m->scomp('/Elements/SelectQueue', Name => 'Queue', Default => $ARGS{Queue}) &>

% if ( RT->Config->Get('FormToolsEnableGroups') ) {
  <&| /Elements/LabeledValue, Label => loc('Group') &>
    <select name="Group" id="Group" class="form-control selectpicker">
      <option value=""><% loc('Select a group') %></option>
% foreach my $group ( @groups ) {
      <option value="<% $group %>" <% $group eq $Group ? 'selected' : '' %>><% $group %></option>
% }
    </select>
  </&>
% }

  <div class="form-row">
    <div class="col-12">
      <& /Elements/Submit, Label => loc('Create'), Name => 'Create' &>
    </div>
  </div>

</form>
</&>
<%INIT>
Abort( loc('Permission Denied') )
    unless $session{'CurrentUser'}->HasRight( Object => $RT::System, Right => 'AdminForm' );

my ($title, @results);
$title = loc('Create form');

my $groups = RT::Attributes->new( $session{'CurrentUser'} );
$groups->Limit( FIELD => 'Name', VALUE => 'FormTools Group' );
my ( @unsorted_groups, @groups );
while ( my $group = $groups->Next ) {
    my $content = $group->Content;
    next if $content->{disabled};
    push @unsorted_groups, $group->Description;
}
@groups = sort { $a cmp $b } @unsorted_groups;

if ( $Create ) {
    push @results, loc('Missing Name') unless $Description;
    push @results, loc('Missing Queue') unless $Queue;

    if ( RT->Config->Get('FormToolsEnableGroups') ) {
        push @results, loc('Missing Group') unless $Group;
    }

    if ( $Description && $Queue ) {

        if ( $Description =~ /^[\w\s]+$/ ) {

            my $form = RT::Attribute->new( $session{CurrentUser} );
            my ($ret) = $form->LoadByCols( Name => 'FormTools Form', Description => $Description );
            if ($ret) {
                push @results, loc( 'Name [_1] already exists', $Description );
            }

            my $queue_obj = RT::Queue->new( $session{CurrentUser} );
            $queue_obj->Load($Queue);
            if ( !$queue_obj->Id ) {
                push @results, loc( 'Invalid queue [_1]', $Queue );
            }

            # It indicates something wrong if @results is not empty
            if ( !@results ) {
                my ( $ret, $msg ) = $form->Create(
                    Name        => 'FormTools Form',
                    Description => $ARGS{Description},
                    Object      => RT->System,
                    Content     => {
                        queue => $queue_obj->Name,
                        group => $Group,
                    },
                );

                if ($ret) {
                    MaybeRedirectForResults(
                        Actions   => [ loc('Created form [_1]', $Description) ],
                        Path      => '/Admin/FormTools/Modify.html',
                        Arguments => { id => $form->Id },
                    );
                }
                else {
                    push @results, loc( "Couldn't create the form: [_1]", $msg );
                }
            }

        } else {
            push @results, loc("Invalid form name (can only contain letters, numbers, underscore, and spaces): [_1]", $Description);
        }
    }

    MaybeRedirectForResults(
        Actions   => \@results,
        Path      => '/Admin/FormTools/Create.html',
        Arguments => { Description => $Description, Queue => $Queue, Group => $Group },
    );
}
</%INIT>
<%ARGS>
$Description => undef
$Queue       => undef
$Create      => undef
$Group       => ''
</%ARGS>
