<script type="text/javascript">
jQuery( function () {
    jQuery('div.repeat input[name=repeat-enabled]').change( function () {
        var val = jQuery(this).val();
        if ( jQuery(this).is(':checked') ) {
            jQuery('div.repeat div.repeat-toggle').removeClass('hidden');
        }
        else {
            jQuery('div.repeat div.repeat-toggle').addClass('hidden');
        }
    } );

    jQuery('div.repeat input[name=repeat-type]').change( function () {
        var val = jQuery(this).val();
        jQuery('div.repeat .repeat-details:not(.repeat-details-'+val+')').addClass('hidden');
        jQuery('div.repeat .repeat-details-' +val ).removeClass('hidden');
    } );

    jQuery('div.repeat input[name=repeat-create-on-recurring-date]').change( function () {
        var val = jQuery(this).val();
        if ( val == 0 ) {
            jQuery('div#repeat-coexistent-number-div').removeClass('hidden');
            if ( jQuery('div.repeat input[name=repeat-coexistent-number]').val() == 0 ) {
              jQuery('div.repeat input[name=repeat-coexistent-number]').val( <% RT->Config->Get('RepeatTicketCoexistentNumber') || 1 %> );
            }
        }
        else {
            jQuery('div#repeat-coexistent-number-div').addClass('hidden');
            jQuery('div.repeat input[name=repeat-coexistent-number]').val(0);
        }
    } );

% if ( $ReadOnly ) {
    jQuery('div.repeat.read-only input, div.repeat.read-only select').attr('disabled', true);
% }
} );
</script>
<div class="repeat <% $ReadOnly ? 'read-only' : '' %>">

  <&| /Elements/LabeledValue, Label => '' &>
    <div class="form-check">
      <input type="checkbox" id="<% $InputIdPrefix %>repeat-enabled" name="repeat-enabled" class="form-check-input" value="1" <% $ARGSRef->{'repeat-enabled'} ? ' checked="checked"': '' |n %>>
      <label for="<% $InputIdPrefix %>repeat-enabled" class="form-check-label"><&|/l&>Enable Recurrence</&></label>
    </div>
  </&>

  <div class="repeat-toggle <% $ARGSRef->{'repeat-enabled'} ? '' : 'hidden' %> ">

    <&| /Elements/LabeledValue, Label => loc('Ticket Create Mode') &>
      <div class="form-check form-check-inline">
        <input type="radio" id="<% $InputIdPrefix %>repeat-create-on-recurring-date-true" name="repeat-create-on-recurring-date" class="form-check-input" value="1" <% $ARGSRef->{'repeat-create-on-recurring-date'} ? ' checked="checked"': '' |n %>>
        <label class="form-check-label" for="<% $InputIdPrefix %>repeat-create-on-recurring-date-true"><&|/l&>Simple Mode - Ticket created on Recurring Date</&></label>
      </div>

      <div class="form-check form-check-inline">
        <input type="radio" id="<% $InputIdPrefix %>repeat-create-on-recurring-date-false" name="repeat-create-on-recurring-date" class="form-check-input" value="0" <% $ARGSRef->{'repeat-create-on-recurring-date'} ? '' : ' checked="checked"' |n %>>
        <label class="form-check-label" for="<% $InputIdPrefix %>repeat-create-on-recurring-date-false"><&|/l&>Concurrent Tickets Mode - Ticket due on Recurring Date</&></label>
      </div>
    </&>

    <div id="repeat-coexistent-number-div" class="<% $ARGSRef->{'repeat-create-on-recurring-date'} ? 'hidden' : '' %>">
      <&| /Elements/LabeledValue, Label => loc('Concurrent active tickets') &>
        <input class="form-control" size="3" name="repeat-coexistent-number" type="text" value="<% $ARGSRef->{'repeat-coexistent-number'} %>" />
      </&>
    </div>

    <&| /Elements/LabeledValue, Label => loc('Ticket lead time (days)'), LabelTooltip => loc($lead_time_tooltip) &>
      <input class="form-control" size="3" name="repeat-lead-time" type="text" value="<% $ARGSRef->{'repeat-lead-time'} %>" />
    </&>

    <&| /Elements/LabeledValue, Label => loc('Recurrence pattern') &>
        <div class="form-check form-check-inline">
            <input type="radio" id="<% $InputIdPrefix %>repeat-type-daily" name="repeat-type" class="form-check-input" value="daily" <% ($ARGSRef->{'repeat-type'} || '') eq 'daily' ? ' checked="checked"': '' |n %>>
            <label class="form-check-label" for="<% $InputIdPrefix %>repeat-type-daily"><&|/l&>Daily</&></label>
        </div>

        <div class="form-check form-check-inline">
            <input type="radio" id="<% $InputIdPrefix %>repeat-type-weekly" name="repeat-type" class="form-check-input" value="weekly" <% ($ARGSRef->{'repeat-type'} || '') eq 'weekly' ? ' checked="checked"': '' |n %>>
            <label class="form-check-label" for="<% $InputIdPrefix %>repeat-type-weekly"><&|/l&>Weekly</&></label>
        </div>

        <div class="form-check form-check-inline">
            <input type="radio" id="<% $InputIdPrefix %>repeat-type-monthly" name="repeat-type" class="form-check-input" value="monthly" <% ($ARGSRef->{'repeat-type'} || '') eq 'monthly' ? ' checked="checked"': '' |n %>>
            <label class="form-check-label" for="<% $InputIdPrefix %>repeat-type-monthly"><&|/l&>Monthly</&></label>
        </div>

        <div class="form-check form-check-inline">
            <input type="radio" id="<% $InputIdPrefix %>repeat-type-yearly" name="repeat-type" class="form-check-input" value="yearly" <% ($ARGSRef->{'repeat-type'} || '') eq 'yearly' ? ' checked="checked"': '' |n %>>
            <label class="form-check-label" for="<% $InputIdPrefix %>repeat-type-yearly"><&|/l&>Yearly</&></label>
        </div>

        <div class="mt-3 repeat-details repeat-details-daily <% ($ARGSRef->{'repeat-type'} || '' ) eq 'daily' ? '' : 'hidden' %>">

          <div class="form-check has-text-input">
            <input type="radio" id="<% $InputIdPrefix %>repeat-details-daily-day" name="repeat-details-daily" class="form-check-input" value="day" <% ($ARGSRef->{'repeat-details-daily'} || '') eq 'day' ? ' checked="checked"': '' |n %>>
            <label class="form-check-label" for="<% $InputIdPrefix %>repeat-details-daily-day"><&|/l_unsafe, $input->("repeat-details-daily-day") &>Every [_1] Day(s)</&></label>
          </div>

          <div class="mt-1 form-check">
            <input type="radio" id="<% $InputIdPrefix %>repeat-details-daily-weekday" name="repeat-details-daily" class="form-check-input" value="weekday" <% ($ARGSRef->{'repeat-details-daily'} || '') eq 'weekday' ? ' checked="checked"': '' |n %>>
            <label class="form-check-label" for="<% $InputIdPrefix %>repeat-details-daily-weekday"><&|/l&>Every Weekday</&></label>
          </div>

          <div class="mt-1 form-check has-text-input">
            <input type="radio" id="<% $InputIdPrefix %>repeat-details-daily-complete" name="repeat-details-daily" class="form-check-input" value="complete" <% ($ARGSRef->{'repeat-details-daily'} || '') eq 'complete' ? ' checked="checked"': '' |n %>>
            <label class="form-check-label" for="<% $InputIdPrefix %>repeat-details-daily-complete"><&|/l_unsafe, $input->("repeat-details-daily-complete") &>Create new task [_1] day(s) after each task is completed</&></label>
          </div>

        </div>

        <div class="mt-3 repeat-details repeat-details-weekly <% ($ARGSRef->{'repeat-type'} || '' ) eq 'weekly' ? '' : 'hidden' %>">
          <div class="form-check has-text-input">
            <input type="radio" id="<% $InputIdPrefix %>repeat-details-weekly-week" name="repeat-details-weekly" class="form-check-input" value="week" <% ($ARGSRef->{'repeat-details-weekly'} || '') eq 'week' ? ' checked="checked"': '' |n %>>
            <label class="form-check-label" for="<% $InputIdPrefix %>repeat-details-weekly-week"><&|/l_unsafe, $input->("repeat-details-weekly-week") &>Recur every [_1] week(s) on</&>:</label>
          </div>

          <div class="ml-4">
