<?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['upload_pdf'])) {	
			if($_FILES["upload_pdf"]["name"] != '') {

				$valid_extensions = array('pdf'); // valid extensions
				$path = '../upload/PDF/procurements/'; // upload directory	

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

				// check valid format
				if(in_array($ext, $valid_extensions)) 
				{
					$pdf_name = $date_today.'_'.rand(100000, 999999).'.'.$ext;
					$path = $path.$pdf_name;
						
					if (file_exists($path))
					{			
						echo 'data exist'; // File Exists
					
					} else {
						
						if($log_lang == 1) {
							if(move_uploaded_file($tmp,$path)) {
								foreach($_POST['division'] as $div) {
									$sqlg = mysqli_query($conn, "SELECT * FROM procurements WHERE division='$div'");
									
									if(mysqli_num_rows($sqlg) == 0) {
										$sql = "INSERT INTO procurements VALUES (NULL ,'$date_today', '$pdf_name', '$div', '$user_id')";
			
										if(mysqli_query($conn, $sql)) {
											echo 'success'; // Success
										
										} else {
											echo 'error'; // Sql Error
										}
									
									} else {
										echo 'data exist'; //Data Already Inserted
									}		
								}
							
							} else {				
							  //exit("Error While uploading image on the server");
							  echo 'error1'; // Uploading Error
							}		
						}
					}
				} 
				else 
				{
					echo 'error2'; // Invaild File
				}
			 
			}
			//echo json_encode($response);	
		}
		
		/****************************** Remove *************************************/
		
		if (array_key_exists('remove_reports', $_POST)) {

			if($log_lang == 1) {
			
				$group_id = htmlspecialchars(strip_tags(trim($_POST['gid'])), ENT_QUOTES, 'UTF-8');		
				$sql = "DELETE FROM procurements WHERE id='$group_id'";
				
				if(!empty($group_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"));
				}
			
			}

		}
		
		/******************************************************************/		

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