#! /usr/local/bin/perl

require '../cgi-bin/gh_init.pl';

MAIN: 
{
	local %in = &decode();

	local %CONFIG = (
		'auther_data' => 'data/auther.txt',
		'critic_data' => 'data/critic.txt',
		'book_data'   => 'data/book.txt'
	);

	if ($in{'id'} && $in{'id'} ne '') {
		&detail($in{'id'});
	}
	if ($in{'auther'} && $in{'auther'} ne '') {
		&search('auther', $in{'auther'});
	}
	if ($in{'critic'} && $in{'critic'} ne '') {
		&search('critic', $in{'critic'});
	}

	# auther
	if (open(IN, "<$CONFIG{'auther_data'}")) {
		while (<IN>) {
			s/\n$//;
			my ($id, $auther, $kana, $exp) = split(/\t/, $_);

			$content{'auther'} .= '<span><a id="a_id_'. $id. '" class="book_auther" ';
			$content{'auther'} .= 'href="index.cgi?auther='. $id. '">';
			$content{'auther'} .= $auther;
			$content{'auther'} .= '</a></span>'. "\n";
		}
		close(IN);
	}

	#critic
	if (open(IN, "<$CONFIG{'critic_data'}")) {
		while (<IN>) {
			s/\n$//;
			my ($id, $auther, $kana, $exp) = split(/\t/, $_);

			if ($id eq $current_id) {
				$cur_auther  = $auther;
				$cur_kana = $kana;
			}

			$content{'critic'} .= '<span><a id="c_id_'. $id. '" class="book_auther" ';
			$content{'critic'} .= 'href="index.cgi?critic='. $id. '">';
			$content{'critic'} .= $auther;
			$content{'critic'} .= '</a></span>'. "\n";
		}
		close(IN);
	}

	&html_output('file' => '_base.html', 'hash' => \%content);

	exit(0);
}

sub search {
	my $mode  = shift;
	my $target = shift;

	local %AUTHER = &get_auther();
	local %CRITIC = &get_critic();

	my $num;
	if ($mode eq 'auther') { $num = 2; }
	if ($mode eq 'critic') { $num = 4; }

	$content{'body'} = '<h4>検索結果</h4>'. "\n";

	$res = '';
	if (open(IN, "<$CONFIG{'book_data'}")) {
		while (<IN>) {
			s/\n$//;
#			my ($id, $book_title, $book_auther, $auther_foot, $book_critic, $book_info, $book_body) = split(/__<TAB>__/, $_);
			my @data = split(/__<TAB>__/, $_);

			next if ($data[$num] ne $target);

			$critic = '';
			if ($CRITIC{$data[4]} && $CRITIC{$data[4]} ne '') {
				$critic = '<p>評者: '. $CRITIC{$data[4]}. '</p>';
			}

			$res .= <<__TCELL__;
			<tr>
				<td>
					<strong><a href="index.cgi?id=$data[0]" class="arw">$data[1]</a></strong><br />
					<p>
						$AUTHER{$data[2]}&nbsp;&nbsp;$afoot{$data[3]}</p>
					<div style="padding-left: 2em; font-size: 12px">
						$data[5]</div>
					${critic}
				</td>
			</tr>
__TCELL__
			;
		}
		close(IN);
	}

	if ($res ne '') {
		$content{'body'} .= '<table class="common_table">'. "\n";
		$content{'body'} .= $res. "\n";
		$content{'body'} .= '</table>'. "\n";
	}
	else {
		$content{'body'} .= '登録図書がありません。'. "\n";
	}

	&html_output('file' => '_search.html', 'hash' => \%content);

	exit(0);
}

sub detail {
	my $target = shift;

	local %AUTHER = &get_auther();
	local %CRITIC = &get_critic();

	my @data;
	if (open(IN, "<$CONFIG{'book_data'}")) {
		while (<IN>) {
			s/\n$//;
#			my ($id, $book_title, $book_auther, $auther_foot, $book_critic, $book_info, $book_body) = split(/__<TAB>__/, $_);
			@data = split(/__<TAB>__/, $_);

			last if ($data[0] eq $target);
		}
		close(IN);
	}

	%afoot = (1 => '著', 2 => '編');

	$data[6] =~ s/__<LF>__/\n/g;

	$content{'body'} = <<__HTML__;
<h4>$data[1]</h4>

<p>$AUTHER{$data[2]}&nbsp;&nbsp;$afoot{$data[3]}</p>
<div style="padding-left: 2em; font-size: 12px;">
$data[5]
</div>

<div class="no_h5">
$data[6]
<div class="critic">評者&nbsp;&nbsp;$CRITIC{$data[4]}</div>
</div>
__HTML__
	;

	&html_output('file' => '_search.html', 'hash' => \%content);

	exit(0);
}


sub get_auther {
	my %hash;

	if (open(IN, "<$CONFIG{'auther_data'}")) {
		while (<IN>) {
			s/\n$//;
			my ($id, $auther, $kana, $exp) = split(/\t/, $_);

			$hash{$id} = $auther;
		}
		close(IN);
	}

	return %hash;
}

sub get_critic {
	my %hash;

	if (open(IN, "<$CONFIG{'critic_data'}")) {
		while (<IN>) {
			s/\n$//;
			my ($id, $auther, $kana, $exp) = split(/\t/, $_);

			$hash{$id} = $auther;
		}
		close(IN);
	}

	return %hash;
}