%         for my $number ( 0 .. 6 ) {
            <div class="form-check form-check-inline">
              <div class="custom-control custom-checkbox">
                <input id="<% $InputIdPrefix %>repeat-details-weekly-weeks-<% $week_values[$number] %>" name="repeat-details-weekly-weeks" type="checkbox" class="form-check-input" value="<% $week_values[$number] %>"
%             if ( defined $ARGSRef->{'repeat-details-weekly-weeks'} && (
%               ( ref $ARGSRef->{'repeat-details-weekly-weeks'} && grep { $_ eq $week_values[$number] }
%               @{$ARGSRef->{'repeat-details-weekly-weeks'}} ) ||
%               ( $ARGSRef->{'repeat-details-weekly-weeks'} eq $week_values[$number] ) ) ) {
                  checked="checked"
%             }
                />
                <label class="form-check-label" for="<% $InputIdPrefix %>repeat-details-weekly-weeks-<% $week_values[$number] %>"><% loc($week_labels[$number]) %></label>
              </div>
            </div>
%       }
          </div>

          <div class="mt-1 form-check has-text-input">
            <input type="radio" id="<% $InputIdPrefix %>repeat-details-weekly-complete" name="repeat-details-weekly" class="form-check-input" value="complete" <% ($ARGSRef->{'repeat-details-weekly'} || '') eq 'complete' ? ' checked="checked"': '' |n %>>
            <label class="form-check-label" for="<% $InputIdPrefix %>repeat-details-weekly-complete"><&|/l_unsafe, $input->("repeat-details-weekly-complete") &>Create new task [_1] week(s) after each task is completed</&></label>
          </div>

        </div>

        <div class="mt-3 repeat-details repeat-details-monthly <% ($ARGSRef->{'repeat-type'} || '' ) eq 'monthly' ? '' : 'hidden' %>">
          <div class="form-check has-text-input">
            <input type="radio" id="<% $InputIdPrefix %>repeat-details-monthly-day" name="repeat-details-monthly" class="form-check-input" value="day" <% ($ARGSRef->{'repeat-details-monthly'} || '') eq 'day' ? ' checked="checked"': '' |n %>>
            <label class="form-check-label" for="<% $InputIdPrefix %>repeat-details-monthly-day"><&|/l_unsafe, $input->("repeat-details-monthly-day-day"), $input->("repeat-details-monthly-day-month") &>Day [_1] of every [_2] month(s)</&></label>
          </div>

          <div class="mt-1 form-check has-text-input">
            <input type="radio" id="<% $InputIdPrefix %>repeat-details-monthly-week" name="repeat-details-monthly" class="form-check-input" value="week" <% ($ARGSRef->{'repeat-details-monthly'} || '') eq 'week' ? ' checked="checked"': '' |n %>>
            <label class="form-check-label" for="<% $InputIdPrefix %>repeat-details-monthly-week"><&|/l_unsafe, $nth->("repeat-details-monthly-week-number"), $wday->("repeat-details-monthly-week-week"), $input->("repeat-details-monthly-week-month") &>The [_1] [_2] of every [_3] month(s)</&></label>
          </div>

          <div class="mt-1 form-check has-text-input">
            <input type="radio" id="<% $InputIdPrefix %>repeat-details-monthly-complete" name="repeat-details-monthly" class="form-check-input" value="complete" <% ($ARGSRef->{'repeat-details-monthly'} || '') eq 'complete' ? ' checked="checked"': '' |n %>>
            <label class="form-check-label" for="<% $InputIdPrefix %>repeat-details-monthly-complete"><&|/l_unsafe, $input->("repeat-details-monthly-complete") &>Create new task [_1] month(s) after each task is completed</&></label>
          </div>

        </div>

        <div class="mt-3 repeat-details repeat-details-yearly <% ($ARGSRef->{'repeat-type'} || '' ) eq 'yearly' ? '' : 'hidden' %>">

          <div class="form-check has-text-input">
            <input type="radio" id="<% $InputIdPrefix %>repeat-details-yearly-day" name="repeat-details-yearly" class="form-check-input" value="day" <% ($ARGSRef->{'repeat-details-yearly'} || '') eq 'day' ? ' checked="checked"': '' |n %>>
            <label class="form-check-label" for="<% $InputIdPrefix %>repeat-details-yearly-day"><&|/l_unsafe, $month->("repeat-details-yearly-day-month"), $input->("repeat-details-yearly-day-day") &>Every [_1] [_2]</&></label>
          </div>

          <div class="mt-1 form-check has-text-input">
            <input type="radio" id="<% $InputIdPrefix %>repeat-details-yearly-week" name="repeat-details-yearly" class="form-check-input" value="week" <% ($ARGSRef->{'repeat-details-yearly'} || '') eq 'week' ? ' checked="checked"': '' |n %>>
            <label class="form-check-label" for="<% $InputIdPrefix %>repeat-details-yearly-week"><&|/l_unsafe, $nth->("repeat-details-yearly-week-number"), $wday->("repeat-details-yearly-week-week"), $month->("repeat-details-yearly-week-month") &>The [_1] [_2] of [_3]</&></label>
          </div>

          <div class="mt-1 form-check has-text-input">
            <input type="radio" id="<% $InputIdPrefix %>repeat-details-yearly-complete" name="repeat-details-yearly" class="form-check-input" value="complete" <% ($ARGSRef->{'repeat-details-yearly'} || '') eq 'complete' ? ' checked="checked"': '' |n %>>
            <label class="form-check-label" for="<% $InputIdPrefix %>repeat-details-yearly-complete"><&|/l_unsafe, $input->("repeat-details-yearly-complete") &>Create new task [_1] year(s) after each task is completed</&></label>
          </div>

        </div>
    </&>

    <&| /Elements/LabeledValue, Label => loc('Start of recurrence'), &>
      <& /Elements/SelectDate, Name => 'repeat-start-date', id => "${InputIdPrefix}repeat-start-date_Date", ShowTime => 0, Default => $ARGSRef->{'repeat-start-date'} || '' &>
    </&>

    <&| /Elements/LabeledValue, Label => loc('End of recurrence'), &>
        <div class="form-check">
          <input type="radio" id="<% $InputIdPrefix %>repeat-end-none" name="repeat-end" class="form-check-input" value="none" <% ($ARGSRef->{'repeat-end'} || '') eq 'none' ? ' checked="checked"': '' |n %>>
          <label class="form-check-label" for="<% $InputIdPrefix %>repeat-end-none"><&|/l&>No end date</&></label>
        </div>

        <div class="mt-1 form-check has-text-input">
          <input type="radio" id="<% $InputIdPrefix %>repeat-end-number" name="repeat-end" class="form-check-input" value="number" <% ($ARGSRef->{'repeat-end'} || '') eq 'number' ? ' checked="checked"': '' |n %>>
          <label class="form-check-label" for="<% $InputIdPrefix %>repeat-end-number"><&|/l_unsafe, $input->("repeat-end-number",6,10) &>End after [_1] occurrence(s)</&>
