<%args>
$id
</%args>
<%init>
my @months = qw/January February March April May June July August September October November December/;
my @days   = qw/Sunday Monday Tuesday Wednesday Thursday Friday Saturday/;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my $today  = "$days[$wday], $months[$mon] ".sprintf("%02d",$mday).", ".($year + 1900);

my $base_path = $RT::LocalPluginPath.'/RT-Site-UMN-Duluth/static/';
my $style     = $base_path.'/css/rt-site-umn-duluth-invoice.css';
my $image     = $base_path.'/images/180x180_circle.gif';

my $TicketObj = LoadTicket($id);
my $ticket_id = $TicketObj->Id;

my $created_on = $TicketObj->Created;
HTML::Mason::Escapes::html_entities_escape(\$created_on);

my $subject = $TicketObj->Subject;
HTML::Mason::Escapes::html_entities_escape(\$subject);

my $creator = RT::User->new($session{CurrentUser});
my ($ok, $msg) = $creator->Load($TicketObj->Creator);
die "$msg\n" unless ($ok);

my $created_by = $m->scomp(
    '/Elements/ShowUser',
    User => $creator,
);

my $av_equipment;
my $cf = RT::CustomField->new($session{CurrentUser});
($ok, $msg) = $cf->Load('AV Equipment');
die "$msg\n" unless ($ok);
my $values = $TicketObj->CustomFieldValues($cf->Id);
my $counter = 0;
while (my $value = $values->Next) {
    $counter++;
    my $content = $value->Content;
    HTML::Mason::Escapes::html_entities_escape(\$content);
    $av_equipment .= "<br/>" if ($counter > 1);
    $av_equipment .= $content;
}

my $date_format = '%a, %Y-%m-%d, %l:%M%p';

my $pickup_date = $TicketObj->FirstCustomFieldValue('Pickup Date');
if ($pickup_date) {
    my $PickupDateObj = RT::Date->new($session{CurrentUser});
    $PickupDateObj->Set(
        Format   => 'unknown',
        Value    => $pickup_date,
        Timezone => 'utc',
    );
    $pickup_date = $PickupDateObj->Strftime($date_format);
}
else {
    $pickup_date = '(no pickup date specified)';
}
HTML::Mason::Escapes::html_entities_escape(\$pickup_date);

my $due_date = $TicketObj->FirstCustomFieldValue('Due Date');
if ($due_date) {
    my $DueDateObj = RT::Date->new($session{CurrentUser});
    $DueDateObj->Set(
        Format   => 'unknown',
        Value    => $due_date,
        Timezone => 'utc',
    );
    $due_date = $DueDateObj->Strftime($date_format);
}
else {
    $due_date = '(no due date specified)';
}
HTML::Mason::Escapes::html_entities_escape(\$due_date);

my $realnames      = $TicketObj->Requestors->MemberRealNamesAsString;
HTML::Mason::Escapes::html_entities_escape(\$realnames);

my $usernames      = $TicketObj->Requestors->MemberNamesAsString;
HTML::Mason::Escapes::html_entities_escape(\$usernames);

my $ids            = $TicketObj->Requestors->MemberIDsAsString;

my $phone          = $TicketObj->FirstCustomFieldValue("Phone Number");
HTML::Mason::Escapes::html_entities_escape(\$phone);

my $department     = $TicketObj->FirstCustomFieldValue("Department");
HTML::Mason::Escapes::html_entities_escape(\$department);

my $location       = $TicketObj->FirstCustomFieldValue("Building") . " " . $TicketObj->FirstCustomFieldValue("Room");
HTML::Mason::Escapes::html_entities_escape(\$location);

my $empty_space    = '&nbsp;'x83;
my $signature_line = "<u>$empty_space</u>";

my $phs1 = '<td>'.'&nbsp;'x1 .'</td>';  # Parts Horizontal Space... Since we are setting the cellpadding to 0, we still want some horizontal spacing

