#!/usr/bin/env perl

use utf8;
use Pg;
use CGI;

# DB設定
my $naoconn = Pg::connectdb("dbname=naoto host=localhost user=naoto password=9513nnn7010");
my $tsuconn = Pg::connectdb("dbname=tsuyoshi host=localhost user=tsuyoshi password=244tsu");

my $query = new CGI;


#とりあえずsid設定

$session_id="3e50d3339baf91601245723694fd74d9";
#$uid
$getnicksql = "select user_id from libloveauth_session where session_id = '" . $session_id . "';";
$outnick = $naoconn->exec($getnicksql);
$unickname =$outnick->getvalue(0,0);
$getuseridsql = "select userid from readers where nickname = '" . $unickname . "';";
$outuid = $naoconn->exec($getuseridsql);
$uid = $outuid->getvalue(0,0);

$favsql="select bookid from rb where userid = '" . $uid . "' order by rbid DESC;";
$outfavbook=$naoconn->exec($favsql);

$maxfavsql="select count(bookid) from rb where userid = '" . $uid . "';";
$outrbmax=$naoconn->exec($maxfavsql);
$maxrbid=$outrbmax->getvalue(0,0);

$tanabooksql="select bookid, imagem, title, author from books order by bookid DESC;";
$outbookinfo=$tsuconn->exec($tanabooksql);

		if ($maxrbid>16) {
			for( $i=0 ; $i < 16 ; $i++ ){
			$favbid = $outfavbook->getvalue($i,0);
			$shelfbooksql="select bookid, imagem, title, author from books where bookid = '" . $favbid . "';";
			$outbookinfo=$tsuconn->exec($shelfbooksql);
			$im=$outbookinfo->getvalue(0,1);
			push(@shelfimage, $im);
			}
		}
		else {
			$bnum=$maxrbid;
			for( $i=0 ; $i < $bnum ; $i++ ){
			$favbid = $outfavbook->getvalue($i,0);
			$shelfbooksql="select bookid, images, title, author from books where bookid = '" . $favbid . "';";
			$outbookinfo=$tsuconn->exec($shelfbooksql);
			$im=$outbookinfo->getvalue(0,1);
			push(@shelfimage, $im);
			}
			$noimage="./none.png";
			$noimagenum=16-$bnum;
			for ($j = 0; $j < $noimagenum; $j++ ) {
			push(@shelfimage, $noimage);
			}
		}

	
print "Content-type: text/html\n\n";

print <<"END_OF_HEAD";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmins="http://www.w3.org/1999/xhtml">
<head>
<title>liblove3</title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="X-UA-Compatible" content="IE=7" />

<link href="libcss3_mypage.css" rel="stylesheet" type="text/css">
</head>
<body>
END_OF_HEAD


print "$favbid";
print "$shelfimage[0]";
print "<img src=\"$shelfimage[0]\" width=\"87\" height=\"126\" alt=\"\">";
print "<br>";
print "$image[1]";
print "<br>";
print "$image[2]";
print "<br>";
print "$image[3]";
print "<br>";
print "$image[4]";
print "<br>";
print "$image[5]";
print "<br>";
print "$image[6]";
print "<br>";
print "$image[7]";
print "<br>";
print "$image[8]";
print "<br>";
print "$image[9]";
print "<br>";
print "$image[10]";
print "<br>";
print "$image[11]";
print "<br>";
print "$image[12]";
print "<br>";
print "$image[13]";
print "<br>";
print "$image[14]";
print "<br>";
print "16";
print "$shelfimage[15]";
print "<img src=\"$shelfimage[15]\" width=\"87\" height=\"126\" alt=\"\">";
print "<br>";
print "17";
print "$image[16]";

	
print <<"END_OF_FOOT";
	<div id="footer">
	<h4>Copyrights &copy; LIBLOVE all rights reserved.</h4>
	</div>
	</body>
	</html>
END_OF_FOOT

exit (0);