%         if ( $Initial ) {
            <input name="repeat-occurrences" type="hidden" value="1">
%         } else {
            <&|/l&>Current</&>: <input size="6" type="text" name="repeat-occurrences" readonly="readonly" value="<% $ARGSRef->{'repeat-occurrences'} %>">
%         }
          </label>
        </div>

        <div class="mt-1 form-check has-text-input">
          <input type="radio" id="<% $InputIdPrefix %>repeat-end-type-date" name="repeat-end" class="form-check-input" value="date" <% ($ARGSRef->{'repeat-end'} || '') eq 'date' ? ' checked="checked"': '' |n %>>
          <label class="form-check-label" for="<% $InputIdPrefix %>repeat-end-type-date">
            <&|/l&>End by</&>: <& /Elements/SelectDate, Name => 'repeat-end-date', id => "${InputIdPrefix}repeat-end-date_Date", ShowTime => 0, Default => $ARGSRef->{'repeat-end-date'} || '' &>
          </label>
        </div>
    </&>
  </div>

  <div class="<% $ARGSRef->{'repeat-enabled'} && $ReadOnly && $preview_num ? '' : 'hidden' %>" id="tickets-preview">
    <&| /Widgets/TitleBox, title => loc("Recurrence Preview") &>
      <div class="row" style="text-align:left;">
        <p>This shows the next <% $preview_num %> tickets that would be created with the current recurrence configuration.</p>

        <p>If the recurrence configuration uses the concurrent active tickets option or the create new task after task is complete option the preview may not be accurate as it depends on whether previously created tickets are still active.</p>
      </div>
      <div class="row" style="text-align:center;">
        <div class="col-6">
          <span><% loc('Starts') . ( $ARGSRef->{'repeat-create-on-recurring-date'} ? ' - ' . loc('Recurring Date') : '' ) %></span>
        </div>
        <div class="col-6">
          <span><% loc('Due') . ( $ARGSRef->{'repeat-create-on-recurring-date'} ? '' : ' - ' . loc('Recurring Date') ) %></span>
        </div>
      </div>