my $computers = "computer's"; # The apostrophe is causing me issues!
HTML::Mason::Escapes::html_entities_escape(\$computers);

my $html = <<EOF;
<html>
<body>

<table width="100%">
    <tr>
        <td valign="top" rowspan="2">
            <table>
                <tr>
                    <td><img width="90" height="90" src="$image"/></td>
                    <td>
                        <i>Information Technology Systems and Services<br/>
                        TechCenter AV Checkout<br/>
                        165 KPlz<br/>
                        (218) 726-6222</i>
                    </td>
                </tr>
            </table>
        </td>
        <td valign="top" align="right">
            <table cellspacing="0" cellpadding="0">
                <tr>
                    <td>
                        <b>AV Checkout</b><br/>
                        <b>Reference #</b> $ticket_id
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td align="right">
            <table cellspacing="0" cellpadding="0">
                <tr>
                    <td>Created:&nbsp;</td>
                    <td align="right">$created_on</td>
                </tr>
                <tr>
                    <td>Creator:&nbsp;</td>
                    <td align="right">$created_by</td>
                </tr>
            </table>
        </td>
    </tr>
</table>

<hr/>

<!-- meta data -->
<table width="100%">
<tr><td valign="top">
<table width="100%" rules="none" border="1" cellpadding="0" bgcolor="#ededed">
    <tr>$phs1<td valign="top" nowrap><b>Customer ID:</b></td><td valign="top">$ids</td></tr>
    <tr>$phs1<td valign="top" nowrap><b>Contact</b>:</td><td valign="top">$realnames</td></tr>
    <tr>$phs1<td valign="top" nowrap><b>Department:</b></td><td valign="top">$department</td></tr>
    <tr>$phs1<td valign="top" nowrap><b>Location:</b></td><td valign="top">$location</td></tr>
    <tr>$phs1<td valign="top" nowrap><b>Phone:</b></td><td valign="top">$phone</td></tr>
    <tr>$phs1<td valign="top" nowrap><b>Username:</b></td><td valign="top">$usernames</td></tr>
</table>
</td><td valign="top">
<table width="100%" rules="none" border="1" cellpadding="0" bgcolor="#ededed">
    <tr>$phs1<td valign="top" nowrap><b>Equipment:</b></td><td valign="top">$av_equipment</td></tr>
    <tr>$phs1<td valign="top" nowrap><b>Pickup Date/Time:</b></td><td valign="top">$pickup_date</td></tr>
    <tr>$phs1<td valign="top" nowrap><b>Due Date/Time:</b></td><td valign="top">$due_date</td></tr>
    <tr>$phs1<td valign="top" nowrap><b>Subject-line:</b></td><td valign="top">$subject</td></tr>
</table>
</td></tr>
</table>
<!-- end of meta data -->
<hr/>

CUSTOMER AGREEMENT
<table>
<tr><td>&nbsp;&nbsp;&nbsp;</td><td>
<p>By signing below, I acknowledge receipt of the equipment listed above.</p>
<p>I certify that I am using this equipment for UMD activities only
(coursework or official business.)</p>
<p>I agree to return the equipment in its current condition by the date and time listed.</p>
<p>I understand that if I do not return the equipment on time, I may lose
privileges in the future and a formal report of theft may be filed with UMD
Police.</p>
<p>If the equipment is lost, stolen, or damaged while in my possession I give
permission to UMD Information Technology Systems and Services to charge my
student account for the cost of replacing or repairing the equipment. For
faculty and staff, we will work with departments to recover our replacement
costs.</p>
</td></tr></table>

<hr/>

&nbsp;<br/>
&nbsp;<br/>
&nbsp;<br/>
&nbsp;<br/>


<table>
    <tr><td><b>Customer signature:</b></td><td>$signature_line</td></tr>
    <tr><td></td><td>$today</td></tr>
</table>

</body>
</html>
EOF

$m->out($html);
</%init>
