<?php
include("includes/config.php");
include("includes/func.php");
include("classes/user.class.php");
include("classes/safeSQL.class.php");
include('classes/readxml.class.php');
$Get_XML_Responce	= new read_xml;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>EQUIVALEN ZA</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="javascripts/jquery.min.js"></script>
<style type="text/css">
#mainPanel{
	width:650px !important;
	height:340px !important;	
}

body{
	overflow-y:hidden !important;
}
</style>
</head>

<body>
<?php 
if (($_SESSION['id'] == ""))
{
	redirect("login");
}


$user 				= new user;
$safeSQL 			= new safeSQL;

$action 			= '';
$actionToExec 		= '';
$errorMsg 			= '';
$_SESSION['msg'] 	= '';
$page_title 		= '';
$page_heading 		= '';
$mui 				= '';
$btn 				= '';

if (isset($_GET['act'])) {
	$action = $safeSQL->$_GET['act'];
}

$page_title 	= $Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'Add_New_User');
$page_heading 	= $Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'Add_New_User');
$mui 			= 'add';
$btn_txt		= $Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'Save');

if ($action == 'delete')
	{
		if(!is_numeric($safeSQL->$_GET['uid'])) {
			$_SESSION['msg'] = "Error occured while retreving data.";
			redirect("user_management.php");
			
		}
		
		$user_id = intval($safeSQL->$_GET['uid']);
		
		$res = $user->deleteUser($user_id);
		
		if ($res === true) 
			{
				$_SESSION['msg'] = "users has been deleted Successfully.";
				redirect("user_management.php");
				
			}
		else
			{
				$errorMsg = $res;
			}
	}
	
if ($action == 'edit')
	{

		if(!is_numeric($safeSQL->$_GET['uid'])) {
			$_SESSION['msg'] = "Error occured while retreving data.";
			redirect("stock_out.php");
			
		}
		
		$user_id = intval($safeSQL->$_GET['uid']);
		
		$Sql_Get	=	"SELECT * FROM `message` where `id` = '$user_id'";		
		$Exe_Sql_Get	=	mysql_query($Sql_Get);
		$Get_Data	=	mysql_fetch_array($Exe_Sql_Get);
		if ($Get_Data) 
			{
				$message 		= $Get_Data['message'];
				$message_id 	= $Get_Data['id'];
			}
		
		$page_title 	= 'Edit Message';
		$page_heading 	= 'Edit Message';
		$mui 			= 'update';
		$btn_txt		= 'Update';
	}
	
	
if (isset($_POST['mui'])) {
	$actionToExec = $safeSQL->$_POST['mui'];
}

if ($actionToExec == 'add')
	{			

			$User_ID		= $_SESSION['id'];	
			$message_for_admin 	= $safeSQL->$_POST['message_for_admin'];
						
			if ($User_ID != '') {

				$Sql	=	"INSERT INTO `message` (`message`, `user`, `current_date`) VALUES ('$message_for_admin', '$User_ID', NOW())";
				$exe_sql	=	mysql_query($Sql);

				$Last_Inserted_ID	=	mysql_insert_id();
				
				echo "<script type='text/javascript'>";
					
					echo "var last_row_count = window.parent.document.getElementById('count_mesage_row').value;";
					echo "last_row_count = parseInt(last_row_count) + 1;";
					echo "window.parent.document.getElementById('count_mesage_row').value = last_row_count;";
										
					$Edit	=	'<a id="fancybox-manual-b" class="fancybox fancybox.iframe" href="Send_Message.php?act=edit&uid='.$Last_Inserted_ID.'"><img src="images/edit.png" alt="Edit" border="0"></a>';
					
					
					$delete	=	'<img onclick="delete_message_row('.$Last_Inserted_ID.');" src="images/delete.png" alt="Delete" />';				
					$tr_id	=	"message_row_$Last_Inserted_ID";
					$td_id	=	"message_id_$Last_Inserted_ID";
					$Input_Hidden	=	'<input type="hidden" name="message_check[]" id="message_check" value="'.$Last_Inserted_ID.'" />';					
										
					echo "var last_row	=	'';";		
					echo "last_row	= 	last_row + '<tr  id=".$tr_id.">';";
       				 	echo "last_row	= 	last_row + '<td>'+last_row_count+'</td>';";
						echo "last_row	= 	last_row + '<td id=".$td_id.">".$message_for_admin."</td>';";							
        				echo "last_row	= 	last_row + '<td>".date('Y-m-d')."</td>';";
						echo "last_row	= 	last_row + '<td>".$Edit.$delete."</td>';";          
      				echo "last_row	= 	last_row + '</tr>';";
					
					echo "last_row	= 	last_row + '".$Input_Hidden."';";
					
					
					
					echo '$("#tabling51 tr:last", window.parent.document).after(last_row);';
					echo '$("table#tabling51 tr#no_message_found", window.parent.document).remove();';
					
					echo 'parent.$.fancybox.close();';
				echo "</script>";
			
			}	 
			
	}

if ($actionToExec == 'update')
	{

		$message_for_admin		= $safeSQL->$_POST['message_for_admin'];
		$Rect_ID				= $_POST['message_id'];
		$User_ID				= $_SESSION['id'];
		
		if ($User_ID != '') {

			$Sql	=	"UPDATE `message` SET `message` = '$message_for_admin', `user` = '$User_ID', `current_date` = NOW() WHERE `id` = $Rect_ID";
			$exe_sql	=	mysql_query($Sql);
			
			echo "<script type='text/javascript'>";
				
				echo "window.parent.document.getElementById('message_id_".$Rect_ID."').innerHTML = '".$message_for_admin."';";
				echo 'parent.$.fancybox.close();';
			echo "</script>";			
			
		} 
	}
		
?>





<?php $page_title ?>

<script type="text/javascript">
$(document).ready(function(e) {
			
		$("#frmUser").submit(function(){
		
					
					var first_name		=	$('#message_for_admin').val();
					

					if(first_name == "")
					{
						alert('Please enter some text');
						return false;
					}
					
																					
						$('#submitButton').attr("disabled", "false");
						return true;		
		});	
});
	
</script>
<!-- Start main box area in contains form-->
<div id="mainPanel">
<!-- Start form -->
	<h2>Write Message</h2>

<div id="form">
	<form id="frmUser" name="frmUser" action="" method="post">
    	<input type="hidden" name="mui" id="mui" value="<?php echo $mui ?>" />
        <input type="hidden" name="message_id" id="message_id" value="<?php echo $message_id; ?>" />
        
    <table id="tabling1" width="100%" border="0" cellspacing="0" cellpadding="0">
        <?php if ($errorMsg != '') { ?>
        <tr>
            <td colspan="2"><span style="color:#ff0000; line-height:20px"><?php echo $errorMsg; ?></span></td>
        </tr>
        <?php } ?>        
        <tr> 
             <td colspan="2" style="text-align:center">
                &nbsp;<textarea name="message_for_admin" id="message_for_admin" rows="7" cols="70"><?php echo $message; ?></textarea><span id="re_pass_error"></span>
            </td>
        </tr>
        <tr> 
            <td colspan="2" nowrap style="text-align:center">
                <input type="submit" name="submitButton" class="form_button" id="submitButton" value="<?php echo $btn_txt ?>" >
            </td>
        </tr>
    </table>
		</form>
		
</div><!-- end form -->

</div><!-- end main box -->
<?php include("templates/footer.php"); ?>