<?php
ini_set("display_startup_errors","1");
ini_set("display_errors","1");
error_reporting(E_ALL);


require ("mysqlconnect.inc.php");
include "header.php";

$JSINCLUDE = "mmanager.js";

/******* Init the interface  ******/
session_start(); //web side
if (isset($_REQUEST['mname']))
	$mname = $_REQUEST['mname'];
else $mname = "";
if (isset($_REQUEST['mip']))
	$mip = $_REQUEST['mip'];
else $mip = "";
if (isset($_REQUEST['msoft']))
	$msoft = $_REQUEST['msoft'];
else $msoft = "";
if (isset($_REQUEST['mbusy']))
	$mbusy = $_REQUEST['mbusy'];
else $mbusy = "";
if (isset($_REQUEST['mtimes']))
	$mtimes = $_REQUEST['mtimes'];
else $mtimes = "";
if (isset($_REQUEST['state']))
	$state = $_REQUEST['state'];
else $state = "";

if((isset($_POST['add']))) {
$sta = addMch($mname, $mip, $msoft, $mbusy, $mtimes, $state);
} elseif ((isset($_POST['rem']))){
$sta = remMch($mip);
} elseif ((isset($_POST['mod']))) {
$sta = modMch($mname, $mip, $msoft, $mbusy, $mtimes, $state);
}

?>


<html>
<head>
<script type="text/javascript" src="<? echo $JSINCLUDE;?>">
</script>
<title> useradmin page</title>
<style type="text/css">@import url(../styles/layout.css);</style>
</head>
<body>
<div id="content">
<br><br><br>
<form name="update" enctype="multipart/form-data" method='post' action="<?$_SERVER['PHP_SELF']?>">
<table>
<tr>
<td>ID:</td><td><input type="span" size="12" maxlength="22" id="id" name="id"></td></tr><tr>
<td>Machine Name:</td><td><input type="span" size="22" maxlength="50" id="mname" name="mname"></td></tr><tr>
<td>IP:</td><td><input type="text" size="22" maxlength="25" id="mip" name="mip"></td></tr><tr>
<td>Software:</td><td><input type="text" size="12" maxlength="100" id="msoft" name="msoft"></td></tr><tr>
<td>Busy:</td><td><input type="text" size="12" maxlength="22" id="mbusy" name="mbusy"></td></tr><tr>
<td>TimeSnapshot:</td><td><input type="text" size="22" maxlength="22" id="mtimes" name="mtimes"></td></tr><tr>
<td>State:</td><td><input type="text" size="12" maxlength="22" id="state" name="state"></td>
</tr>
</table>

<table>
<tr>
<td colspan=2 align=center><input type="submit" value="add" name="add"></td>
<td colspan=1 align=center><input type="submit" value="rem" name="rem"></td>
<td colspan=2 align=center><input type="submit" value="mod" name="mod"></td>
<td colspan=2 align=center><input type="submit" value="reset" name="reset"></td>
</tr>
</table>
</form>




<?
	$dbconnection = mysql_connect($dbhost,$dbuser,$dbpass) or die ("Could not connect!" . mysql_error());
	mysql_select_db($database);	

if (!isset($id)){
echo "";	
} else {



}

?>

<table border=1>
<tr>
<th>&nbsp</th>
<th>Id:</th>
<th>M. Name:</th>
<th>IP:</th>
<th>Software:</th>
<th>Busy:</th>
<th>TimeSnapshot:</th>
<th>State:</th>
</tr>

<?

/* SQL statement to query the database */
$machinetable = $GLOBALS['machinetable'];
$query = "SELECT * FROM $machinetable"; 

/* query the database */
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)){
print("
<tr>
<td><INPUT TYPE=\"radio\" NAME=\"update\" VALUE=\"".$row["iplocation"]."\" onClick=\"if (this.checked) updateSoftInfo('".$row["iplocation"]."');\"></td>
<td>".$row["id"]."</td>
<td>".$row["machinename"]."</td>
<td>".$row["iplocation"]."</td>
<td>".$row["availsoft"]."</td>
<td>".$row["busy"]."</td>
<td>".$row["timesnapshot"]."</td>
<td>".$row["state"]."</td>
</tr>
");

}

?>
</table>
</div>
</body>
</html>
<?

function addMch($mname, $mip, $msoft, $mbusy, $mtimes, $state)
{
print $mname." ".$mip."<br>";
if($mname || $mip) {
	$fields = parseinitfields($GLOBALS['MACHINE_TABLE_FIELDS']);
	$table = $GLOBALS['machinetable'];
        $query = "INSERT INTO $table (".$fields.") VALUES ('$mname', '$mip', '$msoft', '$mbusy', '$mtimes', '$state')";
//	print ($query);
        $result = mysql_query($query);
        if($result) {
                return $result;
        } else {
                return false;
        }
        }
}

function modMch($mname, $mip, $msoft, $mbusy, $mtimes, $state)
{
if($mname) {
        remMch($softname);
	$table = $GLOBALS['machinetable'];
	$fields = parseinitfields($GLOBALS['MACHINE_TABLE_FIELDS']);
        $query = "REPLACE INTO $table (".$fields.") VALUES ('$mname', '$mip', '$msoft', '$mbusy', '$mtimes', '$state')";
        $result = mysql_query($query);

        if($result) {
                return $result;
        } else {
                return false;
        }
        }
}
/* */
function remMch($mip)
{
	$table = $GLOBALS['machinetable'];
        $query = "DELETE FROM $table WHERE iplocation = '$mip'";
//	print "DELETE FROM $table WHERE iplocation = '$mip'";
        if($result = mysql_query($query)) {
                return $result;
        } else {
                return false;
        }
}

?>
