<?php
include '../db/db.php';
session_start();

if(isset($_SESSION['userID'])) {
	$log = htmlspecialchars(strip_tags(trim($_SESSION['userID'])), ENT_QUOTES, 'UTF-8');
} else {
	$log = '';
}

if (empty($log)) {     
	$redirectUrl = "../";
	echo "<script type=\"text/javascript\">";
	echo "window.location.href = '$redirectUrl'";
	echo "</script>";
	 
} else {
	$sqlu = "SELECT id FROM user_acc_tbl WHERE userID='$log' and access=1 and val=1";
	$stmtu = mysqli_query($conn, $sqlu);
	$countu = mysqli_num_rows($stmtu);
	
	if($countu == 1) {
		
		/*************************** Insert ***************************/
		
		if(isset($_POST['main_cat'])) {
			$main_cat = htmlspecialchars(strip_tags(trim($_POST['main_cat'])), ENT_QUOTES, 'UTF-8');
			$cat_code = htmlspecialchars(strip_tags(trim($_POST['cat_code'])), ENT_QUOTES, 'UTF-8');
			
			$string = str_replace(["-", "–"], '' ,$main_cat);
			$string = preg_replace('/\s+/', ' ', $string);
			$search_id = strtolower(str_replace(" ","-",$string));
		
			$stmt1 = mysqli_query($conn, "SELECT search_id FROM category WHERE search_id='$search_id'");
			$row_count1 = mysqli_num_rows($stmt1);
			
			$stmt2 = mysqli_query($conn, "SELECT item_group FROM category WHERE item_group='$cat_code'");
			$row_count2 = mysqli_num_rows($stmt2);
		
			if($row_count1 == 0) {
				if($row_count2 == 0) {
					$sql = "INSERT INTO category (category, item_group, search_id) values ('$main_cat', '$cat_code', '$search_id')";
					$stmt_log = mysqli_query($conn, $sql);
					
					if($stmt_log === false) {
						echo 'error';
					
					} else {
						echo 'success';
					}
				
				} else {
					echo 'data exist1'; // code exist
				}
			
			} else {
				echo 'data exist'; // name exist
			}
		}
		
		/************************ Load Form Data ************************/
	    
		if (array_key_exists('get_dataset', $_POST)) {
			$id = htmlspecialchars(strip_tags(trim($_POST['id'])), ENT_QUOTES, 'UTF-8');

			$sql = mysqli_query($conn, "SELECT * FROM category WHERE id=$id"); 
			$row = mysqli_fetch_array($sql);

			echo json_encode($row);
	    }
		
		/*************************** Update ***************************/
		
		if(isset($_POST['id_up'])) {
			$id = htmlspecialchars(strip_tags(trim($_POST['id_up'])), ENT_QUOTES, 'UTF-8');
			$main_cat = htmlspecialchars(strip_tags(trim($_POST['main_cat_up'])), ENT_QUOTES, 'UTF-8');

			$string = str_replace(["-", "–"], '' , $main_cat);
			$string = preg_replace('/\s+/', ' ', $string);
			$search_id = strtolower(str_replace(" ","-",$string));
		
			$stmt1 = mysqli_query($conn, "SELECT category FROM category WHERE search_id='$search_id' AND id!=$id");
			$row_count1 = mysqli_num_rows($stmt1);
		
			if($row_count1 == 0) {
				$sql = "UPDATE category SET category='$main_cat', search_id='$search_id' WHERE id='$id'"; // , item_group='$cat_code'
				$stmt_log = mysqli_query($conn, $sql);
				
				if($stmt_log === false) {
					echo 'error';
				
				} else {
					echo 'success';
				}
					
			} else {
				echo 'data exist'; // name exist
			}
		}
		
		/************************ Remove *************************/
		
	    if (array_key_exists('remove_data', $_POST)) {
			$id = htmlspecialchars(strip_tags(trim($_POST['id'])), ENT_QUOTES, 'UTF-8');
			$cat_code = htmlspecialchars(strip_tags(trim($_POST['cat_code'])), ENT_QUOTES, 'UTF-8');
			
			$stmt = mysqli_query( $conn, "SELECT Cat_ID FROM category_map WHERE Cat_ID=$id");
			$row_count = mysqli_num_rows( $stmt );
		  
			if($row_count == 0) {
				$sql = "DELETE FROM category WHERE id=$id";
				$stmt_log = mysqli_query($conn, $sql);
				
				if( $stmt_log === false ) {
					echo json_encode(array("msgType" => 2, "msg" => "Sorry ! not deleted"));
				
				} else {
					echo json_encode(array("msgType" => 1, "msg" => "Successfully Removed"));
				}
			
			} else {
				echo json_encode(array("msgType" => 3, "msg" => "data exist"));
			}
	    }
		
	} else {
		$redirectUrl = "../";
		echo "<script type=\"text/javascript\">";
		echo "window.location.href = '$redirectUrl'";
		echo "</script>";
	}
}
?>