<?php
require("../../inc/header.inc");
?>
<body onload="javascript:adjust();" onresize="javascript:adjust();" leftmargin="15" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;">

<script language="JavaScript">
	function saveObj(operation)
	{
		//var no=document.myform.no_text.value;
		var typ=document.myform.type_text.value;
		var filter=document.myform.filter_text.value;
		var logName=document.myform.logName_text.value;
		var logPass=document.myform.logPass_text.value;
		var srcDir=document.myform.srcDir_text.value;
		var dstDir=document.myform.dstDir_text.value;
		var dstIP=document.myform.dstIP_text.value;
		
		if(filter.length == 0 || logName.length == 0 || logPass.length == 0 
			|| srcDir.length == 0 || dstDir.length == 0 || dstIP.length == 0)
		{
			alert("Please input the intact value!");
			return;
		}
		if(operation == 0)
		{
			window.location.href="./operation.php?save_new_obj=1&type="+typ+"&filter="+filter+"&logName="+logName+"&logPass="+logPass+"&srcDir="+srcDir+"&dstDir="+dstDir+"&dstIP="+dstIP;
		}
		else
		{	
			var instance=document.myform.instanceNo_text.value;
			window.location.href="./operation.php?save_old_obj=1&instance="+instance+"&type="+typ+"&filter="+filter+"&logName="+logName+"&logPass="+logPass+"&srcDir="+srcDir+"&dstDir="+dstDir+"&dstIP="+dstIP;
		}
	}
	
	
	function closeWindow()
	{
		//window.close();
		//window.opener.location.href="./fileTransferConfiguration.php";
		//window.opener.location.href="./fileTransferConfiguration.php";
		
	}
</script>	

<?php
adjust_head();
adjust_title_head();
//echo "<FORM NAME=\"downForm\">";
echo "<table id=\"table_up\" border=\"0\" width=\"100%\">";
	echo "<tr>";
		echo "<td width=\"50%\">";
		echo "Template > File Transfer";
		echo "</td>";
		echo "<td width=\"50%\" align=\"right\" title=\"Help\">";
		//showHelp($helpId);
		echo "</td>";
	echo "</tr>";
