#!/usr/bin/perl
$|=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";
#$offset	 = 0;
#$limit	 = 20;

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

$cgifile = "dispprocess.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) or exitError("Cannot connect.");

&read_tmpl($tmpl{'index'});
print "Content-type: text/html\n\n";

$content{'cgifile'}		 = $cgifile;

#hit
$sth = $dbh->prepare("select count(*) from process");
$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 process
	order by process_num, num desc");
#	offset $offset limit $limit");
$sth->execute;
if ($sth->errstr) { print $sth->errstr; }

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

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

	$content{'process_num'}	 = $data{'process_num'};
	$content{'process'}		 = $data{'process'};
	($content{'cmt'}	 = $data{'cmt'}) =~ s/\r\n|\r|\n/<br>\n/g;
	if ($data{'img'}) {
		$content{'img'}	 = qq(<img src="$Image{'path'}/$data{'img'}/_l.jpg">);
	} else {
		$content{'img'}	 = qq(<img src="$Image{'path'}/noimage.gif" border="0" alt="">);
	}

	#最終レコード以外は矢印を表示
	if($i < $hit){
		$content{'arrow'}	= qq(<img src="./images/image_05.gif" border="0" alt="">);
	} else {
		$content{'arrow'}	= '';
	}
	
	$body =~ s/%(\w+)%/jcode($content{"$1"})->sjis/ge;
	print $body;

	$i++;
}
undef $html{'body'};

=commentout
$content{'older'} = $content{'prev'} ne '' ?
	 qq{<input type="submit" name="submit" value="＜＜前$limit件">} : '';
$content{'newer'} = $content{'next'} ne '' ?
	 qq{<input type="submit" name="submit" value="次$nextnum件＞＞">} : '';
=cut

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

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

exit(0);
