<?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'];

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

		if (array_key_exists('add_cat_name', $_POST)) {
			$cat_name = htmlspecialchars(strip_tags(trim($_POST['cat_name'])), ENT_QUOTES, 'UTF-8');
			$sql_cat = mysqli_query($conn, "SELECT category FROM dashboard_category WHERE category='$cat_name'");

			if(mysqli_num_rows($sql_cat) == 0) {
				$sql = "INSERT INTO dashboard_category SET category='$cat_name'";

				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_id', $_POST)) {
			$gid = htmlspecialchars(strip_tags(trim($_POST['gid'])), ENT_QUOTES, 'UTF-8');
			$sql_cat = mysqli_query($conn, "SELECT * FROM menu_list WHERE cat_id='$gid'");

			if(mysqli_num_rows($sql_cat) == 0) {
				$sql = "DELETE FROM dashboard_category 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 category, order_id FROM dashboard_category WHERE id='$gid'");

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

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

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

			$sql_lang = mysqli_query($conn, "SELECT category FROM dashboard_category WHERE category='$cat_name' AND id!='$gid'");
			if(mysqli_num_rows($sql_lang) == 0) {
				$sql_up = mysqli_query($conn, "UPDATE dashboard_category SET category='$cat_name', order_id='$oid' WHERE id='$gid'");

				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>";
	}
}
?>