<?php
	session_start();
	require('includes/config.php');
	
	$dir               = 'uploads/';
	$name              = $_REQUEST['category_name'];
	$sec_category_name = $_REQUEST['sec_category_name'];	
	$secondary_cat_id  = !empty( $_REQUEST['secondary_cat_id'] ) ? $_REQUEST['secondary_cat_id'] : 0;
	$dtTm = strtotime(date('d-m-Y'));
	$user = $_SESSION['userid'];
	$img_path = '';
	$message  = '';

	if( $primary_cat_id == 0 ) {
		$return = 0;
		$chk_query = "select sub_category_name from child_categories where sub_category_name ='".$sub_category_name."' AND parent_id = ". $name." AND active = 1";		
		$cexe = mysqli_query($con,$chk_query);
		$count = mysqli_num_rows( $cexe );

		if( $count >= 1 ) {
			echo json_encode( ['code' => 401 , 'data' => 'Primary Category details already exists' ] );exit;
		}else{

			/* Moving user file to folder */
			$tmp_name           = $_FILES["cat_img"]["tmp_name"];
			$img_name           = $user.'-'.$_FILES["cat_img"]["name"];
			$cat_img_path       = $dir . $img_name;
			$img_size           = $_FILES["cat_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        = $cat_img_path;												
				}else{
					$return = 0;
					$message = 'Error occured. Image upload faild';
				}
			}else{
				$return = 0;
				$message = 'Error occured. Image size is zero';
			}

			$query = "insert into child_categories( sub_category_name, parent_id, child_category_img_path, created_at, created_by ) values( '".$sec_category_name."', '".$name."', '".$img_path."', ".$dtTm.", ".$user." )";		
			$exe = mysqli_query( $con, $query );		
			if( $exe ){
				$return = 1;
				$message = 'Secondary Category added successfully';
			}else{
				$return = 0;
				$message = 'Error occured';
			}
		}
		
	}else{

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

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

		$query = "update product_category SET parent_id = '".$name."', sub_category_name = '".$sec_category_name."', child_category_img_path = '".$new_img_path."', updated_at = ".$dtTm.", updated_by = ".$user." where secondary_cat_id = '".$primary_cat_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' => 'secondary-category' ] );exit;
	}else{
		echo json_encode( ['code' => 405 , 'data' => 'Error Occured' ] );exit;	
	}
?>