<?php
session_start();
require_once('includes/config.php');

$user = $_SESSION['userid'];

/*descriptionEdit*/
if( $_REQUEST['type'] == "descriptionEdit" ){
	$id          = $_REQUEST['id'];
	$sort_order  = $_REQUEST['sort_order'];
	$description = $_REQUEST['description'];
	$status      = $_REQUEST['status'];
	$desc_edit = mysqli_query( $con, "UPDATE ".$table." SET description = '".$description."', active = '".$status."' WHERE ".$key." = '".$id."'" );
	if( $desc_edit ) {
		$message = "A record with id '".$id."' has been updated";
		echo json_encode( ['code' => 200,'data' => $message, 'url' => $call_back ] );exit;
	}else{
		$message = "A record with id '".$id."' update failed";
		echo json_encode( ['code' => 405 , 'data' => $message, 'url' => $call_back ] );exit;	
	}
}
 /*PrimaryCategory*/ 
if($_REQUEST['type']=="PrimaryCategory"){
	$call_back = 'primary-category';
	$table     = 'parent_categories';
	$key       = 'category_id';

	$dir   		= 'uploads/';
	$id 		= $_REQUEST['id'];
	$catName 	= $_REQUEST['cat_name'];
	$sOrder 	= $_REQUEST['order'];
	$status 	= $_REQUEST['status'];			
	$img_path 	= '';
	
	$tmp_name           = $_FILES["img_cat"]["tmp_name"];
	$new_img_name       = $user.'-'.$_FILES["img_cat"]["name"];
	$cat_img_path       = $dir . $new_img_name;
	$new_img_size       = $_FILES["img_cat"]["size"];
	$img_file_moved     = 1;

	if ( $new_img_size > 0 ) {
		if ( move_uploaded_file( $tmp_name, $cat_img_path ) ) {
			$img_file_moved  = 1;	
			$img_path        = $cat_img_path;												
		}else{
			$img_path = $_REQUEST['old_img'];
		}
	}else{
		$img_path = $_REQUEST['old_img'];
	}
	$pc_edit = mysqli_query( $con, "UPDATE parent_categories SET category_name='".$catName."',category_img_path='".$img_path."', active='".$status."' WHERE category_id = '".$id."'" );
	if( $pc_edit ) {
		$message = "A record with id ". $key ." has been updated";
		echo json_encode( ['code' => 200,'data' => $message, 'url' => $call_back ] );exit;
	}else{
		$message = "A record with id ". $key ." update failed";
		echo json_encode( ['code' => 405 , 'data' => $message, 'url' => $call_back ] );exit;	
	}
}
 /*descriptionTitle*/ 
if($_REQUEST['type']=="descriptionTitle"){
	$id 		= $_REQUEST['id'];
	$desc_name	= $_REQUEST['desc_name'];
	$serv_name	= $_REQUEST['serv_name'];
	$sOrder 	= $_REQUEST['sOrder'];
	$status 	= $_REQUEST['status'];
	$descriptionTitleEdit = mysqli_query($con, "UPDATE parameter_title SET title ='".$desc_name."', service_id='".$serv_name."', sort_order='".$sOrder."', active='".$status."' WHERE id =  '".$id."'");
    if( $descriptionTitleEdit ) {
		$message = "A record with id ". $key ." has been updated";
		echo json_encode( ['code' => 200,'data' => $message, 'url' => $call_back ] );exit;
	}else{
		$message = "A record with id ". $key ." update failed";
		echo json_encode( ['code' => 405 , 'data' => $message, 'url' => $call_back ] );exit;	
	}
}   
if($_REQUEST['type']=="secondaryCategory"){
			$call_back = 'secondary-category';
			$table     = 'child_categories';
			$key       = 'child_id ';

			$dir   = 'uploads/';
			$user = $_SESSION['userid'];
			$img_path = '';

			$id 			= $_REQUEST['id'];
			$primary_name	= $_REQUEST['primary_name'];
			$subCat_name	= $_REQUEST['subCat_name'];
			$status 		= $_REQUEST['status'];
		
			$tmp_name       = $_FILES["img_cat"]["tmp_name"];
			$new_img_name   = $user.'-'.$_FILES["img_cat"]["name"];
			$cat_img_path   = $dir . $new_img_name;
			$new_img_size   = $_FILES["img_cat"]["size"];
			$img_file_moved = 1;

			if ( $new_img_size > 0 ) {
				if ( move_uploaded_file( $tmp_name, $cat_img_path ) ) {
					$img_file_moved  = 1;	
					$img_path        = $cat_img_path;												
				}else{
					$img_path = $_REQUEST['old_img'];
				}
			}else{
				$img_path = $_REQUEST['old_img'];
			}

	$sub_categoryEdit = mysqli_query($con, "UPDATE  child_categories  SET  sub_category_name ='".$subCat_name."', parent_id ='".$primary_name."', child_category_img_path ='".$img_path."', active ='".$status."' WHERE child_id='".$id."'");

	//echo "UPDATE  child_categories  SET  sub_category_name ='".$subCat_name."', parent_id ='".$primary_name."', child_category_img_path ='".$img_path."', active ='".$status."' WHERE child_id='".$id."'"		

    if( $sub_categoryEdit ) {
		$message = "A record with id ". $key ." has been updated";
		echo json_encode( ['code' => 200,'data' => $message, 'url' => $call_back ] );exit;
	}else{
		$message = "A record with id ". $key ." update failed";
		echo json_encode( ['code' => 405 , 'data' => $message, 'url' => $call_back ] );exit;	
	}

}
       
?>