echo "</table>";
adjust_title_tail();
adjust_content_head();
echo "<br>";
	$DEBUG=0;
	//debug_log("/tmp/sysCommand.txt","save file transfer object,save_new_obj=$save_new_obj\n");
	
	if($save_new_obj == 1)
	{
		//get the new instanceNo
		$database="OMC_PUB";
		$selSql = "SELECT max(instanceNo) as maxInstanceNo FROM fileTransConf ";
		$selRes = @mysqli_query($pubConn,$selSql);
		$count=mysqli_num_rows($selRes);
		if($count == 1)
		{
			$selRow = @mysqli_fetch_array($selRes);
			$maxInstanceNo=$selRow[maxInstanceNo];
			//echo "maxInstanceNo=$maxInstanceNo<br>";
		}
		//insert the new record
		$newInstaceNo=$maxInstanceNo+1;
		$saveSql="INSERT INTO fileTransConf values('$newInstaceNo','$type','$srcDir','$dstDir','$dstIP','$filter','$logName','$logPass')";
		debug_log("/tmp/sysCommand.txt",$saveSql);
		mysqli_query($pubConn,$saveSql);
		echo mysqli_error($pubConn);
		
		echo "\n<script language=\"JavaScript\">\n";
		echo "window.location.href=\"./fileTransferConfiguration.php\";\n";
		echo "</script>\n";
		
	}
	if($save_old_obj == 1)
	{
		//update the old file transfer object
		$pubDb="OMC_PUB";
		
		$saveSql="UPDATE fileTransConf SET type=$type,srcDir='$srcDir',dstDir='$dstDir',dstIP='$dstIP',filter='$filter',logName='$logName',logPass='$logPass' where instanceNo=$instance ";
		//echo "saveSql=$saveSql<br>";
		mysqli_query($pubConn,$saveSql);
		echo mysqli_error($pubConn);
		
		echo "\n<script language=\"JavaScript\">\n";
		echo "window.location.href=\"./fileTransferConfiguration.php\";\n";
		echo "</script>\n";
	}
	
	
	$typeLabel=array(0=>"FTP",1=>"SFTP");
	echo "<form name=\"myform\" action=\"./operation.php\">";
	if($DEBUG) echo "instanceNo=$instanceNo,showIndex=$showIndex,operation=$operation<br>";
	//Add a new file transfer object
	if($operation=="new")
	{

		echo "<p><table border=\"1\" width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";
		echo "<tr bgcolor=\"#E6E6E6\">";
		echo "<td width=\"50%\">Name</td><td width=\"50%\">Value</td>";
		echo "</tr>";
		
		//No.
		echo "<tr>";
		echo "<td>No.</td>";
		echo "<td>$showIndex</td>";
		echo "<input type=\"hidden\" name=\"no_text\" value=$HpmnMcc>";
		echo "</tr>";
		
		//Type
		echo "<tr>";
		echo "<td>Type</td>";
		$type=0;
		echo "<td><select name=\"type_text\">";
		for($i=0;$i< sizeof($typeLabel);$i++)
		{
			if($i==$type)
				echo "<option value=\"$i\" selected>{$typeLabel[$i]}</option>";
			else
				echo "<option value=\"$i\">{$typeLabel[$i]}</option>";
		}
		echo "</td>";
		echo "</tr>";
		
		//Filter
		echo "<tr>";
		echo "<td>Filter</td>";
		echo "<td><input type=\"text\" name=\"filter_text\" value=\"txt\" class=\"text\"></td>";
		echo "</tr>";
		
		//Login Name
		echo "<tr>";
		echo "<td>Login Name</td>";
		echo "<td><input type=\"text\" name=\"logName_text\" value=\"\" class=\"text\"></td>";
		echo "</tr>";
		
		//Login Pass
		echo "<tr>";
		echo "<td>Login PW</td>";
		echo "<td><input type=\"text\" name=\"logPass_text\" value=\"\" class=\"text\"></td>";
		echo "</tr>";
		
		//Destination IP
		echo "<tr>";
		echo "<td>Destination IP</td>";
		echo "<td><input type=\"text\" name=\"dstIP_text\" value=\"\" class=\"text\"></td>";
		echo "</tr>";
	
		
		//Source Directory
		echo "<tr>";
		echo "<td>Source Directory</td>";
		echo "<td><input type=\"text\" name=\"srcDir_text\" value=\"\" class=\"text\" size=32 maxlength=256></td>";
		echo "</tr>";
		
		//Destination Directory
		echo "<tr>";
		echo "<td>Destination Directory</td>";
		echo "<td><input type=\"text\" name=\"dstDir_text\" value=\"\" class=\"text\" size=32 maxlength=256></td>";
		echo "</tr>";
		
		echo "</table>";
		
		//save button
		echo "<input type=button onClick=\"javascript:saveObj(0);javascript:closeWindow();\" value=\"Save\" class=\"button\">";
	
	}
	else if($operation=="edit")
	{
		//Get the hpmn server info from database
		$database="OMC_PUB";
		$selSql = "SELECT instanceNo,type,srcDir,dstDir,dstIP,filter,logName,logPass FROM fileTransConf where instanceNo='$instanceNo' ";
		$selRes = @mysqli_query($pubConn,$selSql);
		echo mysqli_error($pubConn);
	
		
		$selRow = @mysqli_fetch_array($selRes);
		$fileTransObjInstanceNo=$selRow[instanceNo];
		$fileTransObjType=$selRow[type]+0;
		$fileTransObjSrcDir=$selRow[srcDir];
		$fileTransObjDstDir=$selRow[dstDir];
		$fileTransObjDstIP=$selRow[dstIP];
		$fileTransObjFilter=$selRow[filter];
		$fileTransObjLogName=$selRow[logName];
		$fileTransObjLogPass=$selRow[logPass];
		


		echo "<p><table border=\"1\" width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";
		echo "<tr bgcolor=\"#E6E6E6\">";
		echo "<td width=\"50%\">Name</td><td width=\"50%\">Value</td>";
		echo "</tr>";
		
		//No
		echo "<tr>";
		echo "<td>No.</td><td>$showIndex</td>";
		echo "<input type=\"hidden\" name=\"no_text\" value=$showIndex>";
		echo "<input type=\"hidden\" name=\"instanceNo_text\" value=$fileTransObjInstanceNo>";
		echo "</tr>";
		
		//Type
		echo "<tr>";
		echo "<td>Type</td>";
		echo "<td><select name=\"type_text\">";
		for($i=0;$i< sizeof($typeLabel);$i++)
		{
			if($i==$fileTransObjType)
				echo "<option value=\"$i\" selected>{$typeLabel[$i]}</option>";
			else
				echo "<option value=\"$i\">{$typeLabel[$i]}</option>";
		}
		echo "</td>";
		echo "</tr>";
		
		
		//Filter
		echo "<tr>";
		echo "<td>Filter</td>";
		echo "<td><input type=\"text\" name=\"filter_text\" value=\"$fileTransObjFilter\" class=\"text\"></td>";
		echo "</tr>";
		
		//Login Name
		echo "<tr>";
		echo "<td>Login Name</td>";
		echo "<td><input type=\"text\" name=\"logName_text\" value=\"$fileTransObjLogName\" class=\"text\"></td>";
		echo "</tr>";
		
		//Login Pass
		echo "<tr>";
		echo "<td>Login PW</td>";
		echo "<td><input type=\"text\" name=\"logPass_text\" value=\"$fileTransObjLogPass\" class=\"text\"></td>";
		echo "</tr>";
		
		//Destination IP
		echo "<tr>";
		echo "<td>Destination IP</td>";
		echo "<td><input type=\"text\" name=\"dstIP_text\" value=\"$fileTransObjDstIP\" class=\"text\"></td>";
		echo "</tr>";
	
		
		//Source Directory
		echo "<tr>";
		echo "<td>Source Directory</td>";
		echo "<td><input type=\"text\" name=\"srcDir_text\" value=\"$fileTransObjSrcDir\" class=\"text\" size=32 maxlength=256></td>";
		echo "</tr>";
		
		//Destination Directory
		echo "<tr>";
		echo "<td>Destination Directory</td>";
		echo "<td><input type=\"text\" name=\"dstDir_text\" value=\"$fileTransObjDstDir\" class=\"text\" size=32 maxlength=256></td>";
		echo "</tr>";
		
		
		echo "</table>";
		
		//save button
		echo "<input type=button onClick=\"javascript:saveObj(1);javascript:closeWindow();\" value=\"Save\" class=\"button\">";
		
	}
	adjust_content_tail();
?>