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

$isEdit = FALSE;

$parentRs = mysqli_query($con, "SELECT * FROM tbl_service_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_service_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($_FILES['photo']) && $_FILES['photo']['size'] > 0) {
		if($_FILES['photo']['type'] == 'image/gif' || $_FILES['photo']['type'] == 'image/jpeg' || 
		   $_FILES['photo']['type'] == 'image/png' || $_FILES['photo']['type'] == 'image/pjpeg') {
			$photo_name = $_FILES['photo']['name'];			
			$photo_temp_name = $_FILES['photo']['tmp_name'];
			$photo_extension = pathinfo($photo_name, PATHINFO_EXTENSION);
		} else {
			$photo_name = FALSE;
			$photo_temp_name = FALSE;
			$error = "Invalid Image type. Only gif, jpg and png allowed";
		}
	} else {
		$error = 'Select Image';
	}
		
	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_service_categories(parent_id, name, commission, status) VALUES (
		'".mysqli_real_escape_string($con, $parent)."',
		 '".mysqli_real_escape_string($con, $name)."',
		 '".mysqli_real_escape_string($con, $commission)."', 'Active')";
		
		$execInsert = mysqli_query($con,$insertQry);
		$insert_id = mysqli_insert_id($con);		
		
		if($execInsert) {
			
			if(isset($_FILES['photo']) && $_FILES['photo']['size'] > 0) {
				$photo_dir = SERVICE_IMG_FOLDER;
				$photo_name = $insert_id.'_'.time().'.'.$photo_extension; 
				if(move_uploaded_file($photo_temp_name, $photo_dir.$photo_name)){
					resize2(SERVICE_IMG_FOLDER.$photo_name, 200, 150, SERVICE_IMG_FOLDER.'thumb/'.$photo_name);
					mysqli_query($con, "UPDATE tbl_service_categories SET image = '".$photo_name."' WHERE id = ".$insert_id);
				}			
			}	
						
			$success = 'Category Added Successfully';
			header('Location:index.php?page=manage_category&success='.$success);
			exit();			
		}				
	} 
}

if(isset($_POST['update'])) {
	
	if(isset($_FILES['photo']) && $_FILES['photo']['size'] > 0) {
		if($_FILES['photo']['type'] == 'image/gif' || $_FILES['photo']['type'] == 'image/jpeg' || 
		   $_FILES['photo']['type'] == 'image/png' || $_FILES['photo']['type'] == 'image/pjpeg') {
			$photo_name = $_FILES['photo']['name'];			
			$photo_temp_name = $_FILES['photo']['tmp_name'];
			$photo_extension = pathinfo($photo_name, PATHINFO_EXTENSION);
		} else {
			$photo_name = FALSE;
			$photo_temp_name = FALSE;
			$error = "Invalid Image type. Only gif, jpg and png allowed";
		}
	}
	
	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_service_categories SET name = '".$name."', parent_id = '".$parent."' WHERE id = '".$editId."'";
		
		$execUpdate = mysqli_query($con,$updateQry);			
		
		if($execUpdate) {
			
			if(isset($_FILES['photo']) && $_FILES['photo']['size'] > 0) {
				$photo_dir = SERVICE_IMG_FOLDER;
				$photo_name = $editId.'_'.time().'.'.$photo_extension; 
				if(move_uploaded_file($photo_temp_name, $photo_dir.$photo_name)){
					resize2(SERVICE_IMG_FOLDER.$photo_name, 200, 150, SERVICE_IMG_FOLDER.'thumb/'.$photo_name);
					mysqli_query($con, "UPDATE tbl_service_categories SET image = '".$photo_name."' WHERE id = ".$editId);
				}			
			}	
			
			$success = 'Category Updated Successfully';
			header('Location:index.php?page=manage_category&success='.$success);
			exit();			
		}				
	} 
}

?>