package SWHtmlMakeVilForm;

#----------------------------------------
# 村作成／編集画面のHTML出力
#----------------------------------------
sub OutHTMLMakeVilForm {
	my ($sow, $vil) = @_;
	my $cfg = $sow->{'cfg'};

	my $vmode = '作成';
	my $vcmd = 'makevil';
	if ($sow->{'query'}->{'cmd'} eq 'editvilform') {
		$vmode = '編集';
		$vcmd = 'editvil';
	}
#	$vcmd = 'makevilpr';

	$sow->{'html'} = SWHtml->new($sow); # HTMLモードの初期化
	my $net = $sow->{'html'}->{'net'}; # Null End Tag
	$sow->{'http'}->outheader(); # HTTPヘッダの出力
	$sow->{'html'}->outheader("村の$vmode"); # HTMLヘッダの出力
	$sow->{'html'}->outcontentheader();

	my $reqvals = &SWBase::GetRequestValues($sow);
	$reqvals->{'vid'} = '';
	my $hidden = &SWBase::GetHiddenValues($sow, $reqvals, '    ');
	my $urlsow = "$cfg->{'BASEDIR_CGI'}/$cfg->{'FILE_SOW'}";
	&SWBase::LoadTextRS($sow, $vil);

	&SWHtmlPC::OutHTMLLogin($sow); # ログインボタン

	# 日付別ログへのリンク
	&SWHtmlPC::OutHTMLTurnNavi($sow, $vil) if ($sow->{'query'}->{'cmd'} eq 'editvilform');

	print <<"_HTML_";
<h2>村の$vmode</h2>

_HTML_

	print "<p class=\"caution\">村を$vmodeするにはログインが必要です。</p>\n\n" if ($sow->{'user'}->logined() <= 0);

	print <<"_HTML_";
<p class="paragraph">
募集期限は作成した日から$sow->{'cfg'}->{'TIMEOUT_SCRAP'}日間です。期限内に村が開始しなかった場合、廃村となります。
</p>

_HTML_

	my $vcomment = $vil->{'vcomment'};
	$vcomment =~ s/<br( \/)?>/\n/ig;

	print <<"_HTML_";
<form action="$urlsow" method="$cfg->{'METHOD_FORM'}">
<div class="form_vmake">
  <fieldset>
    <legend>村の名前と説明</legend>
    <label for="vname" class="multicolumn_label" >村の名前：</label>
    <input id="vname" class="multicolumn_left" type="text" name="vname" value="$vil->{'vname'}" size="30"$net>
    <br class="multicolumn_clear"$net>

    <label for="vcomment" class="multicolumn_label">村の説明：</label>
    <textarea id="vcomment" class="multicolumn_left" name="vcomment" cols="30" rows="3">$vcomment</textarea>
  </fieldset>

  <fieldset>
    <legend>基本設定</legend>
    <label for="vplcnt" class="multicolumn_label" >定員：</label>
    <input id="vplcnt" class="multicolumn_left" type="text" name="vplcnt" value="$vil->{'vplcnt'}" size="5"$net>
    <br class="multicolumn_clear"$net>

    <label for="vplcntstart" class="multicolumn_label" >最低人数：</label>
    <input id="vplcntstart" class="multicolumn_left" type="text" name="vplcntstart" value="$vil->{'vplcntstart'}" size="5"$net> ※開始方法が人狼BBS型の時のみ
    <br class="multicolumn_clear"$net>

    <label for="updhour" class="multicolumn_label">更新時間：</label>
    <select id="updhour" name="hour" class="multicolumn_left">
_HTML_

	my $i;
	for ($i = 0; $i < 24; $i++) {
		my $selected = '';
		$selected = " $sow->{'html'}->{'selected'}" if ($vil->{'updhour'} == $i);
		print "      <option value=\"$i\"$selected>$i時$sow->{'html'}->{'option'}\n";
	}

	print <<"_HTML_";
    </select>
    <select id="updminite" name="minite" class="multicolumn_left">
_HTML_

	for ($i = 0; $i < 60; $i += 30) {
		my $min = sprintf('%02d分', $i);
		my $selected = '';
		$selected = " $sow->{'html'}->{'selected'}" if ($vil->{'updminite'} == $i);
		print "      <option value=\"$i\"$selected>$min$sow->{'html'}->{'option'}\n";
	}

	print <<"_HTML_";
    </select>に更新
    <br class="multicolumn_clear"$net>

    <label for="updinterval" class="multicolumn_label">更新間隔：</label>
    <select id="updinterval" name="updinterval" class="multicolumn_left">
_HTML_


	for ($i = 1; $i <= 3; $i++) {
		my $interval = sprintf('%02d時間', $i * 24);
		my $selected = '';
		$selected = " $sow->{'html'}->{'selected'}" if ($vil->{'updinterval'} == $i);
		print "      <option value=\"$i\"$selected>$interval$sow->{'html'}->{'option'}\n";
	}

	my $votetype_anonymity = " $sow->{'html'}->{'selected'}";
	my $votetype_sign = '';
	if ($vil->{'votetype'} eq 'sign') {
		$votetype_anonymity = '';
		$votetype_sign = " $sow->{'html'}->{'selected'}";
	}

	print <<"_HTML_";
    </select>ごとに更新
    <br class="multicolumn_clear"$net>

    <label for="votetype" class="multicolumn_label">投票方法：</label>
    <select id="votetype" name="votetype" class="multicolumn_left">
      <option value="anonymity"$votetype_anonymity>無記名投票$sow->{'html'}->{'option'}
      <option value="sign"$votetype_sign>記名投票$sow->{'html'}->{'option'}
    </select>
    <br class="multicolumn_clear"$net>

    <label for="roletable" class="multicolumn_label">役職配分：</label>
    <select id="roletable" name="roletable" class="multicolumn_left">
_HTML_

	my $order_roletable = $sow->{'ORDER_ROLETABLE'};
	foreach (@$order_roletable) {
		my $selected = '';
		$selected = " $sow->{'html'}->{'selected'}" if ($vil->{'roletable'} eq $_);
		print "      <option value=\"$_\"$selected>$sow->{'textrs'}->{'CAPTION_ROLETABLE'}->{$_}$sow->{'html'}->{'option'}\n";
	}

#      <option value="later">後で設定$sow->{'html'}->{'option'}

	print <<"_HTML_";
    </select>
    <br class="multicolumn_clear"$net>
  </fieldset>

  <fieldset>
    <legend>役職配分自由設定</legend>
_HTML_

	my $roleid = $sow->{'ROLEID'};
	for ($i = 1; $i < @$roleid; $i++) {
		print <<"_HTML_";
    <div class="multicolumn_label"><label for="cnt$roleid->[$i]">$sow->{'textrs'}->{'ROLENAME'}->[$i]：</label></div>
    <div class="multicolumn_role"><input id="cnt$roleid->[$i]" type="text" name="cnt$roleid->[$i]" size="3" value="$vil->{"cnt$roleid->[$i]"}"$net> 人</div>
_HTML_
	print "    <br class=\"multicolumn_clear\"$net>\n\n" if (($i + 1) % 2 == 1);
	}

	my $limitfree     = " $sow->{'html'}->{'checked'}";
	my $limitpassword = '';
	if ($vil->{'entrylimit'} eq 'password') {
		$limitfree     = '';
		$limitpassword = " $sow->{'html'}->{'checked'}";
	}

	print <<"_HTML_";
  </fieldset>

  <fieldset>
    <legend>参加制限</legend>
    <label><input type="radio" name="entrylimit" value="free"$limitfree$net>制限なし</label><br$net>
    <label>
      <input type="radio" name="entrylimit" value="password"$limitpassword$net>参加用パスワード必須（半角８文字以内）
    </label>
    <input type="password" name="entrypwd" maxlength="8" size="8" value="$vil->{'entrypwd'}"$net>
    <br class="multicolumn_clear"$net>
  </fieldset>

  <fieldset>
    <legend>拡張設定</legend>
    <label for="csid" class="multicolumn_label">登場人物：</label>
    <select id="csid" name="csid" class="multicolumn_left">
_HTML_

	my $csidlist = $sow->{'cfg'}->{'CSIDLIST'};
	foreach (@$csidlist) {
		my @csids = split('/', "$_/");
		my @captions;
		foreach (@csids) {
			$sow->{'charsets'}->loadchrrs($_);
			push(@captions, $sow->{'charsets'}->{'csid'}->{$_}->{'CAPTION'});
		}
		my $caption = join('と', @captions);
		my $selected = '';
		$selected = " $sow->{'html'}->{'selected'}" if ($vil->{'csid'} eq $_);
		print "      <option value=\"$_\"$selected> $caption$sow->{'html'}->{'option'}\n";
	}

	# 村編集で登場人物欄を変更すると……。

	print <<"_HTML_";
    </select>
    <br class="multicolumn_clear"$net>

    <label for="saycnttype" class="multicolumn_label">発言制限： </label>
    <select id="saycnttype" name="saycnttype" class="multicolumn_left">
_HTML_

	my $countssay = $sow->{'cfg'}->{'COUNTS_SAY'};
	my $countssay_order = $countssay->{'ORDER'};
	foreach (@$countssay_order) {
		my $selected = '';
		$selected = " $sow->{'html'}->{'selected'}" if ($vil->{'saycnttype'} eq $_);
		print "      <option value=\"$_\"$selected>$countssay->{$_}->{'CAPTION'}$sow->{'html'}->{'option'}\n";
	}

	print <<"_HTML_";
    </select>
    <br class="multicolumn_clear"$net>

    <label for="starttype" class="multicolumn_label">開始方法： </label>
    <select id="starttype" name="starttype" class="multicolumn_left">
_HTML_

	my $starttype = $sow->{'basictrs'}->{'STARTTYPE'};
	my $starttypeorder = $sow->{'basictrs'}->{'ORDER_STARTTYPE'};

	foreach(@$starttypeorder) {
		my $selected = '';
		$selected = " $sow->{'html'}->{'selected'}" if ($vil->{'starttype'} eq $_);
		print "      <option value=\"$_\"$selected>$starttype->{$_}$sow->{'html'}->{'option'}\n";
	}

	# システムの文章
	print <<"_HTML_";
    </select>
    <br class="multicolumn_clear"$net>

    <label for="trsid" class="multicolumn_label">文章： </label>
    <select id="trsid" name="trsid" class="multicolumn_left">
_HTML_

	my $defaulttrsid = $sow->{'trsid'};
	my $defaulttextrs = $sow->{'textrs'};
	my $trsidlist = $sow->{'cfg'}->{'TRSIDLIST'};
	foreach (@$trsidlist) {
		my $selected = '';
		$selected = " $sow->{'html'}->{'selected'}" if ($vil->{'trsid'} eq $_);

		my %dummyvil = (
			trsid => $_,
		);
		&SWBase::LoadTextRS($sow, \%dummyvil);
		print "      <option value=\"$_\"$selected>$sow->{'textrs'}->{'CAPTION'}$sow->{'html'}->{'option'}\n";
	}
	$sow->{'trsid'} = $defaulttrsid;
	$sow->{'textrs'} = $defaulttextrs;

	print <<"_HTML_";
    </select>
    <br class="multicolumn_clear"$net>

_HTML_

	# ランダム対象
	if ($sow->{'cfg'}->{'ENABLED_RANDOMTARGET'} > 0) {
		my $checkedrndtarget = '';
		$checkedrndtarget = " $sow->{'html'}->{'checked'}" if ($vil->{'randomtarget'} > 0);

		print <<"_HTML_";
    <label for="randomtarget" class="multicolumn_label">ランダム： </label>
    <input type="checkbox" id="randomtarget" class="multicolumn_left" name="randomtarget" value="on"$checkedrndtarget$net>投票・能\力の対象に「ランダム」を含める
    <br class="multicolumn_clear"$net>

_HTML_
	}

	# 役職希望無視
	my $checkednoselrole = '';
	$checkednoselrole = " $sow->{'html'}->{'checked'}" if ($vil->{'noselrole'} > 0);

		print <<"_HTML_";
    <label for="noselrole" class="multicolumn_label">役職希望： </label>
    <input type="checkbox" id="noselrole" class="multicolumn_left" name="noselrole" value="on"$checkednoselrole$net>役職希望を無視する
    <br class="multicolumn_clear"$net>

_HTML_

	my $checkedshowid = '';
	$checkedshowid = " $sow->{'html'}->{'checked'}" if ($vil->{'showid'} > 0);

	print <<"_HTML_";
    <label for="showid" class="multicolumn_label">ID公開： </label>
    <input type="checkbox" id="showid" class="multicolumn_left" name="showid" value="on"$checkedshowid$net>プレイヤーIDを公開する
    <br class="multicolumn_clear"$net>

    <label for="rating" class="multicolumn_label">閲覧制限： </label>
    <select id="rating" name="rating" class="multicolumn_left">
_HTML_

	# レイティング
	my $rating = $sow->{'cfg'}->{'RATING'};
	foreach (@{$rating->{'ORDER'}}) {
		my $selected = '';
		$selected = " $sow->{'html'}->{'selected'}" if ($vil->{'rating'} eq $_);
		print "      <option value=\"$_\"$selected>$rating->{$_}->{'CAPTION'}$sow->{'html'}->{'option'}\n";
	}

	print <<"_HTML_";
    </select>
    <br class="multicolumn_clear"$net>
  </fieldset>

  <div class="exevmake">
    <input type="hidden" name="cmd" value="$vcmd"$net>$hidden
_HTML_

	print "    <input type=\"hidden\" name=\"vid\" value=\"$vil->{'vid'}\"$net>\n" if ($vcmd eq 'editvil');

	my $disabled = '';
	$disabled = " $sow->{'html'}->{'disabled'}" if ($sow->{'user'}->logined() <= 0);

	print <<"_HTML_";
    <input type="submit" value="村の$vmode"$disabled$net>
  </div>
</div>
</form>

_HTML_

	# 日付別ログへのリンク
	&SWHtmlPC::OutHTMLTurnNavi($sow, $vil) if ($sow->{'query'}->{'cmd'} eq 'editvilform');

	&SWHtmlPC::OutHTMLReturnPC($sow); # トップページへ戻る

	$sow->{'html'}->outcontentfooter();
	$sow->{'html'}->outfooter(); # HTMLフッタの出力
	$sow->{'http'}->outfooter();

	return;
}

1;