<?php
include '../db/db.php';
session_start();
$log = htmlspecialchars(trim($_SESSION['user']), ENT_QUOTES, 'UTF-8');
$menu = htmlspecialchars(trim($_SESSION['menu']), ENT_QUOTES, 'UTF-8');
$emp_id = htmlspecialchars(trim($_SESSION['emp_id']), ENT_QUOTES, 'UTF-8');

if (empty($log)) {
	$redirectUrl = "index.php";
	echo "<script type=\"text/javascript\">";
	echo "window.location.href = '$redirectUrl'";
	echo "</script>";

} else {

	$sqlu = "SELECT * FROM user_priv_q WHERE emp_id='$emp_id' AND user='$log' AND menu_id='$menu'";
	$resultu = mysqli_query($conn, $sqlu);
	$countu = mysqli_num_rows($resultu);

	if($countu == 1){
		$rowc = mysqli_fetch_assoc($resultu);
		$user_id = $rowc['user_id'];

		/************************************ Load Main Cat **************************************** */
		if (array_key_exists('load_cat', $_POST)) {
			$sql = "SELECT id, category FROM dashboard_category ORDER BY category ASC";

			$result = mysqli_query($conn, $sql);
			echo'<option value=""></option>';
			while ($row = mysqli_fetch_assoc($result)) {
				$data[] = $row;
			}

			if (!empty($data)) {
				foreach ($data AS $data) {
					echo'<option value="' . $data['id'] . '">' . $data['category'] . '</option>';
				}
			} else {
				echo'<option value="">No Data Found</option>';
			}
			echo json_encode($data);
		}

		/****************************** Insert *************************************/

		if (array_key_exists('insert_data', $_POST)) {
			$cat_id = htmlspecialchars(strip_tags(trim($_POST['cat_id'])), ENT_QUOTES, 'UTF-8');
			$name = htmlspecialchars(strip_tags(trim($_POST['name'])), ENT_QUOTES, 'UTF-8');
			$page_url = htmlspecialchars(strip_tags(trim($_POST['page_url'])), ENT_QUOTES, 'UTF-8');

			$sql_eng = mysqli_query($conn, "SELECT name FROM menu_list WHERE name='$name' AND cat_id='$cat_id'");

			if(mysqli_num_rows($sql_eng) == 0) {
				$sql = "INSERT INTO menu_list SET name='$name', cat_id='$cat_id', url='$page_url'";

				if(mysqli_query($conn, $sql)) {
					echo json_encode(array("msgType" => 1, "msg" => "Successfully Added"));

				} else {
					echo json_encode(array("msgType" => 2, "msg" => "Sorry ! Could not be Save Data"));
				}

			} else {
				echo json_encode(array("msgType" => 3, "msg" => "Data Already Exist"));
			}
		}

		/****************************** Remove *************************************/

		if (array_key_exists('remove_data', $_POST)) {
			$gid = htmlspecialchars(strip_tags(trim($_POST['gid'])), ENT_QUOTES, 'UTF-8');
			$sql_cat = mysqli_query($conn, "SELECT * FROM user_privileges WHERE menu_id='$gid'");

			if(mysqli_num_rows($sql_cat) == 0) {
				$sql = "DELETE FROM menu_list WHERE id='$gid'";

				if(!empty($gid)) {
					if(mysqli_query($conn, $sql)) {
						echo json_encode(array("msgType" => 1, "msg" => "Successfully Removed"));

					} else {
						echo json_encode(array("msgType" => 2, "msg" => "Sorry ! not deleted"));
					}

				} else {
					echo json_encode(array("msgType" => 2, "msg" => "Sorry ! empty id"));
				}

			} else {
				echo json_encode(array("msgType" => 3, "msg" => "Data Exist"));
			}
		}

		/****************************** Load Form Data *************************************/

		if (array_key_exists('get_dataset', $_POST)) {
			$gid = htmlspecialchars(strip_tags(trim($_POST['gid'])), ENT_QUOTES, 'UTF-8');

			$data = array();
			$sql = mysqli_query($conn, "SELECT cat_id, name, url, order_id FROM menu_list WHERE id='$gid'");

			while ($row = mysqli_fetch_assoc($sql)) {
				$data[] = $row;
			}
			echo json_encode($data);
		}

		if (array_key_exists('get_cat', $_POST)) {
			$cat_id = htmlspecialchars(strip_tags(trim($_POST['cat_id'])), ENT_QUOTES, 'UTF-8');
			$sql = "SELECT id, category FROM dashboard_category ORDER BY category ASC";

			$result = mysqli_query($conn, $sql);
			while ($row = mysqli_fetch_assoc($result)) {
				$data[] = $row;
			}

			if (!empty($data)) {
				foreach ($data AS $data) { ?><option value="<?php echo $data['id']; ?>" <?php if($data['id'] == $cat_id){ echo "selected"; } ?>><?php echo $data['category']; ?></option>
				<?php }
			}
			echo json_encode($data);
		}

		/****************************** Update *************************************/

		if (array_key_exists('update_data', $_POST)) {
			$id_up = htmlspecialchars(strip_tags(trim($_POST['id_up'])), ENT_QUOTES, 'UTF-8');

			$name = htmlspecialchars(strip_tags(trim($_POST['name'])), ENT_QUOTES, 'UTF-8');
			$cat_id = htmlspecialchars(strip_tags(trim($_POST['cat_id'])), ENT_QUOTES, 'UTF-8');
			$oid = htmlspecialchars(strip_tags(trim($_POST['oid'])), ENT_QUOTES, 'UTF-8');
			$page_url = htmlspecialchars(strip_tags(trim($_POST['page_url'])), ENT_QUOTES, 'UTF-8');

			$sql_lang = mysqli_query($conn, "SELECT name FROM menu_list WHERE name='$name' AND cat_id='$cat_id' and id!='$id_up'");

			if(mysqli_num_rows($sql_lang) == 0) {
				$sql_up = mysqli_query($conn, "UPDATE menu_list SET name='$name', cat_id='$cat_id', url='$page_url', order_id = '$oid' WHERE id='$id_up'");

				if($sql_up) {
					echo json_encode(array("msgType" => 1, "msg" => "Successfully Added"));

				} else {
					echo json_encode(array("msgType" => 2, "msg" => "Sorry ! Could not be Save Data"));
				}

			} else {
				echo json_encode(array("msgType" => 3, "msg" => "Data Already Exist"));
			}
		}

		/******************************************************************/

	} else{
		$redirectUrl = "index.php";
		echo "<script type=\"text/javascript\">";
		echo "window.location.href = '$redirectUrl'";
		echo "</script>";
	}
}
?>