<%init>

my $time_worked;
if ( $TicketObj->TimeWorked ) {
    $time_worked = {};
    my $transactions = $TicketObj->Transactions;
    $transactions->Limit(
        FIELD           => 'TimeTaken',
        VALUE           => 0,
        OPERATOR        => '!=',
    );

    while ( my $txn = $transactions->Next ) {
        $time_worked->{ $txn->CreatorObj->Name } += $txn->TimeTaken;
    }
}

# The self service interface doesn't support updating time fields.
return unless $session{CurrentUser}->Privileged
          and $session{CurrentUser}->HasRight( Object => $TicketObj, Right => "ModifyTicket" );

my $date_cf = RT::CustomField->new($session{CurrentUser});
$date_cf->LoadByName( Name => 'Worked Date', LookupType => 'RT::Queue-RT::Ticket-RT::Transaction');

# The Mason code below calls directly to SelectDate to avoid printing the
# parenthesized date and save some space. Build the CF name normally
# built in EditCustomField and EditCustomFieldDate.
my $cf_name = 'Object-RT::Transaction--CustomField-'
    . $date_cf->Id . '-Values';

# Default to today and also signal that the field is a date field.
my $today = RT::Date->new($session{CurrentUser});
$today->SetToNow();

</%init>
<div class="add-time form-row">
  <div class="col-3 label input"><&|/l&>Add to time</&> <&|/l&>worked:</&></div>
  <div class="col-auto value">
    <form action="<% RT->Config->Get("WebPath") %>/Ticket/Display.html" method="POST">
      <input type="hidden" name="id" value="<% $TicketObj->id %>">
      <div class="form-row">
        <div class="col-auto">
          <& /Elements/SelectDate, Name => "$cf_name", current => 0, ShowTime => 0, Default => $today->Date( Timezone => 'user') &>
        </div>
        <div class="col-auto">
          <input name="UpdateTimeWorked" type="text" value="<% $ARGS{UpdateTimeWorked} || '' %>" size="5" class="form-control" />
        </div>
        <div class="col-auto">
          <& /Elements/SelectTimeUnits, Name => 'UpdateTimeWorked-TimeUnits', Default => $ARGS{'UpdateTimeWorked-TimeUnits'} || 'minutes' &>
        </div>
        <div class="col-auto">
          <input type="submit" class="btn btn-primary form-control" value="<% loc('Add') %>">
        </div>
      </div>
    </form>
  </div>
</div>

<%args>
$TicketObj
</%args>
