#!/usr/bin/perl

use Guidetour::CGI;
use Guidetour::DBI;
use Guidetour::Setup;
use Guidetour::Util;
use Guidetour::Template;
use Squeeze::Util qw( date_time_str );
use Squeeze::Util::Regexp qw( check_mail_address_loose check_katakana );
use Storable qw( dclone );

use Guidetour::Table::Form;

###-----------------------------------------------
### MAKE INSTANCE
###-----------------------------------------------

my( $cgi, $p ) = Guidetour::CGI->new();
my $dbi = Guidetour::DBI->new();
my $dbh = $dbi->db_Main;
my $util = Guidetour::Util->new( cgi => $cgi, dbi => $dbi, dbh => $dbh );

###-----------------------------------------------
### SUBROUTINE
###-----------------------------------------------

##----------入力画面

my $sub_input = sub
{
	my( $b_error ) = @_;
	
	my $tmpl = Guidetour::Template->new( file => qq|$TEMPLATE_DIR/form.html| );
	$cgi->template($tmpl);
	
	$tmpl->convert_area( area => 'AREA_ERROR', cont => '' ) if( !$b_error );
	
	##-----エラー画面から戻ってきた場合の処理
	if( $p->{h_data} )
	{
		my $h_data = $cgi->decode_hidden_value( str => $p->{h_data} );
		
		foreach my $key( sort keys %$h_data )
		{
			$p = $h_data;
		}
	}
	
	##-----form_info
	$tmpl->form_info( name => 'phase', type => 'hidden', args => { -default => 1, -override => 1 } );
	
	$p->{journalist_div} ||= 0;
	foreach my $i( @JOURNALIST_DIV )
	{
		$tmpl->form_info( name => 'journalist_div'.$i, type => 'radio_group', args => { -name => 'journalist_div', -values => [$i], -labels => { $i => $JOURNALIST_DIV{ $i } }, -default => $p->{journalist_div} } );
	}
	
	$tmpl->form_info( name => 'name_sei', args => { -size => 20 } );
	$tmpl->form_info( name => 'name_mei', args => { -size => 20 } );
	$tmpl->form_info( name => 'name_kana_sei', args => { -size => 20 } );
	$tmpl->form_info( name => 'name_kana_mei', args => { -size => 20 } );
	
	$tmpl->form_info( name => 'zip1', args => { -size => 4, -maxlength => 3 } );
	$tmpl->form_info( name => 'zip2', args => { -size => 5, -maxlength => 4 } );
	$p->{pref} ||= 0;
	$tmpl->form_info( name => 'pref', type => 'popup_menu', args => { -values => \@PREF, -labels => \%PREF, -default => $p->{pref} } );
	$tmpl->form_info( name => 'address1', args => { -size => 40 } );
	$tmpl->form_info( name => 'address2', args => { -size => 50 } );
	
	$tmpl->form_info( name => 'tel1', args => { -size => 5, -maxlength => 5 } );
	$tmpl->form_info( name => 'tel2', args => { -size => 5, -maxlength => 5 } );
	$tmpl->form_info( name => 'tel3', args => { -size => 5, -maxlength => 5 } );
	$tmpl->form_info( name => 'other_tel1', args => { -size => 5, -maxlength => 5 } );
	$tmpl->form_info( name => 'other_tel2', args => { -size => 5, -maxlength => 5 } );
	$tmpl->form_info( name => 'other_tel3', args => { -size => 5, -maxlength => 5 } );
	
	$tmpl->form_info( name => 'email', args => { -size => 50, -maxlength => 100 } );
	$tmpl->form_info( name => 'email_confirm', args => { -size => 50, -maxlength => 100 } );
	
	
	$p->{age} ||= 0;
	$tmpl->form_info( name => 'age', type => 'popup_menu', args => { -values => \@AGE, -labels => \%AGE, -default => $p->{age} } );
	$p->{sex} ||= 0;
	$tmpl->form_info( name => 'sex', type => 'radio_group', args => { -values => \@SEX, -labels => \%SEX, -default => $p->{sex} } );
	
	$p->{participate_div} ||= 0;
	$tmpl->form_info( name => 'participate_div', type => 'radio_group', args => { -values => \@PARTICIPATE_DIV, -labels => \%PARTICIPATE_DIV, -default => $p->{participate_div} } );
	
	
	$tmpl->set( hash => { b_error => $b_error } );
	
	$dbh->commit();
	$cgi->print_page();
};

##----------確認画面

my $sub_confirm = sub
{
	my $tmpl = Guidetour::Template->new( file => qq|$TEMPLATE_DIR/check.html| );
	$cgi->template($tmpl);
	
	$p->{guide_date_div} = $JOURNALIST_GUIDE_DIV{ $p->{journalist_div} };
	my $h_data = $cgi->encode_hidden_value( ref => $p );
	
	##-----form_info
	$tmpl->form_info( name => 'h_data', type => 'hidden', args => { -default => $h_data } );
	
	##-----表示データの整形
	my $param = &dclone( $p );
	
	$param->{guide_date_div} = $GUIDE_DATE_DIV{ $p->{guide_date_div} };
	$param->{journalist_div} = $JOURNALIST_DIV{ $param->{journalist_div} };
	$param->{pref} = $PREF{ $param->{pref} };
	$param->{age} = $AGE{ $param->{age} };
	$param->{sex} = $SEX{ $param->{sex} };
	$param->{participate_div} = $PARTICIPATE_DIV{ $param->{participate_div} };
	
	
	$tmpl->set( hash => $param );
	
	$dbi->dbi_commit();
	$cgi->print_page();
};

