#!/usr/bin/env perl
use Mojolicious::Lite;

use lib '/srv/koha_ffzg';
BEGIN {
	$ENV{KOHA_CONF} = '/etc/koha/sites/ffzg/koha-conf.xml';
}
use C4::Context;

use Data::Dump qw(dump);

my $sql = qq{
select
	title,
	author
from items
join biblio on biblio.biblionumber = items.biblionumber where barcode = ?
};

my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare($sql);

my $sth_update = $dbh->prepare(qq{
update items set datelastseen = now() where barcode = ?
});

my $sth_inventura = $dbh->prepare(qq{
insert ignore into ffzg_inventura (date_scanned,barcode,source_id) values (date(now()), ?, 'abc')
});
get '/' => sub {
	my $c = shift;

	my $barcode = $c->param('barcode');
	warn "# $barcode";
	$sth->execute( $barcode );
	while ( my $row = $sth->fetchrow_hashref ) {
		warn "# row ",dump($row);
		$c->stash( $_ => $row->{$_} ) foreach keys %$row;
	}
	$sth_update->execute( $barcode );
	$sth_inventura->execute( $barcode );
	$c->render(template => 'index');
};

app->start;
__DATA__

@@ index.html.ep
% layout 'default';

%= form_for '/' => begin
	%#= text_field 'barcode'
	<input name=barcode size=10 autofocus>
	%= submit_button 'Scan'
% end

<%= stash('author') %>
<b><%= stash('title') %></b>

@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
  <head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <style>
	body {
		font-size: 800%;
	}
  </style>
  <title>Inventura</title>
</head>
  <body><%= content %></body>
</html>
