<select name="<% $Name %>" <% $OnChange ? qq{onchange="$OnChange"} : '' |n %> class="select-problem form-control selectpicker">
% for my $option ( @options ) {
% if ( ref $option eq 'HASH' ) {
    <option <% $Default eq $option->{value} ? 'selected="selected"' : '' |n %> value="<% $option->{value} %>"><% $option->{label} %></option>
% } elsif ( $option ) {
    <optgroup label="<% $option %>">
% } else {
    </optgroup>
% }
% }
</select>

<%INIT>
my @options;
my $queues = RT::Queues->new( $session{'CurrentUser'} );
$queues->UnLimit;
while ( my $q = $queues->Next ) {
    next unless $q->CurrentUserHasRight("CreateTicket");
    my $cf = RT::CustomField->new( $session{'CurrentUser'} );
    $cf->LoadByName(
        Name          => 'Problem Type',
        LookupType    => RT::Ticket->CustomFieldLookupType,
        ObjectId      => $q->id,
        IncludeGlobal => 1,
    );
    if ( not $cf->Id or not $cf->Values->Count ) {
        push @options, { value => $q->Id . '-', label => $q->Name };
    }
    else {
        push @options, $q->Name;
        my $values = $cf->Values;
        while ( my $v = $values->Next ) {
            push @options, { value => $q->Id . "-" . $v->Name, label => $v->Name };
        }
        push @options, ''; # close optgroup
    }
}

</%INIT>

<%ARGS>
$Name => 'Problem'
$OnChange => ''
$Default => ''
</%ARGS>
