<?php
require("../../inc/header.inc");
require("./comm_func.php");
?>

<body leftmargin="15" rightmargin="10" onload="javascript:adjust();" onresize="javascript:adjust();" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;">
<?php
	adjust_head();

	adjust_title_head();
  echo "<form name=\"statusForm\" method=\"post\">";
  echo "<table id=\"table_up\" border=\"0\" width=\"100%\">";
	echo "<tr>";
		echo "<td width=\"50%\">";
		echo "Template > Function Wizard";
		echo "</td>";
		echo "<td width=\"50%\" align=\"right\">";
		echo "&nbsp;";
		echo "</td>";
	echo "</tr>";
	echo "<tr>";
		echo "<td width=\"50%\">&nbsp;</td>";
		echo "<td width=\"50%\" align=right>";
		echo "&nbsp;";
		echo "</td>";
	echo "</tr>";
	echo "</table>";
	
	adjust_title_tail();
	adjust_content_head();
	
	if($create_wizard_flag == 1)
	{
		$wizard_desc=discardReturn($wizard_desc);
		$sql="insert into WIZARD_DB.wizardList(func_title,func_desc) values('$wizard_title','$wizard_desc') ";
		@mysqli_query($pubConn,$sql);
		
		$sql="select instance from WIZARD_DB.wizardList where func_title='$wizard_title' ";
		$res=@mysqli_query($pubConn,$sql);
		$num=@mysqli_num_rows($res);
		$row=@mysqli_fetch_array($res);
		$wizard_instance=$row[instance];
		for($j=0;$j<$step_count;$j++)
		{
			$param="step_title_".$j;
			$step_title=$$param;
			$param="step_desc_".$j;
			$step_desc=$$param;
			$step_desc=discardReturn($step_desc);
			$param="step_related_system_".$j;
			$step_related_system=$$param;
			$param="step_related_table_".$j;
			$step_related_table=$$param;
			$sql="insert into WIZARD_DB.stepList(step_order,step_title,step_desc,related_system,related_table,wizard_instance) values($j,'$step_title','$step_desc',$step_related_system,'$step_related_table',$wizard_instance) ";
			@mysqli_query($pubConn,$sql);
		}
	
		
		if(0)
		{
			echo "wizard_title=$wizard_title<br>";
			echo "wizard_desc=$wizard_desc<br>";
			echo "step_count=$step_count<br>";
			for($j=0;$j<$step_count;$j++)
			{
				$param="step_title_".$j;
				echo "step_title_{$j}={$$param},";
				$param="step_desc_".$j;
				echo "step_desc_{$j}={$$param},";
				$param="step_related_system_".$j;
				echo "step_related_system_{$j}={$$param},";
				$param="step_related_table_".$j;
				echo "step_related_table_{$j}={$$param}<br>";
			}
		}
	}
	
	if($delete_wizard_flag == 1)
	{
		$sql="delete from WIZARD_DB.stepList where wizard_instance={$wizard_instance} ";
		@mysqli_query($pubConn,$sql);
		
		$sql="delete from WIZARD_DB.wizardList where instance={$wizard_instance} ";
		@mysqli_query($pubConn,$sql);
		//echo "wizard_instance=$wizard_instance<br>";
		
		//delete the related wizard record
		$sql="select instance from WIZARD_DB.wizardRecordList where wizard_instance={$wizard_instance} ";
		$res=@mysqli_query($pubConn,$sql);
		$num=@mysqli_num_rows($res);
		if($num > 0)
		{
			for($i=0;$i<$num;$i++)
			{
				$row=@mysqli_fetch_array($res);
				$tmpi=$row[instance]+0;
				$delsql="delete from WIZARD_DB.stepRecordList where record_instance={$tmpi} ";
				@mysqli_query($pubConn,$delsql);
			}
		}
		$delsql="delete from WIZARD_DB.wizardRecordList where wizard_instance={$wizard_instance} ";
		@mysqli_query($pubConn,$delsql);
		
		
	}
	
	if($modify_wizard_flag == 1)
	{
		$wizard_desc=discardReturn($wizard_desc);
		$sql="update WIZARD_DB.wizardList set func_title='$wizard_title',func_desc='$wizard_desc' where instance={$wizard_instance} ";
		@mysqli_query($pubConn,$sql);
		
		$sql="delete from WIZARD_DB.stepList where wizard_instance=$wizard_instance ";
		@mysqli_query($pubConn,$sql);
		
		for($j=0;$j<$step_count;$j++)
		{
			$param="step_title_".$j;
			$step_title=$$param;
			$param="step_desc_".$j;
			$step_desc=$$param;
			$step_desc=discardReturn($step_desc);
			$param="step_related_system_".$j;
			$step_related_system=$$param;
			$param="step_related_table_".$j;
			$step_related_table=$$param;
			$sql="insert into WIZARD_DB.stepList(step_order,step_title,step_desc,related_system,related_table,wizard_instance) values($j,'$step_title','$step_desc',$step_related_system,'$step_related_table',$wizard_instance) ";
			@mysqli_query($pubConn,$sql);
		}
	}
	
	echo "<br>";
	echo "<a href=\"./wizard_param_add.php?type=new\">Create New Function Wizard<a/>";
	
	$wizardList=GetWizardList();
	
	echo "<ul>";
	
	for($i=0;$i<$wizardList[WizardCount];$i++)
	{
		echo "<li>";
		echo "<a href=\"./wizard_param_edit.php?wizard_instance={$wizardList[$i][WizardInstance]}\">{$wizardList[$i][WizardTitle]}</a>";
		echo "</li>";
	}
	echo "</ul>";
	
	adjust_content_tail();
?>

