<?php
/*
* date last modified 28-11-2017
* Edited by PI-Digi Logical Solutions
* HTML Files - add_user.html.php
* Mysql Tables - tbl_designation, tbl_product_categories
*/
include('includes/imgFunctions.php');
include('includes/functions.php'); # general functions

$isEdit = FALSE;

$parentRs = mysqli_query($con, "SELECT * FROM tbl_product_categories WHERE parent_id = '0'");

$commission = 0;

if(isset($_REQUEST['id']) && $_REQUEST['id']!=''){
	
	$isEdit = TRUE;
	$editId = mysqli_real_escape_string($con, trim($_REQUEST['id']));		
	$editRs = mysqli_query($con, "SELECT * FROM tbl_product_categories WHERE id = '".$editId."'");
	if(mysqli_num_rows($editRs)>0){
		$resEdit = mysqli_fetch_array($editRs);
	} else {
		$error = 'Category does not exist!';
	}

} 

if(isset($_POST['add'])) {
		
	if(isset($_POST['parent']) && strlen($_POST['parent'])>0) {
		$parent = trim($_POST['parent']);
	} else {
		$parent = '0';
	}	
	
	if(isset($_POST['name']) && strlen($_POST['name'])>0) {
		$name = trim($_POST['name']);
	} else {
		$error = 'Please enter Category Name';
	}	
	
	
	
	if(empty($error)) {
						
		/* Adding New staff Details */		
		$insertQry = "INSERT INTO tbl_product_categories(parent_id, name) VALUES (
		'".mysqli_real_escape_string($con, $parent)."',
		 '".mysqli_real_escape_string($con, $name)."')";
		
		$execInsert = mysqli_query($con,$insertQry);
		$insert_id = mysqli_insert_id($con);		
		
		if($execInsert) {
						
			$success = 'Category Added Successfully';
			header('Location:index.php?page=manage_product_category&success='.$success);
			exit();			
		}				
	} 
}

if(isset($_POST['update'])) {
	
	if(isset($_POST['parent']) && strlen($_POST['parent'])>0) {
		$parent = trim($_POST['parent']);
	} else {
		$parent = '0';
	}	
	
	if(isset($_POST['name']) && strlen($_POST['name'])>0) {
		$name = trim($_POST['name']);
	} else {
		$error = 'Please enter Category Name';
	}		
		
	if(empty($error)) {					
			
		$updateQry = "UPDATE tbl_product_categories SET name = '".$name."', parent_id = '".$parent."' WHERE id = '".$editId."'";
		
		$execUpdate = mysqli_query($con,$updateQry);			
		
		if($execUpdate) {
			$success = 'Category Updated Successfully';
			header('Location:index.php?page=manage_product_category&success='.$success);
			exit();			
		}				
	} 
}

?>