<?php
	session_start();
	require('includes/config.php');
	
	$dir               = 'uploads/services/';
	$service_name      = $_REQUEST['service_name'];
	$child_id          = $_REQUEST['child_id'];
	$min_visit_price   = $_REQUEST['min_visit_price'];	  		
	$default_price     = !empty( $_REQUEST['default_price'] ) ? $_REQUEST['default_price'] : 0;
	$offer_price       = !empty( $_REQUEST['offer_price'] ) ? $_REQUEST['offer_price'] : 0;
	$discount          = !empty( $_REQUEST['discount'] ) ? $_REQUEST['discount'] : 0;
	$is_best_seller    = !empty( $_REQUEST['is_best_seller'] ) ? $_REQUEST['is_best_seller'] : 0;
	$dtTm              = strtotime(date('d-m-Y h:i:s'));
	$user              = $_SESSION['userid'];
	$service_id        = !empty( $_REQUEST['service_id'] ) ? $_REQUEST['service_id'] : 0;
	$img_path          = '';
	$message           = '';
	$call_back         = !empty( $_REQUEST['call_back'] ) ? $_REQUEST['call_back'] : 'services';
	$table             = !empty( $_REQUEST['table'] ) ? $_REQUEST['table'] : 'services';
	$key               = !empty( $_REQUEST['primary_key'] ) ? $_REQUEST['primary_key'] : 'id';
	

	if( $service_id == 0 ) {
		$return = 0;
		$chk_query = "SELECT service_name FROM ".$table." WHERE service_name = '".$service_name."' AND active = 1";		
		$cexe  	   = mysqli_query( $con, $chk_query );
		$count     = mysqli_num_rows( $cexe );

		if( $count >= 1 ) {
			echo json_encode( ['code' => 401 , 'data' => 'Service details already exists' ] );exit;
		}else{
			/* Moving user file to folder */
			$tmp_name           = $_FILES["service_img"]["tmp_name"];
			$img_name           = $user.'-'.$_FILES["service_img"]["name"];
			$sr_img_path        = $dir . $img_name;
			$img_size           = $_FILES["service_img"]["size"];
			$img_file_moved     = 1;

			if ( $img_size > 0 ) {
				if ( move_uploaded_file( $tmp_name, $dir.$img_name ) ) {
					$img_file_moved  = 1;	
					$img_path        = $sr_img_path;												
				}else{
					$return = 0;
					$message = 'Error occured. Image upload faild';
				}
			}else{
				$return = 0;
				$message = 'Error occured. Image size is zero';
			}

			if( $discount != 0 ){
				$discounted_price = $default_price*($discount/100);
			}else{
				$discounted_price = 0;
			}
			
			$query = "insert into ".$table." ( service_name, child_id, offer_price, default_price,min_visit_price, discount, discounted_price, is_best_seller, service_image, created_by, created_at ) values( '".$service_name."', '".$child_id."', '".$offer_price."', '".$default_price."', '".$min_visit_price."', '".$discount."', '".$discounted_price."', '".$is_best_seller."', '".$img_path."', ".$user.", ".$dtTm." )";
			//echo $query;exit;
			$exe = mysqli_query( $con, $query );		
			if( $exe ) {
				$return = 1;
				$message = 'Service added successfully';
			}else{
				$return = 0;
				$message = 'Error occured';
			}
		}
		
	}else{			

		/* Moving user file to folder */
			$tmp_name           = $_FILES["service_img"]["tmp_name"];
			$new_img_name       = $user.'-'.$_FILES["service_img"]["name"];
			$cat_img_path       = $dir . $img_name;
			$new_img_size       = $_FILES["service_img"]["size"];
			$img_file_moved     = 1;

			if ( $new_img_size > 0 ) {
				if ( move_uploaded_file( $tmp_name, $dir.$new_img_name ) ) {
					$img_file_moved  = 1;	
					$img_path        = $cat_img_path;												
				}else{
					$img_path = $_REQUEST['old_image'];
				}
			}else{
				$img_path = $_REQUEST['old_image'];
			}
				
		$query = "update ".$table." SET service_name='".$_POST['service_name']."', child_id = '".$_POST['child_id']."', min_visit_price = '".$_POST['min_visit_price']."', offer_price = '".$_POST['offer_price']."', default_price = '".$_POST['default_price']."', discount = '".$_POST['discount']."', is_best_seller = '".$_POST['is_best_seller']."', service_image = '".$img_path."', updated_at = ".$dtTm.", updated_by = ".$user." where ". $key." = '".$service_id."'";	
		//echo $query;exit;
		$exe = mysqli_query( $con, $query );		
		if( $exe ){
			$return = 1;
			$message = $name.' updated successfully';
		}else{
			$return = 0;
			$message = $name.' update failed';
		}
	}

	if( $return ) {
		echo json_encode( ['code' => 200,'data' => $message, 'url' => 'services' ] );exit;
	}else{
		echo json_encode( ['code' => 405 , 'data' => 'Error Occured' ] );exit;	
	}
?>