<?php
	session_start();
	require('includes/config.php');
	ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
	$dir                = 'uploads/';
	$service            = $_REQUEST['service_name'];
	$description_title  = $_REQUEST['description_title'];
	$order              = $_REQUEST['sort_order'];	
	$cat_title_id       = !empty( $_REQUEST['cat_title_id'] ) ? $_REQUEST['cat_title_id'] : 0;
	$dtTm               = strtotime(date('d-m-Y'));
	$user               = $_SESSION['userid'];
	$img_path           = '';
	$message            = '';

	if( $cat_title_id == 0 ) {
		$return = 0;
		$chk_query = "select * from parameter_title where title ='".$description_title."' AND service_id = '".$service."' AND active = 1";		
		//echo $chk_query;
		$cexe = mysqli_query($con,$chk_query);
		$count = mysqli_num_rows($cexe);
		if( $count >= 1 ) {
			echo json_encode( ['code' => 405 , 'data' => 'Category title details already exists' ] );exit;
		}else{
			$query = "insert into parameter_title( title, sort_order, service_id, created_at, created_by ) values( '".$description_title."', '".$order."', '".$service."', ".$dtTm.", ".$user." )";	
			//echo $query;		
			$exe = mysqli_query($con,$query);		
			if($exe){
				//if (!$exe || mysqli_num_rows($exe) == 0){
				$return = 1;
				$message = 'Category description title added successfully';
			}else{
				$return = 0;
				$message = 'Error occured';
			}
		}
		
	}else{

		$query = "update parameter_title SET service_id = '".$service."', sort_order = '".$order."', title = '".$title."', updated_at = ".$dtTm.", updated_by = ".$user." where id = '".$cat_title_id."'";	
		//echo $query;exit;	
		$exe = mysqli_query( $con, $query );		
		if( $exe ){
			$return = 1;
			$message = $description_title.' updated successfully';
		}else{
			$return = 0;
			$message = $description_title.' update failed';
		}
	}

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