% foreach my $preview ( @preview_tickets ) {
      <div class="row" style="text-align:center;">
        <div class="col-6">
          <span><% $preview->[0] %></span>
        </div>
        <div class="col-6">
          <span><% $preview->[1] %></span>
        </div>
      </div>
% }
    </&>
    </div>

</div>

<%init>
my @week_labels = qw(Sun Mon Tue Wed Thu Fri Sat); # loc_qw
my @week_values = qw(su mo tu we th fr sa);
my @week_number_labels = qw(First Second Third Fourth Last); # loc_qw
my @month_labels = qw(January February March April  May June July August September October November December); # loc_qw

my $lead_time_tooltip = loc('In simple mode, ticket will be due this many days after the recurring date. In concurrent mode, ticket will be created this many days before the recurring date.');

my $repeat;
if ( $Ticket ) {
    ($repeat) = $Ticket->Attributes->Named('RepeatTicketSettings');
    if ( $repeat ) {
        $ARGSRef = $repeat->Content if $repeat;
    }
}

if ( not $ARGSRef ){
     $Initial = 1;
}

$ARGSRef->{'repeat-type'} ||= 'daily';
$ARGSRef->{'repeat-details-daily'} ||= 'day';
$ARGSRef->{'repeat-details-weekly'} ||= 'week';
$ARGSRef->{'repeat-details-monthly'} ||= 'day';
$ARGSRef->{'repeat-details-yearly'} ||= 'day';
$ARGSRef->{'repeat-end'} ||= 'none';
$ARGSRef->{'repeat-lead-time'} //= RT->Config->Get('RepeatTicketLeadTime') // 14;
$ARGSRef->{'repeat-create-on-recurring-date'} //= 1;

