#!/usr/bin/perl

use strict;
use CGI;

#use Opals::Constant;
use Opals::Context;

#use SIP::Map;
use SIP::Template;
use SIP::Session;

#use SIP::Utility;

my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }

my $cgi = CGI->new;

# $cgi->param('aaa') returns an array of aaa
my $input = $cgi->Vars();

#my $op = $input->{'op'};
#my $util = SIP::Utility->new();

my $template = SIP::Template->new(filename => 'sample.tmpl',);

my $transaction_datetime = $util->now();
my @dt = split(/ /, $transaction_datetime);
my $transaction_date =~ $dt[0];

my $barcode = $input->{'item_identifier'};

my $session = SIP::Session->new(
  dbh               => $dbh,
  session_id        => $input->{'session_id'},
  patron_identifier => $input->{'patron_identifier'},
  item_identifier   => $barcode
);
$session->validate(
  patron_identifier => 1,
  account_expiry    => 1,
  item_identifier   => 1
);

my $screen_message = $session->get('screen_message');
my $user           = $session->get('user');
my $record         = $session->get('record');

if ($session->isValid()) {

  # do something...
}

my $response = {
  debug          => $session->get('debug'),
  institution_id => $input->{'institution_id'},
  screen_message => $screen_message,
};

$template->param($response);

$template->write(cgi => $cgi);
