<?php 
include 'session.php';
extract($_REQUEST);
$act=$action;

if($chkstatus !=null)
	$status =1;
else
	$status =0;

switch($act)
{
	case 'insert':
	
	if(!empty($txtModulename) ) {
		$strChk = "select count(ModuleId) from a_modules where ModuleName = '$txtModulename' and IsActive != '2'";		
 		$reslt = $db->get_a_line($strChk);
		if($reslt[0] == 0) {
			
			$str="insert into a_modules(ModuleName,Description,ModulePath,IsActive,UserId) values('".getRealescape($txtModulename)."','".getRealescape($txtModuledescription)."','".getRealescape($txtModulepath)."','".$status."','".$_SESSION["UserId"]."')";
			$rslt = $db->insert($str);	
			
			$log = $db->insert_log("insert","a_modules","","Module Added Newly","module",$str);
			
			//echo json_encode(array("rslt"=>$rslt)); //success
			echo json_encode(array("rslt"=>"1")); //success
		}
		else {
			 echo json_encode(array("rslt"=>"3")); //same exists
		}
	}
	else {
		echo json_encode(array("rslt"=>"4"));  //no values
	}
	
	break;
	
	
	case 'update':	 	
	//$edit_id
	$today=date("Y-m-d");	
	if(!empty($txtModulename) ) {
		$strChk = "select count(ModuleId) from a_modules where ModuleName = '$txtMenuname' and IsActive != '2' and ModuleId != '".$edit_id."' ";
 		$reslt = $db->get_a_line($strChk);
		if($reslt[0] == 0) {
			$str = "update a_modules set ModuleName = '".getRealescape($txtModulename)."', Description = '".getRealescape($txtModuledescription)."', ModulePath = '".getRealescape($txtModulepath)."', IsActive = '".$status."', ModifiedDate = '$today' , UserId='".$_SESSION["UserId"]."'  where ModuleId = '".$edit_id."'";
			$db->insert($str);
			$db->insert_log("update","a_modules",$edit_id,"Module  updated","module",$str);

			echo json_encode(array("rslt"=>"2"));
		}
		else {
			echo json_encode(array("rslt"=>"3")); //same exists
		}
	}
	else {
		echo json_encode(array("rslt"=>"4"));  //no values
	}
		
	break;
	
	case 'del':
	  $edit_id = base64_decode($Id);
	  
	  $today = date("Y-m-d");
	  $str="update a_modules set IsActive = '2', ModifiedDate = '$today' , UserId='".$_SESSION["UserId"]."'  where ModuleId = '".$edit_id."'  and ModuleId NOT IN(1,2,3) ";
	  $db->insert($str); 	 
	  
	  $db->insert_log("delete","a_modules",$edit_id,"Module deleted","Module",$str);
 	  echo json_encode(array("rslt"=>"5")); //deletion
	  
	break;
	
	case 'changestatus':
		$edit_id = base64_decode($Id);
		$today = date("Y-m-d");
		$status = $actval;
		
		if($edit_id !="1" && $edit_id !="2" && $edit_id !="3"){			
			$str="update a_modules set IsActive = '".$status."', ModifiedDate = '$today' , UserId='".$_SESSION["UserId"]."'  where ModuleId = '".$edit_id."' and ModuleId NOT IN(1,2,3) ";
			$db->insert($str); 		
			echo json_encode(array("rslt"=>"6")); //status update success		
		}
		else{		 
			echo json_encode(array("rslt"=>"7")); // cannot change status	  
		}
			
	
	break;
}



?>