%# 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 }}}
<%args>
$Ticket => undef
$id => undef
$ShowCompleted => 0
$Edit => 0
$ShowSave => 1
</%args>
<%init>

$Ticket = LoadTicket($id) if ($id);
my $resolve_status = $Ticket->LifecycleObj->ReminderStatusOnResolve;

my $count_reminders = RT::Reminders->new($session{'CurrentUser'});
$count_reminders->Ticket($Ticket->id);
my $count_tickets = $count_reminders->Collection;
if (!$ShowCompleted) {
    # XXX: don't break encapsulation if we can avoid it
    $count_tickets->FromSQL(q{Type = "reminder" AND RefersTo = "} .  $Ticket->id . qq{" AND Status != "$resolve_status" });
}
my $has_reminders = $count_tickets->Count;

# We've made changes, let's reload our search
my $reminder_collection = $count_reminders->Collection;
</%init>
<input type="hidden" class="hidden" name="id" value="<% $Ticket->id %>" />
<input type="hidden" class="hidden" name="update-reminders" value="1" />
% my $editable = 0;
% if ($has_reminders) {
<div class="form-row reminder-headers p-2 mt-0">
% if ( $Edit ) {
  <&|/l&>Reminders</&>
% } else {
  <div class="col-4">
    <&|/l&>Reminder</&>
  </div>
  <div class="col-4">
    <&|/l&>Due</&>
  </div>
  <div class="col-4">
    <&|/l&>Owner</&>
  </div>
% }
</div>
% my $i = 0;
% while ( my $reminder = $reminder_collection->Next ) {
<div class="form-row mt-0">
% $i++;
% if ( $reminder->Status eq $resolve_status && !$ShowCompleted ) {
  <input type="hidden" class="hidden" name="Complete-Reminder-<% $reminder->id %>" value="1" />
% $i++;
% }
% else {
%   $editable = 1 if !$editable && $reminder->CurrentUserHasRight( 'ModifyTicket' );
%   if ($Edit) {
<& SELF:EditEntry, Reminder => $reminder, Ticket => $Ticket, Index => $i &>
%   } else {
<& SELF:ShowEntry, Reminder => $reminder, Ticket => $Ticket, Index => $i &>
%   }
% }
</div>
% }
% if ( $editable ) {
<div class="form-row mt-0">
  <i class="mt-2 ml-2"><&|/l&>(Check box to complete)</&></i>
</div>
% }
<hr class="mt-2 mb-2">
% } else {

%# we must always include resolved reminders due to the browser
%# checkbox-with-false-value issue
% while ( my $reminder = $reminder_collection->Next ) {
% if ( $reminder->Status eq $resolve_status && !$ShowCompleted ) {
<input type="hidden" class="hidden" name="Complete-Reminder-<% $reminder->id %>" value="1" />
% }
% }
% }

% if (lc $Ticket->Status ne "deleted" and $Ticket->QueueObj->CurrentUserHasRight('CreateTicket') and $Ticket->CurrentUserHasRight('ModifyTicket') ) {
<& SELF:NewReminder, Ticket => $Ticket &>
% $editable = 1;
% }

% if ( $editable && $ShowSave ) {
<div class="form-row mt-0">
  <div class="col-12">
    <div class="save"><input type="submit" class="button form-control btn btn-primary" value="<&|/l&>Save</&>" /></div>
  </div>
</div>
% }
<%method NewReminder>
<%args>
$Ticket
</%args>
<div class="form-row">
  <div class="col-12">
    <&|/l&>New reminder:</&>
    <div class="form-row">
      <div class="label col-2">
        <&|/l&>Subject</&>:
      </div>
      <div class="value col-9">
        <input type="text" class="form-control reminder-subject" name="NewReminder-Subject" id="NewReminder-Subject" />
      </div>
    </div>
    <div class="form-row">
      <div class="label col-2">
        <&|/l&>Owner</&>:
      </div>
      <div class="value col-9">
        <div class="selectowner">
          <& /Elements/SelectOwner, Name => 'NewReminder-Owner', QueueObj => $Ticket->QueueObj, Default=>$session{'CurrentUser'}->id, DefaultValue => 0 &>
        </div>
      </div>
    </div>
    <div class="form-row">
      <div class="label col-2">
        <&|/l&>Due</&>:
      </div>
      <div class="value col-9">
        <& /Elements/SelectDate, Name => "NewReminder-Due", Default => "" &>
      </div>
    </div>
  </div>