if ( $ARGSRef->{'repeat-create-on-recurring-date'} ) {
    $ARGSRef->{'repeat-coexistent-number'} = 0;
}
else {
    # 0 is not a valid value for concurrent tickets mode
    if ( not $ARGSRef->{'repeat-coexistent-number'} ){
        $ARGSRef->{'repeat-coexistent-number'} = RT->Config->Get('RepeatTicketCoexistentNumber') || 1;
    }
}

my $input = sub {
    my ($name, $width, $default) = (@_);
    $width ||= 4; $default ||= 1;
    my $escaped = $m->interp->apply_escapes($name, "h");
    return qq|<input name="$escaped" type="text" size="$width" class="form-control" value="|
         . $m->interp->apply_escapes($ARGSRef->{$name} || $default, "h")
         . qq|" />|;
};

my $nth = sub {
    my ($name) = @_;
    my $escaped = $m->interp->apply_escapes($name, "h");
    my $str = qq|<select class="form-select selectpicker w-auto d-inline-block" name="$escaped">|;
    for my $number ( 1 .. 4, -1 ) {
        $str .= qq|<option value="$number" |
              . (($ARGSRef->{$name} || '') eq $number ? 'selected="selected"' : '') . ">"
              . $m->interp->apply_escapes(loc($week_number_labels[$number > 0 ? $number-1 : -1]), "h")
              . "</option>";
    }
    $str .= "</select>";
    return $str;
};
my $wday = sub {
    my ($name) = @_;
    my $escaped = $m->interp->apply_escapes($name, "h");
    my $str = qq|<select class="form-select selectpicker w-auto d-inline-block" name="$escaped">|;
    for my $number ( 0 .. 6 ) {
        $str .= qq|<option value="$week_values[$number]" |
              . (($ARGSRef->{$name} || '') eq $week_values[$number] ? 'selected="selected"' : '' ) . ">"
              . $m->interp->apply_escapes(loc($week_labels[$number]), "h")
              . "</option>";
    }
    $str .= "</select>";
    return $str;
};
my $month = sub {
    my ($name) = @_;
    my $escaped = $m->interp->apply_escapes($name, "h");
    my $str = qq|<select class="form-select selectpicker w-auto d-inline-block" name="$escaped">|;
    for my $number ( 1 .. 12 ) {
        $str .= qq|<option value="$number" |
              . (($ARGSRef->{$name} || '') eq $number ? 'selected="selected"' : '') . ">"
              . $m->interp->apply_escapes(loc($month_labels[$number-1]), "h")
              . "</option>";
    }
    $str .= "</select>";
    return $str;
};

