%# 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 }}}
% $m->callback(CallbackName => 'BeforeCc', ARGSRef => \%ARGS, Ticket => $TicketObj, one_time_Ccs => \@one_time_Ccs, txn_addresses => \%txn_addresses);

<div class="form-row">
  <div class="label col-2">
    <&|/l&>One-time Cc</&>:
  </div>
  <div class="value col-9">
    <& /Elements/EmailInput, Name => 'UpdateCc', Size => undef, Default => $ARGS{UpdateCc}, AutocompleteMultiple => 1, Options => \@one_time_Ccs &>
    <input type="hidden" id="UpdateIgnoreAddressCheckboxes" name="UpdateIgnoreAddressCheckboxes" value="<% $ARGS{UpdateIgnoreAddressCheckboxes} || 0 %>">
  </div>
</div>

%if (scalar @one_time_Ccs) {
<div class="form-row mt-0">
  <div class="label col-2"></div>
  <div class="value col-9">
    <div class="form-row">
%   if ($hide_cc_suggestions) {
      <a href="#" class="ToggleSuggestions" data-hide-label="<% $hide_label %>" data-show-label="<% $show_label %>">
        <i class="label">(<&|/l&>show suggestions</&>)</i>
      </a>
      <div class="OneTimeCcs d-flex flex-wrap hidden">
%   }

      <div class="col-auto"><span class="far fa-question-circle icon-helper" data-toggle="tooltip" data-placement="top" data-original-title="<&|/l&>check to add</&>"></span></div>
      <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" id="AllSuggestedCc" name="AllSuggestedCc" value="1" onclick="setCheckbox(this, /^UpdateCc-/); checkboxesToInput('UpdateCc', 'input[name^=UpdateCc-]:checkbox');">
        <label class="custom-control-label" for="AllSuggestedCc"><% loc('All recipients') %></label>
      </div>
%   foreach my $addr ( map $_->{value}, @one_time_Ccs ) {
      <div class="col-auto">
        <span class="ticket-update-suggested-cc">
          <div class="custom-control custom-checkbox">
            <input
              id="UpdateCc-<%$addr%>"
              name="UpdateCc-<%$addr%>"
              class="onetime onetimecc custom-control-input"
              type="checkbox"
              autocomplete="off"
              data-address="<% $addr %>"
              onClick="checkboxToInput('UpdateCc', <% "UpdateCc-$addr" |n,j%>, <%$addr|n,j%> );"
              value="<% $txn_addresses{$addr}->format %>"
              <% $ARGS{'UpdateCc-'.$addr} ? 'checked="checked"' : ''%> >
            <label class="custom-control-label" for="UpdateCc-<%$addr%>"><& /Elements/ShowUser, Address => $txn_addresses{$addr}&></label>
          </div>
        </span>
      </div>
%   }
%   if ($hide_cc_suggestions) {
      </div>
%   }
    </div>
  </div>
</div>
%}

<div class="form-row">
  <div class="label col-2">
    <&|/l&>One-time Bcc</&>:
  </div>
  <div class="value col-9">
    <& /Elements/EmailInput, Name => 'UpdateBcc', Size => undef, Default => $ARGS{UpdateBcc}, AutocompleteMultiple => 1, Options => \@one_time_Ccs &>
  </div>
</div>

%if (scalar @one_time_Ccs) {
<div class="form-row mt-0">
  <div class="label col-2"></div>
  <div class="value col-9">
    <div class="form-row">
%   if ($hide_cc_suggestions) {
      <a href="#" class="ToggleSuggestions" data-hide-label="<% $hide_label %>" data-show-label="<% $show_label %>">
        <i class="label">(<&|/l&>show suggestions</&>)</i>
      </a>
      <div class="OneTimeCcs d-flex flex-wrap hidden">
%   }

      <div class="col-auto"><span class="far fa-question-circle icon-helper" data-toggle="tooltip" data-placement="top" data-original-title="<&|/l&>check to add</&>"></span></div>
      <div class="custom-control custom-checkbox">
        <input type="checkbox" class="checkbox custom-control-input" id="AllSuggestedBcc" name="AllSuggestedBcc" value="1" onclick="setCheckbox(this, /^UpdateBcc-/); checkboxesToInput('UpdateBcc', 'input[name^=UpdateBcc-]:checkbox');">
        <label class="custom-control-label" for="AllSuggestedBcc"><% loc('All recipients') %></label>
      </div>
%   foreach my $addr ( map $_->{value}, @one_time_Ccs ) {
      <div class="col-auto">
        <span class="ticket-update-suggested-cc">
          <div class="custom-control custom-checkbox">
            <input
              id="UpdateBcc-<%$addr%>"
              name="UpdateBcc-<%$addr%>"
              class="onetime onetimebcc custom-control-input"
              type="checkbox"
              autocomplete="off"
              data-address="<% $addr %>"
              onClick="checkboxToInput('UpdateBcc', <% "UpdateBcc-$addr" |n,j%>, <%$addr|n,j%> );"
              value="<% $txn_addresses{$addr}->format %>"
              <% $ARGS{'UpdateBcc-'.$addr} ? 'checked="checked"' : ''%> >
            <label class="custom-control-label" for="UpdateBcc-<%$addr%>"><& /Elements/ShowUser, Address => $txn_addresses{$addr}&></label>
          </div>
        </span>
      </div>
%   }
%   if ($hide_cc_suggestions) {
      </div>
<script type="text/javascript">
jQuery(function() {
    jQuery('a.ToggleSuggestions').click(function(e) {
        e.preventDefault();
        var toggleSuggestions = jQuery(this);
        var oneTimeCcs = toggleSuggestions.closest('div').find('.OneTimeCcs');
        oneTimeCcs.toggleClass('hidden');
        var hideOrShow = oneTimeCcs.hasClass('hidden') ? toggleSuggestions.data('showLabel') : toggleSuggestions.data('hideLabel');
        toggleSuggestions.find('i').html('(' + hideOrShow + ')');
    });
});
</script>
%   }
    </div>
  </div>
</div>
%}

<%args>
$TicketObj
</%args>
<%init>
my %txn_addresses = %{$TicketObj->TransactionAddresses};

# Get people already added as watchers on the ticket so we can filter
# them out of the one-time list
my @people_addresses = Email::Address->parse( $TicketObj->RequestorAddresses );
push @people_addresses, Email::Address->parse( $TicketObj->CcAddresses );
push @people_addresses, Email::Address->parse( $TicketObj->AdminCcAddresses );
if ( my $owner_email = $TicketObj->OwnerObj->EmailAddress ) {
    push @people_addresses, Email::Address->parse( $owner_email );
}

my @one_time_Ccs;

foreach my $addr ( keys %txn_addresses) {
  next if ( grep {$addr eq lc $_->address} @people_addresses );
  push @one_time_Ccs, { label => $txn_addresses{$addr}->format, value => $addr };
}

@one_time_Ccs = sort { lc $a->{label} cmp lc $b->{label} } @one_time_Ccs;

my $hide_cc_suggestions =  RT->Config->Get('HideOneTimeSuggestions', $session{CurrentUser});
my $show_label    = $m->interp->apply_escapes( loc("show suggestions"), 'h' );
my $hide_label    = $m->interp->apply_escapes( loc("hide suggestions"), 'h' );
</%init>
