<?php

if(isset($_REQUEST['id']) && $_REQUEST['id']!=''){
	$catId = trim($_REQUEST['id']);
	$catRec = mysqli_fetch_array(mysqli_query($con, "SELECT visit_charge, min_charge, max_charge, hourly_rate FROM tbl_service_categories WHERE id = '".$catId."'"));
} else { ?>
			<script language="javascript">
			alert("Something went wrong");
			window.location="index.php?page=manage_category";			
			</script>
	<?php 
}


if(isset($_POST['update'])) {
	
	if(isset($_POST['hourly_rate']) && trim($_POST['hourly_rate']) !=''){
		$hourly_rate = mysqli_real_escape_string($con, $_POST['hourly_rate']); 
		if(is_numeric($hourly_rate) && $hourly_rate>0){
			//ok
		} else {
			$error = 'Please enter a numeric value for hourly rate';
		}
	} else {
	$error = 'Enter Hourly Rate'; }
	
	if(isset($_POST['max_charge']) && trim($_POST['max_charge']) !=''){
		$max_charge = mysqli_real_escape_string($con, $_POST['max_charge']); 
		if(is_numeric($max_charge) && $max_charge>0){
			//ok
		} else {
			$error = 'Please enter a numeric value for maximum charge';
		}
	} else {
	$error = 'Enter Maximum Charge'; }
	
	if(isset($_POST['min_charge']) && trim($_POST['min_charge']) !=''){
		$min_charge = mysqli_real_escape_string($con, $_POST['min_charge']); 
		if(is_numeric($min_charge) && $min_charge>0){
			//ok
		} else {
			$error = 'Please enter a numeric value for minimum charge';
		}
	} else {
	$error = 'Enter Minimum Charge'; }
	
	if(isset($_POST['visit_charge']) && trim($_POST['visit_charge']) !=''){
		$visit_charge = mysqli_real_escape_string($con, $_POST['visit_charge']); 
		if(is_numeric($visit_charge)){
			//ok
		} else {
			$error = 'Please enter a numeric value for visit charge';
		}
	} else {
	$error = 'Enter Visit Charge'; }

	if(empty($error)) {
		
		$update = mysqli_query($con, "UPDATE tbl_service_categories SET `visit_charge` = '".$visit_charge."', `min_charge` = '".$min_charge."', `max_charge` = '".$max_charge."', `hourly_rate` = '".$hourly_rate."' WHERE id = '".$catId."'");
		
		if($update) {	?>
			<script language="javascript">
			alert("Charges updated successfully");
			window.location="index.php?page=manage_category";			
			</script>
		<?php //exit;		
		}
	}
}


?>