</div>
</%method>
<%method EditEntry>
<%args>
$Reminder
$Ticket
$Index
</%args>

  <div class="col-12 <% $Index%2 ? 'oddline' : 'evenline' %>" id="reminder-<% $Reminder->id %>">
% unless ( $Reminder->CurrentUserHasRight('ModifyTicket') ) {
      <input class="custom-control-input" name="Complete-Reminder-<% $Reminder->id %>" type="hidden"
        value=<% $Reminder->Status eq $Reminder->LifecycleObj->ReminderStatusOnResolve ? 1 : 0 %>
      />
% }
      <div class="form-row m-0 pb-2 pt-2">
      <div class="col-1">
        <div class="custom-control custom-checkbox">
          <input class="custom-control-input" type="checkbox" value="1" id="Complete-Reminder-<% $Reminder->id %>" name="Complete-Reminder-<% $Reminder->id %>" <% $Reminder->Status eq $Reminder->LifecycleObj->ReminderStatusOnResolve ? 'checked="checked"' : '' |n %>
% unless ( $Reminder->CurrentUserHasRight('ModifyTicket') ) {
disabled="disabled"
% }
          />
          <label class="custom-control-label" for="Complete-Reminder-<% $Reminder->id %>"></label>
        </div>
      </div>
        <div class="label col-1">
          <&|/l&>Subject</&>:
        </div>
        <div class="value col-9">
          <input type="text" class="reminder-subject form-control" name="Reminder-Subject-<% $Reminder->id %>" value="<% $Reminder->Subject %>"
% unless ( $Reminder->CurrentUserHasRight('ModifyTicket') ) {
readonly="readonly"
% }
          />
        </div>
      </div>
      <div class="form-row m-0 pb-2">
        <div class="label col-2">
          <&|/l&>Owner</&>:
        </div>
        <div class="value col-9">
          <div class="row">
            <div class="col-auto">
              <div class="selectowner">
                <& /Elements/SelectOwner, Name => 'Reminder-Owner-'.$Reminder->id, QueueObj => $Ticket->QueueObj, Default => $Reminder->Owner, DefaultValue => 0  &>
              </div>
            </div>
            <div class="label col-auto">
              <&|/l&>Due</&>:
            </div>
            <div class="value">
% if ( $Reminder->CurrentUserHasRight('ModifyTicket') ) {
              <div class="float-left">
                <& /Elements/SelectDate, Name => 'Reminder-Due-'.$Reminder->id &>
              </div>
              <div class="float-left ml-3">
                <span class="current-value form-control">(<% $Reminder->DueObj->AsString %>)</span>
              </div>
% }
            </div>
          </div>
        </div>
      </div>

    </div>
</%method>
<%method ShowEntry>
<%args>
$Reminder
$Ticket
$Index
</%args>
% my $dueobj = $Reminder->DueObj;
% my $overdue = $dueobj->IsSet && $dueobj->Diff < 0 ? 1 : 0;

<div class="col-12 <% $Index%2 ? 'oddline' : 'evenline' %>" id="reminder-<% $Reminder->id %>">
  <div class="form-row m-0 pb-2 pt-2">
% unless ( $Reminder->CurrentUserHasRight('ModifyTicket') ) {
    <div class="col-4">
      <input name="Complete-Reminder-<% $Reminder->id %>" type="hidden"
        value=<% $Reminder->Status eq $Reminder->LifecycleObj->ReminderStatusOnResolve ? 1 : 0 %>
      />
    </div>
% }
    <div class="col-4">
      <div class="custom-control custom-checkbox">
        <input class="custom-control-input" type="checkbox" value="1" id="Complete-Reminder-<% $Reminder->id %>" name="Complete-Reminder-<% $Reminder->id %>" <% $Reminder->Status eq $Reminder->LifecycleObj->ReminderStatusOnResolve ? 'checked="checked"' : '' |n %>
% unless ( $Reminder->CurrentUserHasRight('ModifyTicket') ) {
          disabled="disabled"
% }
        />
        <label class="custom-control-label" for="Complete-Reminder-<% $Reminder->id %>"><% $Reminder->Subject %></label>
      </div>
    </div>

    <div class="col-4">
      <% $overdue ? '<span class="overdue">' : '' |n %><% $dueobj->AgeAsString || loc('Not set') %><% $overdue ? '</span>' : '' |n %>
    </div>
    <div class="col-4">
      <& /Elements/ShowUser, User => $Reminder->OwnerObj &>
    </div>
  </div>
</div>
</%method>
