%# BEGIN BPS TAGGED BLOCK {{{
%#
%# COPYRIGHT:
%#
%# This software is Copyright (c) 1996-2025 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 }}}
<%perl>
for my $group ( @Groupings ) {
    my $CustomFields = $Object->CustomFields;
    $m->callback(
        Table        => 1, # default is true
        %ARGS,
        CallbackPage => '/Elements/ShowCustomFields',
        CallbackName => 'MassageCustomFields',
        Object       => $Object,
        CustomFields => $CustomFields,
    );

    $CustomFields->LimitToGrouping( $Object => $group );
    next unless $CustomFields->First;

    my $modify_url = $title_href ? "$title_href?id=".$Object->id.($group?";Grouping=".$m->interp->apply_escapes($group,'u')."#".CSSClass("$css_class-$group") : "#".$css_class) : undef;
    my $modify_inline
        = '<a hx-boost="false" class="inline-edit-toggle edit" href="'
        . $m->interp->apply_escapes( ( $modify_url || '#' ), 'h' ) . '">'
        . '<span class="rt-inline-icon border rounded">'
        . GetSVGImage( Name => 'pencil', Title => $edit_label )
        . '</span>'
        . '</a>'
        . '<a class="inline-edit-toggle cancel hidden" href="#">'
        . '<span class="rt-inline-icon border rounded">'
        . GetSVGImage( Name => 'close', Title => $cancel_label )
        . '</span>'
        . '</a>';
    my $modify_behavior = $InlineEdit ? ($inline_edit_behavior{$group} || $inline_edit_behavior{_default} || 'link') : 'hide';
    my @class = $css_class;
    push @class, CSSClass("$css_class-$group") if $group;
    push @class, 'editing' if $modify_behavior eq 'always';

    my %grouping_args = (
        title => $group? loc($group) : loc('Custom Fields'),
        class => (join " ", @class),
        hide_empty => 1,
        title_href => $modify_url,
        ($modify_behavior =~ /^(link|click)$/ ? (titleright_raw => $modify_inline) : ()),
        data => { 'inline-edit-behavior' => $modify_behavior },
        %$TitleBoxARGS,
    );
    $m->callback( CallbackName => 'TitleBox', Object => $Object, Grouping => $group, ARGSRef => \%grouping_args );
</%perl>

<div class="<% $ARGS{'GroupingClass'} ? $ARGS{'GroupingClass'} : '' %>">

<&| /Widgets/TitleBox, %grouping_args &>
% unless ($modify_behavior eq 'always') {
  <div class="inline-edit-display show-custom-fields-container" hx-trigger="none" hx-get="<% RT->Config->Get('WebPath') %>/Views/Component/ShowCustomFields?ObjectType=<% ref $Object %>&ObjectId=<% $Object->Id %>&Grouping=<% $group %>">
    <& ShowCustomFields, %ARGS, Object => $Object, Grouping => $group &>
  </div>
% }
% if ($modify_behavior ne 'hide') {
  <form class="inline-edit" hx-post="<% $ActionURL %>" hx-swap="none" enctype="multipart/form-data">
    <input type="hidden" class="hidden" name="id" value="<% $Object->id %>" />
    <div class="edit-custom-fields-container" hx-trigger="none" hx-get="<% RT->Config->Get('WebPath') %>/Views/Component/EditCustomFields?ObjectType=<% ref $Object %>&ObjectId=<% $Object->Id %>&Grouping=<% $group %>&InTable=0" hx-swap="innerHTML">
      <& /Elements/EditCustomFields, Object => $Object, Grouping => $group, InTable => 0 &>
    </div>
    <div class="row mt-2">
      <div class="col-12 text-end">
        <input type="submit" class="btn btn-primary" value="<&|/l&>Save</&>" />
      </div>
    </div>
    <& /Widgets/Spinner &>
  </form>
% }
% $m->callback( %ARGS, CallbackName => 'EndOfWidget', Object => $Object, Grouping => $group );
</&>
</div>
% }
<%ARGS>
$Object
$title_href => ""
$InlineEdit => 0
@Groupings  => ()
$ActionURL  => RT->Config->Get('WebPath') . '/Helpers/TicketUpdate'
</%ARGS>
<%INIT>
my $css_class = lc(ref($Object)||$Object);
$css_class =~ s/^rt:://;
$css_class =~ s/::/-/g;
$css_class = CSSClass($css_class);
$css_class .= '-info-cfs';

my $TitleBoxARGS = delete $ARGS{TitleBoxARGS} || {};

$InlineEdit = 0 unless $Object->isa('RT::Ticket') || $Object->isa('RT::Asset');
my %inline_edit_behavior;
if ( my $config = RT->Config->Get('InlineEditPanelBehavior') ) {
    %inline_edit_behavior = %{ $config->{ (RT::CustomField->_GroupingClass($Object))[0] } || $config->{'RT::Ticket'} || {} };
}

my $edit_label   = $m->interp->apply_escapes( loc("Edit"), 'h' );
my $cancel_label = $m->interp->apply_escapes( loc("Cancel"), 'h' );

if ( @Groupings ) {
    my %valid = map { $_ => 1 } RT::CustomField->Groupings( $Object ), '';
    if ( my @invalid_groups = grep { !$valid{$_} } @Groupings ) {
        RT->Logger->warning(
            "Invalid custom field groupings for " . ref($Object) . '#' . $Object->Id . ': ' . join ', ',
            @invalid_groups );
        push @{ $m->notes->{'HXUserWarnings'} ||= [] },
            loc( "Some custom field groupings were not found: [_1]" , join ', ', @invalid_groups )
                . ". " . loc("Contact your RT administrator.");
    }
    @Groupings = grep { $valid{$_} } @Groupings;
}
else {
    @Groupings = ( RT::CustomField->CustomGroupings( $Object ), '' );
}
</%INIT>
