<?php
print "<html>";
print "<head>";
print "<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">";
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"/ansys/config/css/style.css\">";
print "<link rel=\"shortcut icon\" href=\"/ansys/config/favicon.ico\">";
print "<title>退避管理システム</title>";
print "</head>";

print "<body>";
print "<center>";
print "<font size=\"3\"><b>";
print "退避管理システム<br>-作業者氏名一覧-";
print "【五洋建設株式会社】";
print "</b></font>";
print "</center>";
print "<hr />";

try
{
	$conn = pg_connect("
		host	=	192.168.100.153
		dbname	=	cnbs
		user	=	postgres
		password=	am#71ZJ*
	");
	if (!$conn)
		throw new Exception("データベースへの接続に失敗");
	
	$btnregist = isset($_POST["btnregist"]) ? htmlspecialchars($_POST["btnregist"]) : "";
	if (strcmp($btnregist, "") != 0)
	{
		$txtdispno = isset($_POST["txtdispno"]) ? htmlspecialchars($_POST["txtdispno"]) : "";
		if (strcmp($txtdispno, "") == 0)
			throw new Exception("【登録エラー】表示番号が指定されていません");
		if (!is_numeric($txtdispno))
			throw new Exception("【登録エラー】表示番号が正しくありません");
		$txtwname = isset($_POST["txtwname"]) ? htmlspecialchars($_POST["txtwname"]) : "";
		if (strcmp($txtwname, "") == 0)
			throw new Exception("【登録エラー】作業員氏名が指定されていません");
		$insertWname = "INSERT INTO nakagusuku.worker_name (dispno, wname, update, regdate) VALUES ('". $txtdispno ."','". $txtwname. "',current_timestamp,current_timestamp);";
		print "<font color=\"#ff0000\">";
		if (pg_query($conn, $insertWname))
			print "氏名を登録しました<br>";
		else
			print "新規登録に失敗しました<br>";
		print "</font>";
	}
	
	$selectWname = "SELECT dispno, wname, useflg FROM nakagusuku.worker_name ORDER BY dispno;";
	$res = pg_query($conn, $selectWname);
	if (!$res)
		throw new Exception("SQLエラー");
	
	print "<center><table border=\"1\" style=\"width:80%\">";
	print "<tr bgcolor=\"#ff6666\"><th>表示番号</th><th>氏名</th><th>使用状況</th></tr>";
	
	$rowcnt = pg_num_rows($res);
	for($i = 0; $i < $rowcnt; $i++)
	{
		$row = pg_fetch_assoc($res, $i);
		print "<tr align=\"center\">";
		print "<td><a href=\"./wname.php?dispno=". $row["dispno"]. "\">". $row["dispno"]. "</a></td>";
		print "<td>". $row["wname"]. "</td>";
		$vuseflg = strcmp($row["useflg"], "t") == 0 ? "使用中" : "未使用";
		print "<td><a href=\"./usen.php?pid=". $row["dispno"]. "\">". $vuseflg. "</a></td>";
		print "</tr>";
	}
	
	print "<tr align=\"center\">";
	print "<form method=\"post\">";
	print "<td><input type=\"text\" name=\"txtdispno\" value=\"\" style=\"width:100%;\"></td>";
	print "<td><input type=\"text\" name=\"txtwname\" value=\"\" style=\"width:100%;\"></td>";
	print "<td><input type=\"submit\" name=\"btnregist\" value=\"新規登録\"></td>";
	print "</tr>";
	print "</form>";
	print "</table></center>";
}
catch(Exception $exc)
{
	print "<font color=\"#ff0000\">";
	print $exc. "<br>";
	print "</font>";
}
pg_close($conn);

print "<p style=\"text-align:right;\">";
print "<a href=\"../index.html\">戻る</a>";
print "</p>";
print "<hr>";
print "<center>";
print "<font size=\"3\" face=\"Arial\"><b><i>";
print "penta-ocean";
print "</i></b></font>";
print "</center>";
?>
