<?php
	include '../db/db.php';
	session_start();
	$log=htmlspecialchars(trim($_SESSION['user']), 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 details_of_users WHERE user='$log' and val='1'";
	  $resultu=mysqli_query($conn, $sqlu);
	  $countu=mysqli_num_rows($resultu);
	  
	if($countu==1){

	  $sqlc="SELECT * FROM details_of_users where user='$log'";
	  $resultc=mysqli_query($conn, $sqlc);
	  while ($rowc = mysqli_fetch_array($resultc)){
	  $name=$rowc['id'];
	}

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

	}}


	if (array_key_exists('sub_cat', $_POST)) {
		/*echo $_POST['uname'];*/
		$main_cat = htmlspecialchars(trim($_POST['main_cat']), ENT_QUOTES, 'UTF-8');
		$sub_cat = htmlspecialchars(trim($_POST['sub_cat']), ENT_QUOTES, 'UTF-8');
		$sub_descrip = $_POST['sub_descrip'];

		$sql = mysqli_query($conn, "INSERT INTO sub_cat VALUES (NULL, '$main_cat', '$sub_cat','$name' , '$sub_descrip')");	
		
		if ($sql) {
			$response['status'] = 'ok';
		}else{
			$response['status'] = 'error';
		}
		
		echo json_encode($response);
		//echo "In";
	}
	if (array_key_exists('del_subcat', $_POST)) {
		/*echo $_POST['uname'];*/
		$id = htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');

		$sql = mysqli_query($conn, "DELETE FROM sub_cat WHERE id=$id");	
		
		if ($sql) {
			$response['status'] = 'ok';
		}else{
			$response['status'] = 'error';
		}
		
		echo json_encode($response);
		//echo "In";
	}



	if (array_key_exists('update_subcat', $_POST)) {
		/*echo $_POST['uname'];*/
		$id = htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');
		$sub_cat = htmlspecialchars(trim($_POST['subcat_val']), ENT_QUOTES, 'UTF-8');
		$cat = htmlspecialchars(trim($_POST['cat_val']), ENT_QUOTES, 'UTF-8');
		$sub_descrip = $_POST['sub_descrip'];
		$sql = "UPDATE sub_cat SET cat_id='$cat' , sub_cat = '$sub_cat' , sub_descrip = '$sub_descrip' WHERE id= '$id'";

		$result = mysqli_query($conn, $sql);
		
		if ($result) {
			$response['status'] = 'ok';
		}else{
			$response['status'] = 'error';
		}
		
		echo json_encode($response);
		//echo "In";
	}


if (array_key_exists('load_subcat', $_POST)) {
   $sql = "SELECT cat,id FROM cat";
	//echo $sql;
    $result = mysqli_query($conn, $sql);
    echo'<option value="empty">Select Main Category...</option>';
    while ($row = mysqli_fetch_assoc($result)) {
        $data[] = $row;
    }

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

if (array_key_exists('load_subcat_up', $_POST)) {
   $sql = "SELECT * FROM sub_cat WHERE id = '".htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8')."'";
	//echo $sql;
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($result);

    echo json_encode($row);
}


?>