<?php
require '../2_Projects/FileOperations.php';
$con = mysql_connect($website,$userid,$pw);
if (!$con) {
	die('Could not connect: ' . mysql_error());
}

$curDB = "../CurrentlyOpenedDBName.tmp";
if(file_exists ($curDB)){
	$f = fopen($curDB, "r");
	$dbase   = trim(fgets($f)); // linked database name
	fclose($f);
	mysql_select_db($dbase, $con);
}else{
	return;
}
mysql_query("SET foreign_key_checks = 0",$con);
echo ("<head><link rel='stylesheet' type='text/css' href='../CSS/Font.css'></head>");
if($debug)echo ("<script>  document.write(document.URL); </script>");
$output_id = $_GET[output_id];
$operation_id = $_GET[operation_id];

if($_GET[act] == "yes"){
	if(isset($_POST[NotUpdateTable])){
	}elseif(isset($_POST[UpdateTable])){
		// Choose operations to be output to xml if conditional output
		if($_POST[outputField] == "if true" || $_POST[outputField] == "if false"){
			echo ("<h3> These results will be output to xml</h3>");
			foreach($_POST['cond_output_field'] as $out_id){

			}
			exit;
			//$_POST[nameField]=
		}
		if($operation_id == "add"){
			$res = mysql_query("SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = '{$dbase}' AND TABLE_NAME = 'Operation'",$con);
			mysql_query("INSERT INTO Operation(output,selectionA,operation,selectionB,output_id)
						VALUES('{$_POST[outputField]}','{$_POST[selAField]}',
						'{$_POST[operField]}','{$_POST[selBField]}',{$output_id})",$con);
			//$res = mysql_query("SELECT LAST_INSERT_ID()",$con);
			while($row = mysql_fetch_array($res))$mi = $row[0];
		}else{
			mysql_query("UPDATE Operation SET 	output = '{$_POST[outputField]}',
												selectionA = '{$_POST[selAField]}',
												operation = '{$_POST[operField]}',
												selectionB = '{$_POST[selBField]}',
												output_id = {$output_id}
										WHERE operation_id = {$operation_id}",$con);
			$mi = $operation_id;
		}
		if($_POST[nameField] == NULL || $_POST[nameField] == "")$_POST[nameField] = "result" . $mi;
		mysql_query("UPDATE Operation SET name = '{$_POST[nameField]}' WHERE operation_id = {$mi}",$con);
	}
	echo ("<script>	window.opener.document.location.href ='Operation.php?output_id={$output_id}';</script>");
	echo("<script> window.close() </script>");
}

if($operation_id != "add"){ // User selected to edit
	$res = mysql_query("SELECT name,output,selectionA,operation,selectionB FROM Operation WHERE operation_id={$operation_id}",$con);
	while($row = mysql_fetch_array($res)){
		$name = $row[0];
		$output = $row[1];
		$selectionA = $row[2];
		$operation = $row[3];
		$selectionB = $row[4];
	}
	echo "name = $name , output = $output , selA = $selectionA , oper = $operation , selB = $selectionB <br />";
}

echo ("<form action='AddEditOperation.php?act=yes&operation_id={$operation_id}&output_id={$output_id}' method='post'>");
echo ("<table>");

// Operand A field
echo ("<tr>");
echo ("<td><font size='2'>Selection</font></td>");
echo("<td>
<select name='selAField'>
<option disabled>Relevant scopes</option>");
$resulty = mysql_query("SELECT attribute,scope_id FROM Scope WHERE output_id =".$_GET[output_id],$con);
while($row = mysql_fetch_array($resulty)){
	if($row[0] == "entity")continue;
	$scope_id = $row[1];
	////if($selectionA == $row[0]){
	if($selectionA == $scope_id){
		echo("<option selected='selected' value=' {$scope_id}'>{$row[0]}</option>");
	}else{
		echo("<option value='{$scope_id}'>$selectionA $scope_id {$row[0]}</option>");
	}
}
echo("<option disabled>Previous results</options>");
if($operation_id == "add"){
	$resulty = mysql_query("SELECT name,operation_id FROM Operation WHERE output_id = {$_GET[output_id]}",$con);
}else{
	$resulty = mysql_query("SELECT name,operation_id FROM Operation WHERE output_id = {$_GET[output_id]}
				AND operation_id < {$operation_id}",$con);
}
while($row = mysql_fetch_array($resulty)){
	$oper_id = intval($row[1]);
	$oper_id = -1 * $oper_id;
	if($selectionA == $oper_id){
		echo("<option selected='selected' value='{$oper_id}'>{$row[0]}</option>");
	}else{
		echo("<option value='{$oper_id}'>{$row[0]}</option>");
	}
}
echo("</select></td>");
echo ("</tr>");

// Operation field
echo ("<tr>");
echo ("<td><font size='2'>Action</font></td>");
echo("<td>
<select name='operField'>
<option>None</option>");

$ops = array(	"IO",
				"Count",
				"List",
				//"Count (raw)",
				"List (raw)",
				"Arithmetic operations",
				"Integrate",
				"Sum",
				"Plus",
				"Minus",
				"Multiply",
				"Divide",
				"Equals",
				"Greater than",
				"Not less than",
				"Less than",
				"Not greater than",
				"Statistical operations",
				"Maximum",
				"Minimum",
				"Mean",
				"Median",
				"Mode",
				"Standard deviation",
				"5th percentile",
				"10th percentile",
				"25th percentile",
				"75th percentile",
				"90th percentile",
				"95th percentile",
				"Logical operations",
				"AND",
				"OR",
				"NOT",
				);

foreach ($ops as $value){
	$symbol = $value;
	$disabled = "";
	if($value == "IO" || $value == "Arithmetic operations" || $value == "Statistical operations" || $value == "Logical operations"){
		$disabled = "disabled";
	}elseif($value == "Plus"){
		$symbol = "+";
	}elseif($value == "Minus"){
		$symbol = "-";
	}elseif($value == "Multiply"){
		$symbol = "*";
	}elseif($value == "Divide"){
		$symbol = "/";
	}elseif($value == "Greater than"){
		$symbol = ">";
	}elseif($value == "Not less than"){
		$symbol = ">=";
	}elseif($value == "Less than"){
		$symbol = "<";
	}elseif($value == "Not greater than"){
		$symbol = "<=";
	}

	if($operation == $value || $operation == $symbol){
		echo("<option selected='selected' $disabled name=$value>{$symbol}</option>");
	}else{
		echo("<option $disabled  name=$value>{$symbol}</option>");
	}
}
echo ("</select></td>");
echo ("</tr>");

//Operand B field
echo ("<tr>");
echo ("<td><font size='2'>Supplement</font></td>");
echo("<td>
<select name='selBField'>
<option disabled>Relevant scopes</option>
<option></option>");
$resulty = mysql_query("SELECT attribute,scope_id FROM Scope WHERE output_id =".$_GET[output_id],$con);
while($row = mysql_fetch_array($resulty)){
	if($row[0] == "entity")continue;
	$scope_id = $row[1];
	if($selectionB == $scope_id){
		echo("<option selected='selected' value='{$scope_id}'>{$row[0]}</option>");
	}else{
		echo("<option value='{$scope_id}'>{$row[0]}</option>");
	}
}
echo("<option disabled>Previous results</options>");
if($operation_id == "add"){
	$resulty = mysql_query("SELECT name,operation_id FROM Operation WHERE output_id = {$_GET[output_id]}",$con);
}else{
	$resulty = mysql_query("SELECT name,operation_id FROM Operation WHERE output_id = {$_GET[output_id]}
				AND operation_id < {$operation_id}",$con);
}
while($row = mysql_fetch_array($resulty)){
	$oper_id = intval($row[1]);
	$oper_id = -1 * $oper_id;
	if($selectionB == $row[0]){
		echo("<option selected='selected' value='{$oper_id}'>" .$row[0]. "</option>");
	}else{
		echo("<option value='{$oper_id}'>" .$row[0]. "</option>");
	}
}
echo("</select></td>");
echo ("</tr>");

// Name field
echo ("<tr>");
echo ("<td><font size='2'>Result</font></td>");
echo 	("<td><input 	type='text'  autocomplete='off'
						name='nameField'
						value='{$name}'
						maxlength = '{$smaStr}'
						size = '60'
						placeholder='Optional - EnTrak will generate if not provided'>
		</td>");
echo ("</tr>");

//Output to xml?
echo ("<tr>");
echo ("<td><font size='2'>Output</font></td>");
echo("<td>
<select name='outputField'>");
if($output == "yes"){
	$yes_sel = "selected";
}elseif($output == "no"){
	$no_sel = "selected";
}elseif($output == "if true"){
	$true_sel = "selected";
}elseif($output == "if false"){
	$false_sel = "selected";
}
echo ("<option $yes_sel> yes </option>");
echo ("<option $no_sel> no </option>");
echo ("<option $true_sel> if true </option>");
echo ("<option $false_sel> if false </option>");

echo("</select></td>");
echo ("</tr>");

echo ("<tr><th rowspan='2'>");
echo ("</table>");
echo ("Choose results that will be output if this condition is true/false</th></tr>");
//echo ("Conditional output")
echo ("<table><tr><td><font size='6'>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</font></td><td>");
echo("<select multiple='multiple' name='cond_output_field[]'>");
if($operation_id == "add"){
	$resulty = mysql_query("SELECT name,operation_id FROM Operation WHERE output_id = {$_GET[output_id]}",$con);
}else{
	$resulty = mysql_query("SELECT name,operation_id FROM Operation WHERE output_id = {$_GET[output_id]}
				AND operation_id < {$operation_id}",$con);
}
while($row = mysql_fetch_array($resulty)){
	$oper_id = intval($row[1]);
	$oper_id = -1 * $oper_id;
	if($selectionB == $row[0]){
		echo("<option selected='selected' value='{$oper_id}'>" .$row[0]. "</option>");
	}else{
		echo("<option value='{$oper_id}'>" .$row[0]. "</option>");
	}
}
echo("</select></td></tr>");
echo ("</table>");
echo ("<button type='submit' name='UpdateTable' value='OK' >OK</button>&nbsp");
echo ("<button type='submit' name='NotUpdateTable' value='Cancel' >Cancel</button>");
echo ("<form><br/>");
?>
