#!/usr/bin/env perl
#use strict;
use warnings;
use lib qw(../members);
use libloveAuth;
use utf8;
use Encode;
use Jcode;
use Pg;
use Net::Amazon;
use Net::Amazon::Property::Book;
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 $user_management_table = 'readers';
my $user_name_field = 'nickname';
my $user_password_field = 'password';
my $auth = libloveAuth->new($naoconn, $user_management_table, $user_name_field, $user_password_field);

# CGI設定
$q=new CGI;
my $user_name = '';
my $user_password = '';
my $session_id = '';
if ($ENV{'HTTP_COOKIE'}) {
	$session_id = $auth->get_cookie_value('sid');
}


if ($session_id) {
 print "Content-Type: text/html; charset=UTF-8\n\n";
 print <<"END_OF_SEARCH";
<!DOCTYPE html PUBLIC "-//W3C//DTDXHTML 1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/
xhtml1-transitional.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_search.css" rel="stylesheet" type="text/css">
</head>
<body>

<!--ログインフォーム-->
<div class="loginform">
	<form>
		<a href="#">[招待する]</a>&nbsp;&nbsp;<a href="#">[マイページ]</a>&nbsp;&nbsp;<a href="#">[ヘルプ]</a>&nbsp;&nbsp;<a href="#">[ログアウト]</a>
	</form>
</div>


<!--top部分の画像-->
	<div id="top">
	</div>
	<!--背景開始-->
	
	<div id="wrapper">
		
	<!--ヘッダースタート！-->
	<div id="header">
		<div class="head_left">
			<a href="lib3.html"><img src="images/liblove5_042.png" width="287" height="62" alt=""></a>
		</div>
		
		<!--検索窓-->
		
		<div class="head_right">
			<!--なにかバナーはりたかったらどうぞ-->
		</div>
		<!--検索窓おわり-->
	</div>
	
	<!--ヘッダーおわり！-->
	
	
	
	<!--メニューすたーと！-->
	
	<div class="nav">
		<ul>
		<li><a href=""><img src="images/liblove5_07.png" width="186" height="71" alt=""></a></li>
		<li><a href=""><img src="images/liblove5_08.png" width="165" height="71" alt=""></a></li>
		<li><a href="lib3_search.html"><img src="images/liblove5_09.png" width="178" height="71" alt=""></a></li>
		<li><a href=""><img src="images/liblove5_10.png" width="172" height="71" alt=""></a></li>
		<li><a href=""><img src="images/liblove5_11.png" width="183" height="71" alt=""></a></li>
		</ul>
	</div>
<!--メニューおわり！-->


<!--ここからメニュー以下の中身だおっ(´･ω･｀)-->
<div id="contents">
	
	<!--トップ見出し主要コンテンツなど-->
	<div class="topmenu">
	</div>
	<div class="leftmenu">
		
		
	<h1><img src="./libicon/libicon_nor.png/">一般検索</h1>
	<form>
	<input type="text" name="sword" size="30" alt="SEARCH WORD" />
	<input type="submit" class="ssubmit" value="検索" />
	<br />
	作品名、著者名、出版社名、ISBNなどを入力してください。
	</form>
	
	
	<h1><img src="./libicon/libicon_book.png/">「設定」検索</h1>
	<form>
	<input type="text" name="sword" size="30" alt="SEARCH WORD" />
	<input type="submit" class="ssubmit" value="検索" />
	<br />
	<a href="#">タグ１</a>&nbsp;<a href="#">タグ２</a>&nbsp;<a href="#">タグ３</a>&nbsp;
	</form>
	
	
	<h1><img src="./libicon/libicon_heart.png/">「きもち」検索</h1>
	<form>
	<input type="checkbox" name="sword" size="30" alt="SEARCH WORD" />キュン度<img src="./libicon/libicon_love2.png/" width="15">／
	<input type="checkbox" name="sword" size="30" alt="SEARCH WORD" />泣け度<img src="./libicon/libicon_naki2.png/" width="15">／
	<input type="checkbox" name="sword" size="30" alt="SEARCH WORD" />ヤキモキ度<img src="./libicon/libicon_kaminari2.png/" width="15">
	<input type="submit" class="ssubmit" value="検索" />
	<br />
	もしくはタグ検索？（仮）<br />
	</form>
	<form>
	<input type="text" name="sword" size="30" alt="SEARCH WORD" />
	<input type="submit" class="ssubmit" value="検索" />
	<br />
	<a href="#">タグ１</a>&nbsp;<a href="#">タグ２</a>&nbsp;<a href="#">タグ３</a>&nbsp;
	</form>
	<br />
	<br />
	</div>
	<div class="rightmenu">
			<p>

END_OF_SEARCH

$sword = $q->param('sword');
 if (!$sword) {
     print "何も書かれていません \n";
   } else {&search;}


sub search{
my $access_key_id     = 'AKIAJVXTJFHDN7HW6UKA';
my $secret_access_key = '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';
my $keyword           = $sword;
my $mode              = 'books';
my $locale            = 'jp';
my $sort              = 'daterank';

my $aws = Net::Amazon->new(
    token      => $access_key_id,
    secret_key => $secret_access_key,
    locale     => $locale
);

my $response = $aws->search(
    keyword => $keyword,
    mode    => 'books',
    sort    => $sort
);
if($response->is_success()) {
    foreach my $item ($response->properties) {
        #$title        =      encode('utf-8', $item->ProductName()), "\n";
        #$author       =       encode('utf-8', $item->author()), "\n";
        #$ASIN      =      encode('utf-8', $item->ASIN()), "\n";
        #$OP         =      encode('utf-8', $item->OurPrice()), "\n";
	    #$IUM       =       encode('utf-8', $item->ImageUrlMedium()), "\n";
        #$URL       =   encode('utf-8', $item->url()), "\n\n";
		$title = $item->ProductName();
		$author = $item->author();
		$ASIN = $item->ASIN();
		$OP = $item->OurPrice();
		$IUS = $item->ImageUrlSmall();
		$IUM = $item->ImageUrlMedium();
		$IUL = $item->ImageUrlLarge();
		$URL = $item->url();

print "<center>";
print "<br>";
print "$title";
print "<br>";
print "$author";
print "<br>";
print "$ASIN";
print "<br>";
print "$OP";
print "<br>";
print "<a href=\"http://gms.gdl.jp/~tsuyoshi/cgi-bin/aqh2.pl?title=$title&author=$author&asin=$ASIN&price=$OP&images=$IUS&&imagem=$IUM&imagel=$IUL&&alink=$URL\"><img src=\"$IUM\"></img></a>";
print "<br>";
print "<a href=\"$URL\">リンク</a>";
print "<br>";
print "<br>";
print "<a href=\"aqj3.pl?title=$title&author=$author&asin=$ASIN&price=$OP&images=$IUS&&imagem=$IUM&imagel=$IUL&&alink=$URL\">お気に入り</a>";
print "<br>";
print "<br>";
print "</center>";
 }
} else {
    print "Error: ", $response->message(), "\n";
}
}


print <<"END_OF_OTHER";

 </p>
	</div>
</div>
</div>


<!--コンテントとwrapperおわり！-->


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

</body>
</html>

END_OF_OTHER
}
exit(0);
