<?php
session_start();

include_once('config.php');
include_once("mysql_interface.php");
?>
<html>
	<head>
		<title>UniMate PKI Token Demo Database Management System</title>
	</head>
	<body bgcolor=#eeffaa>
			<?php
			//check login
			if(!isset($_SESSION['valid_optor']))
			{
				echo "You have not login, please login first";
				?>
				<meta http-equiv="refresh" content="3;url=index.php">
				<?php
			}
			else//already login
			{
				if(!isset($_POST['RecordID']))//
				{
					?>
						<div id="userinfo_query_form">
							<form action=<?php echo $_SERVER['PHP_SELF'];?> method="post">
								<font style="font-weight:bold;color:#228822">User information query</font><br />
								<font style="color:#228822">RecordID</font><input type="text" name="RecordID" /><br />
								<input type="submit" value="Submit">
							</form>
						</div>
					<?php
				}
				else
				{
					try{
						
									$token_db = new mydb();//create db obj
									if(!($token_db->connect($_config['host'], $_config['dbuser'], $_config['passwd'], $_config['port'], $_config['dbname'])))//db connection
									{
										throw new Exception(-2);
									}
									if("" == $_POST['RecordID'])
									{
										$sql = "select * from UniMateToken_UserInfo";
									}
									else
									{
										$sql = "select * from UniMateToken_UserInfo where  RecordID = '".$_POST['RecordID']."'";
									}
								
									if(!($result = $token_db->query($sql)))//sql execute
									{
										throw new Exception(-3);
									}
									if(!($re = $token_db->fetch_row($result)))//no items
									{
										echo "no related items";

										?>
											<meta http-equiv="refresh" content=3";url=userinfo_manage.php">
										<?php
										exit;
									}
									?>
									<font style="font-weight:bold;font-size:18px;">Related Items</font>(name:<?php echo $_POST['ID'];?>)
									<table border=1 id="item_list">
										<tr>
											<th>RecordID</th><th>Credential</th><th>Active</th><th>UniMateID</th><th>UserID</th><th>UserInfo</th><th>Token Type</th><th>Random Number</th><th>Current State</th>
										</tr>
									<?php
									$i = 0;
									do
									{
										?>
											<tr>
										<?php
												foreach($re as $value)
												{
													if("" == $value)
													{
														$value = "NULL";
													}
													echo '
																<td>'.$value.'</td>
													';
												}
												if($re[2])// activated
												{
													?>
													<td><input id=<?php echo $re[0]; ?> type="checkbox" checked value="0" onclick="updateval(this)" /></td>
													<?php
												}
												else
												{
													?>
													<td><input id=<?php echo $re[0]; ?> type="checkbox" value="0" onclick="updateval(this)" /></td>
													<?php
												}
										?>
											</tr>
										<?php
										++$i;
									}while(($re = $token_db->fetch_row($result)));
									?>
								</table>
								<input type="button" onclick="do_change()" value="Execute">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<a href="userinfo_manage.php">return</a>
									<?php
						}
						catch(Exception $e)
						{
							switch($e->GetMessage())
							{
								case -2:
									echo "Connect Database failed";
									exit;
								case -3:
									echo "SQL statement execute failed";
									exit;
								
							}
						}
				}
			}
			?>
	</body>
</html>
<script language="javascript">

	function do_change()
	{
		tb = document.getElementById("item_list");
		rowcount = tb.rows.length;
		colcount = tb.rows(0).cells.length;
		ckval = colcount - 1;
		strid = "";
		strckd = "";

		for(i = 1; i < rowcount; ++i)
		{
			//ck = document.getElementById(tb.rows(i).cells(0).innerText);
			ck = tb.rows(i).cells(8).childNodes(0);
			if("1" == ck.value)
			{
				strid +=  "|" + tb.rows(i).cells(0).innerText;
				if(ck.checked)
				{
					strckd += "|" + "1";
				}
				else
				{
					strckd += "|" + "0";
				}
			}
		}
		if("" != strid)
		{
	  	window.location.href="update.php?strid="+strid+"&strckd="+strckd;
	  }
	  else
	  {
	  	alert("no item changed!");
	  }

	}	
	
	function updateval(obj)
	{
		ck = obj;
		ck.value = ++(ck.value)%2;
	}
</script>