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

	}}

	$response = array();

	$valid_extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp');

  	if(isset($_FILES['itm_img']))
    {

      /*if (isset($_POST['active'])) {
        $active = 1;
      }else{
        $active = 0;
      }*/

      //echo $active;
      $cat = htmlspecialchars(trim($_POST['cat']), ENT_QUOTES, 'UTF-8');

      $path = '../upload/maincat/'; // upload directory
      $img = htmlspecialchars(trim($_FILES['itm_img']['name']), ENT_QUOTES, 'UTF-8');
      $tmp = htmlspecialchars(trim($_FILES['itm_img']['tmp_name']), ENT_QUOTES, 'UTF-8');

      if (isset($_POST['selected_cat'])) {
          $selected_cat = 1;
      }else{
        $selected_cat = 0;
      }

      if (isset($_POST['special_cat'])) {
          $special_cat = 1;
      }else{
        $special_cat = 0;
      }

      // get uploaded file's extension
      $ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));

      // can upload same image using rand function
      $final_image = rand(1000,1000000).strtolower($img);

      // check's valid format
      if(in_array($ext, $valid_extensions))
      {
        $path = $path.$final_image;
        //echo "img logo name".$final_image;
        if(move_uploaded_file($tmp,$path))
        {
          //echo "string";
          	$sql = "INSERT INTO cat VALUES (NULL, '$cat', '$selected_cat', '$special_cat' , '$name', '$final_image')";
          	$result = mysqli_query($conn, $sql);
          	if ($sql) {
				$response['status'] = 'ok';
			}else{
				$response['status'] = 'error';
			}
        }
      }
      else
      {
        	$sql = "INSERT INTO cat SET cat = '$cat' , selected = '$selected_cat', special = '$special_cat' , user = '$name'";
          	$result = mysqli_query($conn, $sql);
          	if ($sql) {
				$response['status'] = 'ok';
			}else{
				$response['status'] = 'error';
			}
      }

      echo json_encode($response);

    }


	if(isset($_FILES['up_itm_img']))
    {

		$cat = htmlspecialchars(trim($_POST['txt_up_cat']), ENT_QUOTES, 'UTF-8');
		$id_holder = htmlspecialchars(trim($_POST['id_holder']), ENT_QUOTES, 'UTF-8');
		$order_id = htmlspecialchars(trim($_POST['order_id']), ENT_QUOTES, 'UTF-8');

		if (isset($_POST['up_selected_cat'])) {
          $selected_cat = 1;
	    }else{
	        $selected_cat = 0;
	    }

	    if (isset($_POST['up_special_cat'])) {
		  	$special_cat = 1;
		}else{
			$special_cat = 0;
		}

      //echo $main_cat;

      $path = '../upload/maincat/'; // upload directory
      $img = htmlspecialchars(trim($_FILES['up_itm_img']['name']), ENT_QUOTES, 'UTF-8');
      $tmp = htmlspecialchars(trim($_FILES['up_itm_img']['tmp_name']), ENT_QUOTES, 'UTF-8');

      // get uploaded file's extension
      $ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));

      // can upload same image using rand function
      $final_image = rand(1000,1000000).strtolower($img);

      // check's valid format
      if(in_array($ext, $valid_extensions))
      {
        $path = $path.strtolower($final_image);
        //echo "img logo name".$final_image;
        if(move_uploaded_file($tmp,$path))
        {
          $sql = "UPDATE cat SET cat = '$cat' , selected = '$selected_cat', special = '$special_cat' , user = '$name', order_id = '$order_id' , img = '$final_image'  WHERE id= '$id_holder'";
          $result = mysqli_query($conn, $sql);
          if ($result) {
            echo "Logo Database inserted";
          }
        }
      }
      else
      {
        $sql2 = "UPDATE cat SET cat = '$cat' , selected = '$selected_cat', special = '$special_cat' , user = '$name', order_id = '$order_id' WHERE id= '$id_holder'";
          $result2 = mysqli_query($conn, $sql2);
          if ($result2) {
            echo "Data inserted without image";
          }
      }
    }



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

		$sql = mysqli_query($conn, "INSERT INTO cat VALUES (NULL, '$cat', '$sel' ,'$name')");

		if ($sql) {
			$response['status'] = 'ok';
		}else{
			$response['status'] = 'error';
		}

		echo json_encode($response);
		//echo "In";
	}

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

		$sql = mysqli_query($conn, "SELECT * FROM sub_cat WHERE cat_id=$id");
		$row = mysqli_num_rows($sql);

		if ($row != 0) {
			$response['status'] = 'unable';
		}else{
			$sql2 = mysqli_query($conn, "DELETE FROM cat WHERE id=$id");

			if ($sql2) {
				$response['status'] = 'ok';
			}else{
				$response['status'] = 'error';
			}
		}
		echo json_encode($response);
		//echo "In";
	}

	if (array_key_exists('update_cat', $_POST)) {
		/*echo $_POST['uname'];*/
		$id = htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');
		$val = htmlspecialchars(trim($_POST['val']), ENT_QUOTES, 'UTF-8');
		$sel = htmlspecialchars(trim($_POST['sel']), ENT_QUOTES, 'UTF-8');
		$sql = "UPDATE cat SET cat='$val' , selected='$sel' 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_cat', $_POST)) {
		/*echo $_POST['uname'];*/
		$id = htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');

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

		echo json_encode($row);
		//echo "In";
	}
?>