#!/usr/bin/perl -w

use DBI;
use CGI qw(:standard);
use Tangram;
use Schema qw($schema);

# Connect to the schema.
my $dsn  = "dbi:mysql:database=library";
my $user = "demo";
my $pass = "open_sesame";
my $storage =
    Tangram::Storage->connect($schema, $dsn, $user, $pass);

# Get all the books, and create a hash with their
# ids and titles.
@books = $storage->select( 'Book' );
%book_labels =
    map { $storage->id($_) => $_->{title} } @books;

# Get all the patrons, and create a hash with their
# ids and titles.
@patrons = $storage->select( 'Patron' );
%patron_labels =
    map { $storage->id($_) => $_->{name} } @patrons;

# Create an HTML form for making a loan.
print header();
print start_html("Loan a Book");
print ("<H1>Loan a Book</H1><HR>");
print start_form( -action => 'makeloan.cgi');

print "Choose Patron: ";
print popup_menu( -name => 'patron',
                  -values => [ keys %patron_labels ],
                  -labels => \%patron_labels );
print "<P>\n";
print "Choose Book: ";
print popup_menu( -name => 'book',
                  -values => [ keys %book_labels ],
                  -labels => \%book_labels );

print "<P>\n";
print submit( -value => 'Approve Loan');

print end_form();
print end_html();