#!/usr/bin/perl -w
$|=1;

#製品紹介表示処理
=commentout
	初期設定はinit.plで行います
=cut

use CGI::Carp qw(fatalsToBrowser);
use lib '../master/lib';
use CGI qw(:cgi);
use DBI;
use Jcode;
use AETmpl;
use AEPhone;
use AEImg;

require 'init.pl';

$tmpl{'index'}	 = "tmp_index.html";
$tmpl{'details'} = "tmp_details.html";

$Image{'dir'}	 = '/home/tyuzou/public_html/equipment_images';
$Image{'path'}	 = '../equipment_images';

$cgifile = "dispequipment.cgi";

foreach (param) {
	next unless $_;
	if (param("$_") =~ /\0/) {
		@{$_} = split(/\0/, param("$_"));
		@{$_} = map {jcode($data)->euc} @{$_};
	} else {
		${$_} = jcode(param("$_"))->euc;
	}
}

# テンプレート振り分け
if ($phone{'furiwake'} eq 'mobile') {
	foreach(keys %tmpl) {
		$tmpl{"$_"} =~ s/\.html/_m.html/;
	}
}

$dbh = DBI->connect($ds,$dbuser,$dbpass) or die("Cannot connect.");

$content{'cgifile'}		 = $cgifile;

#一覧表示
unless($num) {
	&read_tmpl($tmpl{'index'});
	
	print "Content-type: text/html\n\n";
	
	#hit
	$sth = $dbh->prepare("select count(*) from equipment");
	$sth->execute;
	if ($sth->errstr) { print $sth->errstr; }
	while($ref = $sth->fetchrow_arrayref){
		($hit) = @$ref;
	}
	
	$content{'hit'}		 = $hit;
	$content{'start'}	 = $hit ? $offset+1 : 0;
	$content{'prev'}	 = ($offset-$limit >= 0) ? $offset-$limit : '';
	$content{'end'}		 = ($offset+$limit < $hit) ? $offset+$limit : $hit;
	$content{'next'}	 = ($offset+$limit < $hit) ? $offset+$limit : '';
	$nextnum			 = ($hit-$offset-$limit < $limit) ? $hit-$offset-$limit : $limit;
	
	$sth = $dbh->prepare("select * from equipment
		order by to_number(dispnum, 99999), num desc");
	$sth->execute;
	if ($sth->errstr) { print $sth->errstr; }

	$html{'head'} =~ s/%(\w+)%/jcode($content{"$1"})->sjis/ge;
	print $html{'head'};
	undef $html{'head'};
	
	$body{'head'} =~ s/%(\w+)%/jcode($content{"$1"})->sjis/ge;
	$body{'foot'} =~ s/%(\w+)%/jcode($content{"$1"})->sjis/ge;
	
	while($ref = $sth->fetchrow_hashref){
		my %data = %$ref;
		my $body = $html{'body'};
	
		$content{'num'}				 = $data{'num'};
		$content{'kind'}			 = $data{'kind'};
		$content{'regname'}			 = $data{'regname'};
		$content{'maker'}			 = $data{'maker'};
		$content{'capacity'}		 = $data{'capacity'};
		$content{'cnt'}				 = $data{'cnt'};
		$content{'img_link'} 		 = $data{img} ? qq(<input type="image" src="images/photo-on.gif" alt="">) : '';
	
		$body =~ s/%(\w+)%/jcode($content{"$1"})->sjis/ge;
		print $body;
	}
	undef $html{'body'};
	
	$html{'foot'} =~ s/%(\w+)%/jcode($content{"$1"})->sjis/ge;
	print $html{'foot'};

#詳細表示（popup）
} else {
	&read_tmpl($tmpl{details});
	
	print "Content-type: text/html\n\n";

	$sth = $dbh->prepare("select * from equipment
		where num = '$num'");
	$sth->execute;
	if ($sth->errstr) { print $sth->errstr; }

	$html{'head'} =~ s/%(\w+)%/jcode($content{"$1"})->sjis/ge;
	print $html{'head'};
	undef $html{'head'};

	while($ref = $sth->fetchrow_hashref){
		my %data = %$ref;
		my $body = $html{'body'};

		if ($data{'img'}) {
			$content{'img'}	 = qq(<img src="$Image{'path'}/$data{'img'}/_l.jpg">);
		} else {
			'';
		}
		$content{cmt}		 = &nl2br($data{cmt});
		$body =~ s/%(\w+)%/jcode($content{"$1"})->sjis/ge;
		print $body;
	}
	undef $html{'body'};
	
	$html{'foot'} =~ s/%(\w+)%/jcode($content{"$1"})->sjis/ge;
	print $html{'foot'};
}

$sth->finish if $sth;
$dbh->disconnect;

exit(0);
