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

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

if(isset($_REQUEST['tech_id']) && $_REQUEST['tech_id']!=''){
	$technicianId = trim($_REQUEST['tech_id']);
} else {
	$error = 'Something went wrong!';
}

if(isset($_REQUEST['id']) && $_REQUEST['id']!=''){	
	$isEdit = TRUE;
	$editId = mysqli_real_escape_string($con, trim($_REQUEST['id']));	
	$editRs = mysqli_query($con, "SELECT * FROM tbl_technician_category 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['experience']) && strlen($_POST['experience'])>0) {
		$experience = trim($_POST['experience']);
	} else {
		$error = 'Please select experience';
	}
	if(isset($_POST['rate']) && strlen($_POST['rate'])>0) {
		$rate = trim($_POST['rate']);
	} else {
		$rate = '0';
	}		
	if(isset($_POST['category']) && strlen($_POST['category'])>0) {
		$category = trim($_POST['category']);
	} else {
		$error = 'Please select category';
	}	
	
	if(empty($error)) {					
		
		$insertQry = "INSERT INTO tbl_technician_category(technician_id, category_id, rate, experience, verified) VALUES (
		'".mysqli_real_escape_string($con, $technicianId)."',
		'".mysqli_real_escape_string($con, $category)."',
		 '".mysqli_real_escape_string($con, $rate)."',
		 '".mysqli_real_escape_string($con, $experience)."', 'yes')";
		
		$execInsert = mysqli_query($con,$insertQry);
		$insert_id = mysqli_insert_id($con);	
		if($execInsert) {						
			$success = 'Category Added in Technician';
			header('Location:index.php?page=technician_category&tech_id='.$technicianId.'&success='.$success);
			exit();			
		}				
	} 
}

if(isset($_POST['update'])) {
	
	//print_r($_POST); 
	
	if(isset($_POST['experience']) && strlen($_POST['experience'])>0) {
		$experience = trim($_POST['experience']);
	} else {
		$error = 'Please select experience';
	}
	if(isset($_POST['rate']) && strlen($_POST['rate'])>0) {
		$rate = trim($_POST['rate']);
	} else {
		$rate = '0';
	}		
	if(isset($_POST['category']) && $_POST['category']!='') {
		$category = trim($_POST['category']);
	} else {
		$error = 'Please select category';
	}	
	
	//exit;
	
	if(empty($error)) {							
		$updateQry = "UPDATE tbl_technician_category SET technician_id = '".$technicianId."', category_id = '".$category."', rate = '".$rate."', experience = '".$experience."' WHERE id = '".$editId."'";
		$execUpdate = mysqli_query($con,$updateQry);				
		if($execUpdate) {
			$success = 'Updated Technician Category Successfully';
			header('Location:index.php?page=technician_category&tech_id='.$technicianId.'&success='.$success);
			exit();			
		}				
	} 
}

?>