my @preview_tickets;
my $preview_num = RT->Config->Get('RepeatTicketPreviewNumber') // 5;
unless ( $Initial ) {
  # find next X tickets to be created for preview window
    if ( $preview_num ) {
        RT::Extension::RepeatTicket::RepeatTicketPreview(1);

        my $content = $repeat->Content;

        my ( $last_due, $last_created );
        if ( $content->{'last-ticket'} ) {
            my $last_ticket = RT::Ticket->new( RT->SystemUser );
            $last_ticket->Load( $content->{'last-ticket'} );

            if ( $last_ticket->DueObj->Unix ) {
                $last_due = DateTime->from_epoch(
                    epoch     => $last_ticket->DueObj->Unix,
                    time_zone => RT->Config->Get('Timezone'),
                );
                $last_due->truncate( to => 'day' );
            }

            $last_created = DateTime->from_epoch(
                epoch     => $last_ticket->CreatedObj->Unix,
                time_zone => RT->Config->Get('Timezone'),
            );
            $last_created->truncate( to => 'day' );
        }
        my $today = DateTime->today( time_zone => RT->Config->Get('Timezone') );
        $today->truncate( to => 'day' );

        my $start_set_date;
        if ( $content->{'repeat-create-on-recurring-date'} ) {
            $start_set_date = $last_created || $today;
        }
        else {
            $start_set_date = $last_due || $last_created || $today;
        }

        my $set = RT::Extension::RepeatTicket::BuildSet( $content, $start_set_date );
        if ($set) {
            # use a counter to ensure we don't get an endless loop trying to find the next X tickets
            my $counter = 0;
            my $iter = $set->iterator;
            while ( my $dt = $iter->next ) {
                if ( !$content->{'repeat-create-on-recurring-date'} && $content->{'repeat-lead-time'} ) {
                    # the date to pass to Repeat must be the start/create date which in concurrent mode is lead time days in the past
                    $dt->add( days => -1 * $content->{'repeat-lead-time'} );
                }

                # skip dates in the past
                next if $dt->epoch < $today->epoch;

                push @preview_tickets, RT::Extension::RepeatTicket::Repeat( $repeat, $dt );
                $counter++;

                last if scalar @preview_tickets >= $preview_num || $counter > $preview_num * 2;
            }
        }

        RT::Extension::RepeatTicket::RepeatTicketPreview(0);
    }
}
</%init>
<%args>
$ARGSRef => undef
$Ticket => undef
$ReadOnly => undef
$Initial => undef
$InputIdPrefix => ''
</%args>

<%attr>
directly_accessible => 1
</%attr>
