<?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['name'])) {
			$name = htmlspecialchars(strip_tags(trim($_POST['name'])), ENT_QUOTES, 'UTF-8');

			//$string = preg_replace('/[^\p{L}\p{N}\s]/u', '', $_POST['name']);
			$string = str_replace(["-", "–"], '' ,$name);
			$string = preg_replace('/\s+/', ' ', $string);
			$search_id = strtolower(str_replace(" ","-",$string));
		
			$stmt = mysqli_query($conn, "SELECT search_id FROM author WHERE search_id='$search_id'");
			$row_count = mysqli_num_rows($stmt);
			
			if($row_count == 0) {
				$sql = "INSERT INTO author (name, search_id) values ('$name', '$search_id')";
				$stmt_log = mysqli_query($conn, $sql);
				
				if($stmt_log === false) {
					echo 'error';
				
				} else {
					echo 'success';
				}
			
			} 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 author 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');
			$name = htmlspecialchars(strip_tags(trim($_POST['name_up'])), ENT_QUOTES, 'UTF-8');
			
			//$string = preg_replace('/[^\p{L}\p{N}\s]/u', '', $_POST['name_up']);
			$string = str_replace(["-", "–"], '' ,$name);
			$string = preg_replace('/\s+/', ' ', $string);
			$search_id = strtolower(str_replace(" ","-",$string));
		
			$stmt = mysqli_query($conn, "SELECT search_id FROM author WHERE search_id='$search_id' AND id!=$id");
			$row_count = mysqli_num_rows($stmt);
		
			if($row_count == 0) {
				$sql = "UPDATE author SET name='$name', search_id='$search_id' WHERE id='$id'";
				$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');
			
			$stmt = mysqli_query( $conn, "SELECT Auth_ID FROM author_map WHERE Auth_ID=$id");
			$row_count = mysqli_num_rows( $stmt );
		  
			if($row_count == 0) {
				$sql = "DELETE FROM author 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>";
	}
}
?>