<?php
include('../includes/con2pma3.php');
$var = "";
if(isset($_REQUEST['Check']))
{
	session_name("AdminSession");
	//session_start();
	
	if (isset($_SESSION['loggedin']) && ($_SESSION['loggedin']==true))
	{
		$id = 0;
		$var = "&Check=1";
	}
	else
	header("location: ../Admin/index.php?msg=YOU ARE NOT LOGGED IN");

}
else
{
	//include('../includes/checkSession.php');
	$id = $_SESSION['id'];
	$var = "";
}
$WBSID = $_REQUEST['WBSID'];
$WID = $_REQUEST['WID'];
//include('../includes/con2db.php');
$PNode =  $_POST['PNode'];
$description =$_POST['WBSD'];
$writeupitem = $_POST['WBSN'];
$creationDate =  date('Y-m-d');
$unit =  $_POST['unit'];
$itemList =  $_POST['itemList'];
if(isset($_REQUEST['del'])){
$itemID = $_REQUEST['del'];
if(mysql_query("delete from globalitems WHERE `itemID` = '$itemID'"))
{
	mysql_query("delete from globalitems WHERE `itemParent` = '$itemID'");
header("location: WBSITEM1.php?msg= DELETED&WBSID=$WBSID".$var."&WID=$WID".$var);
}
else
{
	header("location: WBSITEM1.php?msg=NOT DELETED&WBSID=$WBSID".$var."&WID=$WID".$var);
	}
}
else if(isset($_REQUEST['Edit'])){
$itemid = $_REQUEST['Edit'];
$rdir = $_REQUEST['rdir'];
if ($itemList==0)
	{
		if(mysql_query("UPDATE globalitems SET WBSID = '$PNode', itemParent = '0', sortCode = 'ITEM', dateCreated = '$creationDate', description = '$description', writeupitem='$writeupitem' , unitID = '$unit' where itemID = '$itemid'"))
		{
			$resultit = mysql_query("SELECT itemID FROM globalitems where itemParent = '$itemid'");
   		while($row = mysql_fetch_array($resultit , MYSQL_ASSOC))
		{
			$itemIDU= $row["itemID"];
			mysql_query("UPDATE globalitems SET WBSID = '$PNode' where itemID = '$itemIDU'");			
		}
			header("location: ".$rdir.".php?msg= UPDATED&WBSID=$WBSID".$var."&WID=$WID".$var);

		}
		else
		{
			header("location: ".$rdir.".php?msg= NOT UPDATED&WBSID=$WBSID".$var."&WID=$WID".$var);
		}
	}
	else
	{
				if(mysql_query("UPDATE globalitems SET WBSID = '$PNode', itemParent = '$itemList', sortCode = 'SITEM',  dateCreated = '$creationDate', description = '$description', writeupitem='$writeupitem' , unitID = '$unit' where itemID = '$itemid'"))
		{
			header("location: ".$rdir.".php?msg= UPDATED&WBSID=$WBSID".$var."&WID=$WID".$var);
		}
		else
		{
			header("location: ".$rdir.".php?msg= NOT UPDATED&WBSID=$WBSID".$var."&WID=$WID".$var);
		}
		}
}
else{
	if ($itemList==0)
	{	
		 $itemList;
		if(mysql_query("insert into globalitems values('NULL','ITEM','$PNode','$description','$writeupitem','0','$creationDate','$unit','$id')"))
		{
			header("location: WBS2.php?msg=ADDED&WID=$WID".$var);	

		}
		else
		{
			//echo "data not inserted...";
			header("location: WBSITEM1.php?msg=ADDED:  ERROR&WBSID=$WBSID".$var."&WID=$WID".$var);
		}
	}
	else
	{

		if(mysql_query("insert into globalitems values('NULL','SITEM','$PNode','$description','$writeupitem','$itemList','$creationDate','$unit','$id')"))
		{
			header("location: WBS2.php?msg=ADDED&WID=$WID".$var);
		}
		else
		{
			//echo "data not inserted...";
			header("location: WBSITEM1.php?msg=ADDED:  ERROR&WBSID=$WBSID".$var."&WID=$WID".$var);
		}
		}
}

?> 