<?php
include_once("../include/main.inc.php");
check_session('MyCpanel','index.php');
@extract($_REQUEST);

if (empty($catID)) {
	$heading="Add New Location";
}
else {
	$heading="Edit Location";
}
$res=getResult("admin_details"," where user_type='admin'");

$record_type="Location";
$script_name="addeditlocation.php";
$redirect_script_name="manage_locations.php";
$table_name="location_tbl";
#################### Add Record ##################

if($_REQUEST['action']=="add_record" && $catName!="") {
	$numCnt=getCount($table_name," where locationName='$catName' and parentID='$parentID' and locationID!='$catID' and locationType='$catgType' ");
	$count_cat = mysqli_query($dbconn,"SELECT * FROM $table_name");
	if(mysqli_num_rows($count_cat) >= $res['location_count']){
		$_SESSION['session_message']='<div class="btn btn-warning">'.$record_type." count exceeds the allowed limit[$res[location_count]]</div>";
	}else{
		if($numCnt>0) {
		$_SESSION['session_message']='<div class="btn btn-warning">'.$record_type.' already exist</div>';
	}
	else{

		$maxRes=db_fetch_array(db_query("select max(locationOrder) from $table_name where parentID='$parentID'"));
		$catOrder=$maxRes[0]+1;
		$sql=db_query("insert into $table_name set parentID='$parentID', locationName='$catName', locationType='$catgType',locationOrder='$catOrder'");
		$_SESSION['session_message']='<div class="btn btn-success">'.$record_type.' has been added successfully</div>';
		header("Location: ".SITE_ADMIN_URL."/".$redirect_script_name."?start=".$start."&parentID=".$parentID."&locationID=".$catID."&pagesize=".$pagesize);
		exit;
	}
	}
	
}

################ Update Record ##################

if($_REQUEST['action']=="update_record" && $catName!="" ) {
	$numCnt=getCount($table_name," where locationName='$catName' and parentID='$parentID' and locationID!='$catID' and locationType='$catgType'");
	if($numCnt>0) {
		$_SESSION['session_message']='<div class="btn btn-success">'.$record_type.' already exist</div>';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&locationID=".$catID."&pagesize=".$pagesize);
		exit;
	}
			
	updateTable($table_name," locationName='$catName', locationType='$catgType' where locationID='$catID'");
    $_SESSION['session_message']='<div class="btn btn-warning">'.$record_type.' has been updated successfully</div>';
	header("Location: ".SITE_ADMIN_URL."/".$redirect_script_name."?start=".$start."&parentID=".$parentID."&locationID=".$catID."&pagesize=".$pagesize);
	exit;
}
########################################################
admin_header();
?>

<div align="left" class="myTitle"><?=$heading;?></div>
<hr size="2" width="100%" color="#92B2D6">
<?php
print_message();
$res=getResult($table_name," where locationID='$catID'");

	$catName=$res['locationName'];
	$catgType=$res['locationType'];

echo $catgType;

?>
<br />
<div class="text-right">
    <input type="button" class="button" value="Go Back To Manage <?=$record_type;?>" onclick="window.location.href='<?=SITE_ADMIN_URL?>/<?=$redirect_script_name;?>?parentID=<?=$parentID;?>&locationID=<?=$catID;?>&start=<?=$start;?>&pagesize=<?=$pagesize;?>';">
</div>
<br />
<form name="formn" method="post" action="" enctype="multipart/form-data" onsubmit="return validate(this);">
<?=$msg;?>
<div class="form-group">
    <label>Location Name <span>*</span></label>
    <input type="text" name="catName" value="<?=$catName?>" class="form-control">
</div>    

<div class="form-group">
  <?php
   if($res['locationID']=="") {
	   ?>
	   <input type="submit" name="add_record" class="btn btn-primary" value="Add <?=$record_type;?>"><input type="hidden" name="action" value="add_record">
	   <?php	   
   }
   else{
	   ?>
	   <input type="submit" name="update_record" class="btn btn-primary" value="Update <?=$record_type;?>"><input type="hidden" name="action" value="update_record">
	   <?php	   
   }
   ?>
   &nbsp;&nbsp;<input type="button" name="cancel" class="btn btn-warning" value="Cancel" onclick="javascript: window.location.href='<?=SITE_ADMIN_URL;?>/<?=$redirect_script_name;?>?parentID=<?=$parentID;?>&locationID=<?=$catID;?>&start=<?=$start;?>&pagesize=<?=$pagesize;?>'">   
</div> 

</form>

<?php
admin_footer();
exit;
?>