%# BEGIN BPS TAGGED BLOCK {{{
%#
%# COPYRIGHT:
%#
%# This software is Copyright (c) 1996-2024 Best Practical Solutions, LLC
%#                                          <sales@bestpractical.com>
%#
%# (Except where explicitly superseded by other copyright notices)
%#
%#
%# LICENSE:
%#
%# This work is made available to you under the terms of Version 2 of
%# the GNU General Public License. A copy of that license should have
%# been provided with this software, but in any event can be snarfed
%# from www.gnu.org.
%#
%# This work is distributed in the hope that it will be useful, but
%# WITHOUT ANY WARRANTY; without even the implied warranty of
%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%# General Public License for more details.
%#
%# You should have received a copy of the GNU General Public License
%# along with this program; if not, write to the Free Software
%# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
%# 02110-1301 or visit their web page on the internet at
%# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
%#
%#
%# CONTRIBUTION SUBMISSION POLICY:
%#
%# (The following paragraph is not intended to limit the rights granted
%# to you to modify and distribute this software under the terms of
%# the GNU General Public License and is only of importance to you if
%# you choose to contribute your changes and enhancements to the
%# community by submitting them to Best Practical Solutions, LLC.)
%#
%# By intentionally submitting any modifications, corrections or
%# derivatives to this work, or any other work intended for use with
%# Request Tracker, to Best Practical Solutions, LLC, you confirm that
%# you are the copyright holder for those contributions and you grant
%# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
%# royalty-free, perpetual, license to use, copy, create derivative
%# works based on those contributions, and sublicense and distribute
%# those contributions and any derivatives thereof.
%#
%# END BPS TAGGED BLOCK }}}
% foreach( @lines ) {
<& ConditionRow, Condition => $_ &>
% }
<%INIT>

my @lines;