##----------完了画面

my $sub_complete = sub
{
	my( $h_data ) = shift;
	
	my $tmpl = Guidetour::Template->new( file => qq|$TEMPLATE_DIR/thanks.html| );
	$cgi->template($tmpl);
	
	$tmpl->set( hash => {} );
	
	$dbi->dbi_commit();
	$cgi->print_page();
};

##----------入力チェック

my $sub_input_check = sub
{
	my $param = shift;
	
	##-----入力チェック
	my $b_error;
	
	$b_error .= "<li>日程及びガイドを希望するジャーナリストが未選択です。</li>\n" if( !$param->{journalist_div} );
	
	$b_error .= "<li>お名前が未入力です。</li>\n" if( !$param->{name_sei} or !$param->{name_mei} );
	$b_error .= "<li>お名前フリガナが未入力です。</li>\n" if( !$param->{name_kana_sei} or !$param->{name_kana_mei} );
	
	if( $param->{zip1} and $param->{zip2} )
	{
		$b_error .= "<li>郵便番号の入力が『半角数字3桁』-『半角数字4桁』になっておりません。</li>\n" if( $param->{zip1} !~ /^\d{3}$/ or $param->{zip2} !~ /^\d{4}$/ );
	}
	else
	{
		$b_error .= "<li>郵便番号が未入力です。</li>\n";
	}
	
	$b_error .= "<li>都道府県が未選択です。</li>\n" if( !$param->{pref} );
	$b_error .= "<li>市区町村が未入力です。</li>\n" if( !$param->{address1} );
	$b_error .= "<li>番地、マンション・アパート名が未入力です。</li>\n" if( !$param->{address2} );
	
	if( $param->{tel1} and $param->{tel2} and $param->{tel3} )
	{
		$b_error .= "<li>ご自宅の電話番号の入力が『半角数字』-『半角数字』-『半角数字』になっておりません。</li>\n" if( $param->{tel1} !~ /^\d{2,5}$/ or $param->{tel2} !~ /^\d{1,5}$/ or $param->{tel3} !~ /^\d{2,5}$/ );
	}
	else
	{
		$b_error .= "<li>ご自宅の電話番号が未入力です。</li>\n";
	}
	
	if( $param->{other_tel1} and $param->{other_tel2} and $param->{other_tel3} )
	{
		$b_error .= "<li>日中の連絡先の入力が『半角数字』-『半角数字』-『半角数字』になっておりません。</li>\n" if( $param->{other_tel1} !~ /^\d{2,5}$/ or $param->{other_tel2} !~ /^\d{1,5}$/ or $param->{other_tel3} !~ /^\d{2,5}$/ );
	}
	else
	{
		$b_error .= "<li>日中の連絡先が未入力です。</li>\n";
	}
	
	if( $param->{email} )
	{
		if( $param->{email_confirm} )
		{
			if( $param->{email} eq $param->{email_confirm} )
			{
				$b_error .= "<li>メールアドレスの入力フォーマットがおかしいようです。</li>\n" if( !&check_mail_address_loose( $param->{email} ) );
			}
			else
			{
				$b_error .= "<li>メールアドレスの入力が確認用と一致しません。</li>\n";
			}
		}
		else
		{
			$b_error .= "<li>メールアドレス（確認用）が未入力です。</li>\n";
		}
	}
	else
	{
		$b_error .= "<li>メールアドレスが未入力です。</li>\n";
	}
	
	$b_error .= "<li>性別が未選択です。</li>\n" if( !$param->{sex} );
	$b_error .= "<li>年齢が未選択です。</li>\n" if( !$param->{age} );
	$b_error .= "<li>東京モーターショー参加回数が未選択です。</li>\n" if( !$param->{participate_div} );
	
	return $b_error;
};

###-----------------------------------------------
### MAIN
###-----------------------------------------------

if( $p->{phase} == 1 )
{
	##-----入力チェック
	my $b_error = $sub_input_check->( $p );
	
	if( $b_error )
	{
		$sub_input->( $b_error );
	}
	else
	{
		$sub_confirm->();
	}
}
elsif( $p->{phase} == 2 )
{
	my $h_data = $cgi->decode_hidden_value( str => $p->{h_data} );
	$p = $h_data;
	
	##-----入力チェック
	my $b_error = $sub_input_check->( $p );
#	my $h_data = $cgi->encode_hidden_value( ref => $p );
#	$util->error_out( mesg => $b_error, h_data => $h_data, back_flag => 1 ) if( $b_error );
	
	if( $b_error )
	{
		$sub_input->( $b_error );
	}
	
	##-----データ登録
	eval
	{
		$dbi->Lock_table( table => { tbl_form_guidetour => 'WRITE' } );
		
		$p->{regist_date} = &date_time_str();
		
		## tbl_form2009_Guidetour
		my $obj_form = Guidetour::Table::Form->insert_data( data => $p, delete_id => 1 ) or die;
		
		$dbi->dbi_commit();
		$dbi->Unlock_table();
	};
	if( $@ )
	{
		my $msg = $@;
		
		$dbi->dbi_rollback();
		$dbi->Unlock_table();
		$util->error_out( mesg => 'システムエラーが発生しました。:'.$msg, back_flag => 1 )
	}
	
	
	$sub_complete->();
}
else
{
	$sub_input->();
}
