<?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 />";

$vstatus = array(
	"0" => "退避済",
	"3" => "不明",
	"4" => "退避済",
	"7" => "不明",
	"8" => "退避済",
	"B" => "不明",
	"F" => "作業中"
);


try
{
	$conn = pg_connect("
	host	=	192.168.100.153
	dbname	=	cnbs
	user	=	postgres
	password=	am#71ZJ*
	");
	if (!$conn)
		throw new Exception("データベースへの接続に失敗");
	
	$btnchstate = isset($_POST["btnchstate"]) ? htmlspecialchars($_POST["btnchstate"]) : "";
	if (strcmp($btnchstate, "切替") == 0)
	{
		// refresh state
		$editid = isset($_POST["hipid"]) ? htmlspecialchars($_POST["hipid"]) : "";
		$nowstate = isset($_POST["hinowstate"]) ? htmlspecialchars($_POST["hinowstate"]) : "";
		
		$refstate = "";
		if (strcmp($nowstate, "F") == 0)
			$refstate = "0";
		else if (strcmp($nowstate, "0") == 0 || strcmp($nowstate, "4") == 0 || strcmp($nowstate, "8") == 0)
			$refstate = "F";
		
		if (strcmp($refstate, "") != 0)
		{
			$updatestate = "UPDATE nakagusuku.worker_position SET state='". $refstate. "', update=current_timestamp WHERE pid='". $editid. "';";
			$resupdatestate = pg_query($conn, $updatestate);
			
			print "<font color=\"#ff0000\">";
			print "状態を更新しました<br>";
			print "</font>";
		}
	}
	print "<center><table border=\"1\" style=\"width:80%\">";
	print "<tr bgcolor=\"#66ff66\"><th>表示番号</th><th>作業者氏名</th><th>状態</th><th></th></tr>";
	
	$selectWrkName = "SELECT dispno, tagid, wname FROM nakagusuku.worker_info WHERE useflg='t' ORDER BY dispno;";
	$resWrkName = pg_query($conn, $selectWrkName);
	$rowcnt = pg_num_rows($resWrkName);
	for($i = 0; $i < $rowcnt; $i++)
	{
		$rowWrkName = pg_fetch_assoc($resWrkName, $i);
		$selectState = "SELECT pid, recvid, state, update FROM nakagusuku.worker_position WHERE tagid='". $rowWrkName["tagid"]. "';";
		$resState = pg_query($conn, $selectState);
		$rowcntState = pg_num_rows($resState);
		if ($rowcntState < 1)
			continue;
		$rowState = pg_fetch_assoc($resState, 0);
		
		
		print "<tr align=\"center\">";
		print "<td>". $rowWrkName["dispno"]. "</td>";
		print "<td>". $rowWrkName["wname"]. "</td>";
		print "<td>". $vstatus[$rowState["state"]]. "</td>";
		
		print "<td>";
		print "<form method=\"post\" style=\"display:inline;\">";
		//print "<a href=\"./info.php?dispno=". $row["pid"]. "\">". $row["dispno"]. "</a></td>"
		print "<input type=\"submit\" name=\"btnchstate\" value=\"切替\">";
		print "<input type=\"hidden\" name=\"hipid\" value=\"". $rowState["pid"]. "\">";
		print "<input type=\"hidden\" name=\"hinowstate\" value=\"". $rowState["state"]. "\">";
		print "</form>";
		print "</tr>";
	}
	print "</table></center>";
}
catch(Exception $exc)
{
	
}
pg_close($conn);
print "<p style=\"text-align:right;\">";
print "<a href=\"../index.html\">戻る</a>";
print "</p>";
?>