if ( $Class eq 'RT::Transactions' ) {
    my $cfs = RT::CustomFields->new( $session{'CurrentUser'} );
    $cfs->LimitToLookupType( $ObjectType->CustomFieldLookupType );
    if ( %queues && $ObjectType eq 'RT::Ticket' ) {
        my @ids = 0;
        for my $name ( keys %queues ) {
            my $queue = RT::Queue->new( $session{CurrentUser} );
            $queue->Load($name);
            push @ids, $queue->id if $queue->id;
        }
        $cfs->LimitToAdded( @ids );
    }

    @lines = (
        {
            Name => 'id',
            Field => loc('id'),
            Op => {
                Type => 'component',
                Path => '/Elements/SelectEqualityOperator',
            },
            Value => { Type => 'text', Size => 5 }
        },
        {
            Name => 'Attachment',
            Field => {
                Type => 'component',
                Path => '/Elements/SelectAttachmentField',
            },
            Op => {
                Type => 'component',
                Path => '/Elements/SelectBoolean',
                Arguments => {
                    True => loc("matches"),
                    False => loc("doesn't match"),
                    TrueVal => 'LIKE',
                    FalseVal => 'NOT LIKE',
                },
            },
            Value => { Type => 'text', Size => 20 },
        },
        {
            Name => 'Creator',
            Field => loc('Creator'),
            Op => {
                Type => 'component',
                Path => '/Elements/SelectBoolean',
                Arguments => { TrueVal=> '=', FalseVal => '!=' },
            },
            Value => {
                Type => 'component',
                Path => '/Elements/EmailInput',
                Arguments => { AutocompleteReturn => 'Name' },
            },
        },
        {
            Name => 'Created',
            Field => loc('Created'),
            Op => {
                Type => 'component',
                Path => '/Elements/SelectDateRelation',
            },
            Value => {
                Type => 'component',
                Path => '/Elements/SelectDate',
                Arguments => { ShowTime => 0, Default => '' },
            },
        },
        {
            Name => 'TimeTaken',
            Field => loc('Time Taken'),
            Op => {
                Type => 'component',
                Path => '/Elements/SelectEqualityOperator',
            },
            Value => [
                {
                    Type => 'component',
                    Path => '/Elements/EditTimeValue',
                },
            ],
        },
        {
            Name => 'Type',
            Field => loc('Type'),
            Op => {
                Type => 'component',
                Path => '/Elements/SelectBoolean',
                Arguments => { TrueVal=> '=', FalseVal => '!=' },
            },
            Value => {
                Type    => 'select',
                Options => [
                    # The key could be ObjectType-Type-Field or Type-Field or Type.
                    # We just want Type
                    '' => '-',
                    map {  $_ => loc($_) } List::MoreUtils::uniq map { s/RT::.*?-//; s/-.*//; $_ } sort keys %RT::Transaction::_BriefDescriptions,
                ],
            },
        },
        {
            Name => 'Field',
            Field => loc('Field'),
            Op => {
                Type => 'component',
                Path => '/Elements/SelectMatch',
            },
            Value => { Type => 'text', Size => 5 }
        },
        {
            Name => 'OldValue',
            Field => loc('Old Value'),
            Op => {
                Type => 'component',
                Path => '/Elements/SelectMatch',
            },
            Value => { Type => 'text', Size => 5 }
        },
        {
            Name => 'NewValue',
            Field => loc('New Value'),
            Op => {
                Type => 'component',
                Path => '/Elements/SelectMatch',
            },
            Value => { Type => 'text', Size => 5 }
        },
        {
            Name => 'CFName',
            Field => loc('CF Name'),
            Op => {
                Type => 'component',
                Path => '/Elements/SelectBoolean',
                Arguments => { TrueVal=> '=', FalseVal => '!=' },
            },
            Value => {
                Type    => 'select',
                Options => [ '' => '-', map {  $_->Name => $_->Name } @{$cfs->ItemsArrayRef} ],
            },
        },
        {
            Name => 'OldCFValue',
            Field => loc('Old CF Value'),
            Op => {
                Type => 'component',
                Path => '/Elements/SelectMatch',
            },
            Value => { Type => 'text', Size => 5 }
        },
        {
            Name => 'NewCFValue',
            Field => loc('New CF Value'),
            Op => {
                Type => 'component',
                Path => '/Elements/SelectMatch',
            },
            Value => { Type => 'text', Size => 5 }
        },
    );
}
elsif ( $Class eq 'RT::Assets' ) {
    @lines = (
        {
            Name => 'id',
            Field => loc('id'),
            Op => {
                Type => 'component',
                Path => '/Elements/SelectEqualityOperator',
            },
            Value => { Type => 'text', Size => 5 }
        },
        {
            Name => 'Attachment',
            Field => {
                Type => 'component',
                Path => '/Elements/SelectAttachmentField',
                Arguments => { Class => 'RT::Assets' },
            },
            Op => {
                Type => 'component',
                Path => '/Elements/SelectBoolean',
                Arguments => {
                    True => loc("matches"),
                    False => loc("doesn't match"),
                    TrueVal => 'LIKE',
                    FalseVal => 'NOT LIKE',
                },
            },
            Value => { Type => 'text', Size => 20 },
        },
        {
            Name => 'Catalog',
            Field => loc('Catalog'),
            Op => {
                Type => 'component',
                Path => '/Elements/SelectBoolean',
                Arguments => { TrueVal=> '=', FalseVal => '!=' },
            },
            Value => {
                Type => 'component',
                Path => '/Asset/Elements/SelectCatalog',
                Arguments => { NamedValues => 1, ShowNullOption => 1, UpdateSession => 0, CheckRight => 'ShowAsset' },
            },
        },
        {
            Name => 'Status',
            Field => loc('Status'),
            Op => {
                Type => 'component',
                Path => '/Elements/SelectBoolean',
                Arguments => { TrueVal=> '=', FalseVal => '!=' },
            },
            Value => {
                Type => 'component',
                Path => '/Asset/Elements/SelectStatus',
                Arguments => { Catalogs => \%catalogs },
            },
        },
        {
            Name => 'Watcher',
            Field => {
                Type => 'component',
                Path => 'SelectPersonType',
                Arguments => { Default => 'Owner', Class => 'RT::Assets', Catalogs => \%catalogs },
            },
            Op => {
                Type => 'component',
                Path => '/Elements/SelectMatch',
            },
            Value => { Type => 'text', Size => 20 }
        },
        {
            Name => 'WatcherGroup',
            Field => {
                Type => 'component',
                Path => 'SelectPersonType',
                Arguments => { Default => 'Owner', Suffix => 'Group', Class => 'RT::Assets' },
            },
            Op => {
                Type => 'select',
                Options => [ '=' => loc('is') ],
            },
            Value => { Type => 'text', Size => 20, "data-autocomplete" => "Groups" }
        },
        {
            Name => 'Date',
            Field => {
                Type => 'component',
                Path => '/Elements/SelectDateType',
                Arguments => { Class => 'RT::Assets' },
            },
            Op => {
                Type => 'component',
                Path => '/Elements/SelectDateRelation',
            },
            Value => {
                Type => 'component',
                Path => '/Elements/SelectDate',
                Arguments => { ShowTime => 0, Default => '' },
            },
        },
        {
            Name => 'Links',
            Field => {
                Type => 'component',
                Path => 'SelectLinks',
            },
            Op => {
                Type => 'component',
                Path => '/Elements/SelectBoolean',
                Arguments => { TrueVal=> '=', FalseVal => '!=' },
            },
            Value => { Type => 'text', Size => 5 }
        },
    );
}
else {
    @lines = (
        {
            Name => 'id',
            Field => loc('id'),
            Op => {
                Type => 'component',
                Path => '/Elements/SelectEqualityOperator',
            },
            Value => { Type => 'text', Size => 5 }
        },
        {
            Name => 'Attachment',
            Field => {
                Type => 'component',
                Path => '/Elements/SelectAttachmentField',
            },
            Op => {
                Type => 'component',
                Path => '/Elements/SelectBoolean',
                Arguments => {
                    True => loc("matches"),
                    False => loc("doesn't match"),
                    TrueVal => 'LIKE',
                    FalseVal => 'NOT LIKE',
                },
            },
            Value => { Type => 'text', Size => 20 },
        },
        {
            Name => 'Queue',
            Field => loc('Queue'),
            Op => {
                Type => 'component',
                Path => '/Elements/SelectMatch',
                Arguments => { Default => '=' },
            },
            Value => {
                Type => 'component',
                Path => '/Elements/SelectQueue',
                Arguments => { NamedValues => 1, },
            },
        },
        {
            Name => 'Lifecycle',
            Field => loc('Lifecycle'),
            Op => {
                Type      => 'component',
                Path      => '/Elements/SelectBoolean',
                Arguments => { TrueVal => '=', FalseVal => '!=' },
            },
            Value => {
                Type => 'component',
                Path => '/Elements/SelectLifecycle',
            },
        },
        {
            Name => 'Status',
            Field => loc('Status'),
            Op => {
                Type => 'component',
                Path => '/Elements/SelectBoolean',
                Arguments => { TrueVal=> '=', FalseVal => '!=' },
            },
            Value => {
                Type => 'component',
                Path => '/Ticket/Elements/SelectStatus',
                Arguments => { SkipDeleted => 1, Queues => \%queues, ShowActiveInactive => 1 },
            },
        },
        {
            Name => 'SLA',
            Field => loc('SLA'),
            Op => {
                Type => 'component',
                Path => '/Elements/SelectMatch',
                Arguments => { Default => '=' },
            },
            Value => {
                Type => 'component',
                Path => '/Elements/SelectSLA',
                Arguments => { NamedValues => 1 },
            },
        },
        {
            Name => 'Actor',
            Field => {
                Type    => 'select',
                Options => [
                    Owner => loc('Owner'),
                    Creator => loc('Creator'),
                    LastUpdatedBy => loc('Last updated by'),
                    UpdatedBy => loc('Updated by'),
                ],
            },
            Op => {
                Type => 'component',
                Path => '/Elements/SelectBoolean',
                Arguments => { TrueVal=> '=', FalseVal => '!=' },
            },
            Value => {
                Type => 'component',
                Path => '/Elements/SelectOwner',
                Arguments => { ValueAttribute => 'Name', Queues => \%queues },
            },
        },
        {
            Name => 'Watcher',
            Field => {
                Type => 'component',
                Path => 'SelectPersonType',
                Arguments => { Default => 'Requestor', Queues => \%queues },
            },
            Op => {
                Type => 'component',
                Path => '/Elements/SelectMatch',
                Arguments => { IncludeShallow => 1 },
            },
            Value => { Type => 'text', Size => 20 }
        },
        {
            Name => 'WatcherGroup',
            Field => {
                Type => 'component',
                Path => 'SelectPersonType',
                Arguments => { Default => 'Owner', Suffix => 'Group' },
            },
            Op => {
                Type => 'select',
                Options => [ '=' => loc('is') ],
            },
            Value => { Type => 'text', Size => 20, "data-autocomplete" => "Groups" }
        },
        {
            Name => 'Date',
            Field => {
                Type => 'component',
                Path => '/Elements/SelectDateType',
            },
            Op => {
                Type => 'component',
                Path => '/Elements/SelectDateRelation',
            },
            Value => {
                Type => 'component',
                Path => '/Elements/SelectDate',
                Arguments => { ShowTime => 0, Default => '' },
            },
        },
        {
            Name => 'Time',
            Field => {
                Type    => 'select',
                Options => [
                    TimeWorked => loc('Time Worked'),
                    TimeEstimated => loc('Time Estimated'),
                    TimeLeft => loc('Time Left'),
                ],
            },
            Op => {
                Type => 'component',
                Path => '/Elements/SelectEqualityOperator',
            },
            Value => [
                {
                    Type => 'component',
                    Path => '/Elements/EditTimeValue',
                },
            ],
        },
        {
            Name => 'Priority',
            Field => {
                Type    => 'select',
                Options => [
                    Priority => loc('Priority'),
                    InitialPriority => loc('Initial Priority'),
                    FinalPriority => loc('Final Priority'),
                ],
            },
            Op => {
                Type => 'component',
                Path => '/Elements/SelectEqualityOperator',
            },
            Value => {
                Type => 'component',
                Path => '/Elements/SelectPriority',
                Arguments => { Queues => \%queues, ValueAsString => 1 },
            },
        },
        {
            Name => 'Links',
            Field => { Type => 'component', Path => 'SelectLinks' },
            Op => {
                Type => 'component',
                Path => '/Elements/SelectBoolean',
                Arguments => { TrueVal=> '=', FalseVal => '!=' },
            },
            Value => { Type => 'text', Size => 5 }
        },
    );
}

$m->callback( Conditions => \@lines );

</%INIT>

% if ( $Class =~ /^RT::(?:Tickets|Transactions)$/ ) {
<script type="text/javascript">
    jQuery(function() {

    // move the actual value to a hidden value, and shadow the others
% if ( $Class eq 'RT::Tickets' ) {
    var hidden = jQuery('<input>').attr('type','hidden').attr('name','ValueOfQueue');

    // change the selector's name, but preserve the values, we'll set value via js
    var selector = jQuery("[name='ValueOfQueue']");
% } else {
    var hidden = jQuery('<input>').attr('type','hidden').attr('name','ValueOfTicketQueue');
    var selector = jQuery("[name='ValueOfTicketQueue']");
% }

    // rename the selector so we don't get an extra term in the query
    selector[0].name = "";
    selector.bind('change',function() {
        hidden[0].value = selector[0].value;
    });

    // create a text input box and hide it for use with matches / doesn't match
    // NB: if you give text a name it will add an additional term to the query!
    var text = jQuery('<input>').attr('type','text').attr('class', 'form-control');
    text.hide();
    text.bind('change',function() {
        hidden[0].value = text[0].value;
    });

    // hook the op field so that we can swap between the two input types
% if ( $Class eq 'RT::Tickets' ) {
    var op = jQuery("[name='QueueOp']");
% } else {
    var op = jQuery("[name='TicketQueueOp']");
% }
    op.bind('change',function() {
        if (op[0].value == "=" || op[0].value == "!=" ) {
            text.hide();
            selector.show();
            hidden[0].value = selector[0].value;
        } else {
            text.show();
            selector.hide();
            hidden[0].value = text[0].value;
        }
    });

    // add the fields to the DOM
    selector.before(hidden);
    selector.after(text);
    });
</script>
% }
<%ARGS>
%queues => ()
%catalogs => ()
$Class => 'RT::Tickets'
$ObjectType => $Class eq 'RT::Transactions' ? 'RT::Ticket' : ''
</%ARGS>
