#!/usr/bin/env perl

use strict;
use warnings;
use FindBin qw($RealBin);
use lib "$RealBin/../../lib";
use IMu::Module;
use IMu::Session;
use IMu::Terms;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $cgi = CGI->new();

# example-1
#
# Create new session object.
#
my $session = IMu::Session->new();
$session->setHost('imu.mel.kesoftware.com');

#
# Work out what port to connect to
#
my $port = 40136;
if (defined($cgi->param('port')) && $cgi->param('port') ne '')
{
    $port = $cgi->param('port')
}
$session->setPort($port);

# example-2
#
# Establish connection and tell the server we may want to re-connect
#
$session->connect();
$session->setSuspend(1);

# example-3
#
# Create module object and tell the server not to destroy it.
#
my $module = IMu::Module->new('eparties', $session);
$module->setDestroy(0);

# example-4
#
# If name is supplied, do new search.
#
my $name = $cgi->param('name');
my $id = $cgi->param('id');

if (defined($name) && $name ne '')
{
    my $terms = IMu::Terms->new();
    $terms->add('NamLast', $name);
    $module->findTerms($terms);
}
#
# Otherwise, if id is supplied reattach to existing server-side object
#
elsif (defined($id) && $id ne '')
{
    $module->{'id'} = $id;
}
#
# Otherwise, we can't process
#
else
{
    print($cgi->header('text/html','400 Bad Request'));
    die("missing 'name' or 'id' parameter\n");
}

# example-5
my $columns =
[
    'NamFirst',
    'NamLast'
];

# example-6
#
# Work out which block of records to fetch
#
my $rownum = 1;
if (defined($cgi->param('rownum')))
{
    $rownum = $cgi->param('rownum');
}

# example-7
#
# Fetch next five records
#
my $results = $module->fetch('start', $rownum - 1, 5, $columns);
my $hits = $results->{'hits'};

#
# Build the results page
#
print($cgi->header());
print($cgi->start_html(-title => 'IMu Perl API - Maintaining State'));       
print($cgi->p("Number of matches: $hits"));

#
# Display each match in a separate row in a table
#
print($cgi->start_table());

foreach my $row (@{$results->{'rows'}})
{
    my $rowNum = $row->{'rownum'};
    my $firstName = $row->{'NamFirst'};
    my $lastName = $row->{'NamLast'};

    print($cgi->start_Tr()); 
    print($cgi->start_td(), "$rowNum.", $cgi->end_td());
    print($cgi->start_td(), "$lastName, $firstName", $cgi->end_td());
    print($cgi->end_Tr());
}

print($cgi->end_table(), "\n");

# example-8
#
# Add the Prev and Next links
#
my $url = $cgi->url();
$url .= '?port=' . $session->{'port'};
$url .= '&id=' . $module->{'id'};

# example-9
my $first = $results->{'rows'}->[0];
if ($first->{'rownum'} > 1)
{
    my $prev = $first->{'rownum'} - 5;
    if ($prev < 1)
    {
        $prev = 1;
    }
    $prev = $url . '&rownum=' . $prev;
    print($cgi->a({href => $prev}, 'Prev'), "\n");
}

my $count = @{$results->{'rows'}}; 
my $last = $results->{'rows'}->[$count - 1];
if ($last->{'rownum'} < $results->{'hits'})
{
    my $next = $last->{'rownum'} + 1;
    $next = $url . '&rownum=' . $next;
    print($cgi->a({href => $next}, 'Next'));
}
print($cgi->end_html());
exit(0);
