<?php
header('Content-type: application/json');
include '../db/db.php';
session_start();
$log = htmlspecialchars(trim($_SESSION['user']), ENT_QUOTES, 'UTF-8');
$log_lang = htmlspecialchars(trim($_SESSION['lang']), ENT_QUOTES, 'UTF-8');

$menu = htmlspecialchars(trim($_SESSION['menu']), ENT_QUOTES, 'UTF-8');
$emp_id = htmlspecialchars(trim($_SESSION['emp_id']), 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 user_priv_q WHERE emp_id='$emp_id' AND user='$log' AND menu_id='$menu'";
	$resultu = mysqli_query($conn, $sqlu);
	$countu = mysqli_num_rows($resultu);

	if($countu == 1){
		$rowc = mysqli_fetch_assoc($resultu);
		$user_id = $rowc['user_id'];
		
		date_default_timezone_set('Asia/Colombo');
		$date_today = date("Y-m-d");

		/****************************** Insert Eng *************************************/

		if(isset($_FILES['img_upload'])) {	
			if($_FILES["img_upload"]["name"] != '') {
				$name = htmlspecialchars(strip_tags(trim($_POST['name'])), ENT_QUOTES, 'UTF-8');
				$designation = htmlspecialchars(strip_tags(trim($_POST['designation'])), ENT_QUOTES, 'UTF-8');
				$tel_office = htmlspecialchars(strip_tags(trim($_POST['tel_office'])), ENT_QUOTES, 'UTF-8');	
				$fax = htmlspecialchars(strip_tags(trim($_POST['fax'])), ENT_QUOTES, 'UTF-8');	
				$email = htmlspecialchars(strip_tags(trim($_POST['email'])), ENT_QUOTES, 'UTF-8');	
				$level = htmlspecialchars(strip_tags(trim($_POST['level'])), ENT_QUOTES, 'UTF-8');	

				$valid_extensions = array('png','jpg','jpeg'); // valid extensions
				$path = '../upload/carder/'; // upload directory	

				// get uploaded file's extension
				$ext = strtolower(pathinfo($_FILES["img_upload"]["name"], PATHINFO_EXTENSION));
				$tmp = $_FILES['img_upload']['tmp_name'];

				// check valid format
				if(in_array($ext, $valid_extensions)) 
				{					
					$img_name = rand(100000, 999999).'.'.$ext;	
					$path = $path.$img_name;	
						
					if (file_exists($path))
					{			
						$response['status'] = '2'; // File Exists
					
					} else {		
						if(move_uploaded_file($tmp,$path)) 
						{  
							if($log_lang == 1) {	
								$sql = "INSERT INTO carder_details SET name='$name', designation='$designation', tel_office='$tel_office', fax='$fax', email='$email', level='$level', prof_pic='$img_name', date='$date_today', user='$user_id', del='0'";

								if(mysqli_query($conn, $sql)) {
									$response['status'] = '1'; // Success
								
								} else {
									$response['status'] = '3'; // Sql Error
								}
							}
						
						} else{				
						  //exit("Error While uploading image on the server");
						  $response['status'] = '4'; // Uploading Error
						}
					}
				} 
				else 
				{
					$response['status'] = '5'; // Invaild File
				}
			 
			}
			echo json_encode($response);	
		}
		
		/****************************** Insert Sinhala/Tamil *************************************/
		
		/****************************** Remove *************************************/
		
		if (array_key_exists('remove_reg', $_POST)) {	
			
			if($log_lang == 1) {			
				$reg_id = htmlspecialchars(strip_tags(trim($_POST['reg_id'])), ENT_QUOTES, 'UTF-8');		
				$sql = "UPDATE carder_details SET del=1 WHERE id='$reg_id'";
				
				if(!empty($reg_id)) {
				
					if(mysqli_query($conn, $sql)) {
						echo json_encode(array("msgType" => 1, "msg" => "Successfully Removed"));
					
					} else {
						echo json_encode(array("msgType" => 2, "msg" => "Sorry ! not deleted"));
					}
				
				} else {
					echo json_encode(array("msgType" => 2, "msg" => "Sorry ! empty id"));
				}
			
			}
		}		
		
		/****************************** Load Form Data *************************************/
		
		if (array_key_exists('get_dataset', $_POST)) {

			$reg_id = htmlspecialchars(strip_tags(trim($_POST['reg_id'])), ENT_QUOTES, 'UTF-8');

			$data = array();
			$sql = mysqli_query($conn, "SELECT * FROM carder_details WHERE id=$reg_id AND del=0");

			while ($row = mysqli_fetch_assoc($sql)) {
				$data[] = $row;
			}
			echo json_encode($data);
		}
		
		/****************************** Update Eng/Sinhala/Tamil *************************************/
		
		if (array_key_exists('update_dr_lang', $_POST)) {
			$reg_id = htmlspecialchars(strip_tags(trim($_POST['reg_id'])), ENT_QUOTES, 'UTF-8');

			$name = htmlspecialchars(strip_tags(trim($_POST['name'])), ENT_QUOTES, 'UTF-8');
			$designation = htmlspecialchars(strip_tags(trim($_POST['des'])), ENT_QUOTES, 'UTF-8');
			$tel_office = htmlspecialchars(strip_tags(trim($_POST['tel_office'])), ENT_QUOTES, 'UTF-8');	
			$fax = htmlspecialchars(strip_tags(trim($_POST['fax'])), ENT_QUOTES, 'UTF-8');	
			$email = htmlspecialchars(strip_tags(trim($_POST['email'])), ENT_QUOTES, 'UTF-8');	
			$level = htmlspecialchars(strip_tags(trim($_POST['level'])), ENT_QUOTES, 'UTF-8');
				
			$sql_up = "UPDATE carder_details SET name='$name', designation='$designation', tel_office='$tel_office', fax='$fax', email='$email', level='$level', user='$user_id' WHERE id='$reg_id'";
			
			if($log_lang == 1) {
				if(!empty($reg_id)) {
					
					if(mysqli_query($conn, $sql_up)) {
						echo json_encode(array("msgType" => 1, "msg" => "Successfully Removed"));
					
					} else {
						echo json_encode(array("msgType" => 2, "msg" => "Sorry ! not deleted"));
					}
				
				} else {
					echo json_encode(array("msgType" => 3, "msg" => "Sorry ! empty id"));
				}
			
			}			
			
		}

		/****************************** Update Image *************************************/

		if(isset($_FILES['img_upload_update'])) {	
			if($_FILES["img_upload_update"]["name"] != '') {
				$reg_id = htmlspecialchars(strip_tags(trim($_POST['reg_id'])), ENT_QUOTES, 'UTF-8');

				$name = htmlspecialchars(strip_tags(trim($_POST['name'])), ENT_QUOTES, 'UTF-8');
				$designation = htmlspecialchars(strip_tags(trim($_POST['des'])), ENT_QUOTES, 'UTF-8');
				$tel_office = htmlspecialchars(strip_tags(trim($_POST['tel_office'])), ENT_QUOTES, 'UTF-8');	
				$fax = htmlspecialchars(strip_tags(trim($_POST['fax'])), ENT_QUOTES, 'UTF-8');	
				$email = htmlspecialchars(strip_tags(trim($_POST['email'])), ENT_QUOTES, 'UTF-8');	
				$level = htmlspecialchars(strip_tags(trim($_POST['level'])), ENT_QUOTES, 'UTF-8');
			
				$valid_extensions = array('png','jpg','jpeg'); // valid extensions
				$path = '../upload/carder/'; // upload directory	

				// get uploaded file's extension
				$ext = strtolower(pathinfo($_FILES["img_upload_update"]["name"], PATHINFO_EXTENSION));
				$tmp = $_FILES['img_upload_update']['tmp_name'];

				// check valid format
				if(in_array($ext, $valid_extensions)) 
				{					
					$img_name = rand(100000, 999999).'.'.$ext;	
					$path = $path.$img_name;	
						
					if (file_exists($path))
					{			
						$response['status'] = '2'; // File Exists
					
					} else if(!empty($reg_id)) {
						
						$sql_eng = mysqli_fetch_assoc(mysqli_query($conn, "SELECT prof_pic FROM carder_details WHERE id='$reg_id'"));	

						if(!empty($sql_eng['prof_pic'])) {
							$path_old = "../upload/carder/".$sql_eng['prof_pic'];
							$files = glob($path_old);
							
							foreach($files as $file) {
								unlink($file);
							}
						}
						
						if(move_uploaded_file($tmp,$path)) 
						{  
							if($log_lang == 1) {					
								$sql = "UPDATE carder_details SET name='$name', designation='$designation', tel_office='$tel_office', fax='$fax', email='$email', level='$level', user='$user_id', prof_pic='$img_name' WHERE id='$reg_id'";
								
								if(mysqli_query($conn, $sql)) {
									$response['status'] = '1'; // Success
								
								} else {
									$response['status'] = '3'; // Sql Error
								}
							}
						
						} else{				
						  //exit("Error While uploading image on the server");
						  $response['status'] = '4'; // Uploading Error
						}
					
					} else {
						$response['status'] = '6'; // Empty ID
					}
				} 
				else 
				{
					$response['status'] = '5'; // Invaild File
				}
			 
			}
			echo json_encode($response);	
		}		

		/******************************************************************/		

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