<?php
if(!isset($_SESSION)) 
{ 
session_start(); 
} 
if(!isset($_SESSION['is_login']) ){
header("location: login.php");
?>
<script>
window.location="login.php";
</script>
<?php
}

// $permission  = explode(',',$_SESSION['permission_id']);
// if(!in_array(19,$permission) && !in_array(1,$permission)  ){

// $op['is_success'] = false;
// $op['data'] 	  = "عذرا انت غير مخول للوصل الى هذه الصفحة";
// echo json_encode($op);
// exit;

// }

date_default_timezone_set($_SESSION['SYSTimezone']);

		$Validator = array('success'=>false, 'data'=>array());
		// date_default_timezone_set('Asia/Baghdad');
		$LogTime = date('Y-m-d H:i:s');

		require_once "db_connect.php";

		if(isset($_GET['type']) AND $_GET['type']=='load'){
							
							$sys_res = $db->query("SELECT * FROM system_settings");
							$sysinfo = $sys_res->fetch_assoc();
							
							$_SESSION['USD_TO_IQD']      	= $sysinfo['SYSUSDRATE'];
							$_SESSION['company_logo']    	= $sysinfo['SYSCompanyLogo'];
							$_SESSION['company_name']    	= $sysinfo['SYSCompanyName'];
							$_SESSION['company_mngr_en'] 	= $sysinfo['SYSCompanyManager_en'];
							$_SESSION['company_mngr_ku'] 	= $sysinfo['SYSCompanyManager_ku'];
							$_SESSION['company_mngr_ar'] 	= $sysinfo['SYSCompanyManager_ar'];
							$_SESSION['company_address'] 	= $sysinfo['SYSCompanyAddress'];
							$_SESSION['company_phone']   	= $sysinfo['SYSCompanyPhone'];
							$_SESSION['company_email']   	= $sysinfo['SYSCompanyEmail'];
							$_SESSION['notify_email']   	= $sysinfo['notify_email'];
							$_SESSION['SYSName']   			= $sysinfo['SYSName'];
							$_SESSION['SYSVersion']   	 	= $sysinfo['SYSVersion']; 
							$_SESSION['SYSSetupDate']    	= date('d-M-Y',strtotime($sysinfo['SYSSetupDate'])) ;
							$_SESSION['SYSTimezone']     	= $sysinfo['SYSTimezone'];
						exit();
		}

		if($_POST['type']=='update_profile_image'){
					$LogTime = date('Y-m-d H:i:s');
					$target_dir = "../image/sys/";
					$fileBook = rand(1000,100000) . "-" . $_FILES['profile_image']['name'];
					$target_file = $target_dir . basename($fileBook);
					$uploadOk = 1;
					$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
					// Check if image file is a actual image or fake image

			    $check = getimagesize($_FILES["profile_image"]["tmp_name"]);
			    if($check == false) {
							$error= " File is not an image. ";
							$State=false;
			    }else {
			    	$State=true;
			    }


					// Check file size
					if ($_FILES["profile_image"]["size"] > 1500000) {
							$error= " Sorry, your file is too large. ";
							$State=false;
					}
					// Allow certain file formats
					if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
					&& $imageFileType != "gif" ) {
							$error= " Sorry, only JPG, JPEG, PNG & GIF files are allowed. ";
							$State=false;
					}
					// Check if $uploadOk is set to 0 by an error
					if ($State == false) {
							$error= " Sorry, your file was not uploaded. ";
					// if everything is ok, try to upload file
					} else {

					    if (move_uploaded_file($_FILES["profile_image"]["tmp_name"], $target_file)) {
					    	$filePath = 'image/sys/'.$fileBook;
								$sql = "UPDATE system_settings SET SYSCompanyLogo='$filePath' WHERE SYSID = 1";

								$query = $db->query($sql);
								if (!$query) {
											$error= "حصل خطأ: الرجاء اعادة تحميل الصفحة والمحاولة من جديد";
											$State=false;

								}else{
										$error= " Company Logo Successfully updated ";
										$State=true;
								}

					    } else {
									$error= " Sorry, there was an error uploading your file. ";
									$State=false;
					    }
					}

					$Validator['success'] = $State;
					$Validator['data'] = $error;

		}



		if($_POST['type']=='update_company_info'){

			
			$company_name 		= $_POST['company_name'];
			$manger_name_en 	= $_POST['manger_name_en'];
			$manger_name_ar 	= $_POST['manger_name_ar'];
			$manger_name_kr 	= $_POST['manger_name_kr'];
			$company_address 	= $_POST['company_address'];
			$company_phone 		= $_POST['company_phone'];
			$company_email 		= $_POST['company_email'];
			$notify_email 		= $_POST['notify_email'];

			$sql = "UPDATE system_settings SET SYSCompanyName='$company_name', 
											   SYSCompanyManager_en='$manger_name_en',
											   SYSCompanyManager_ku='$manger_name_kr' ,
											   SYSCompanyManager_ar='$manger_name_ar',
											   SYSCompanyAddress='$company_address' ,
											   SYSCompanyPhone='$company_phone',
											   SYSCompanyEmail='$company_email',  
											   notify_email='$notify_email'  
							WHERE SYSID = 1";

			$query = $db->query($sql);
			if (!$query) {
						$error= "حصل خطأ: الرجاء اعادة تحميل الصفحة والمحاولة من جديد";
						$State=false;

			}else{
						$error= "Successfully Update Your Company Profile";
						$State=true;
			}
			$Validator['success'] = $State;
			$Validator['data'] = $error;
		}

		if($_POST['type']=='update_time_zone'){

			
			$time_zone 		= $_POST['time_zone'];
			$usd_rate 		= $_POST['usd_rate'];
			

			$sql = "UPDATE system_settings SET SYSTimezone='$time_zone',SYSUSDRATE='$usd_rate'  WHERE SYSID = 1";

			$query = $db->query($sql);
			if (!$query) {
						$error= "حصل خطأ: الرجاء اعادة تحميل الصفحة والمحاولة من جديد";
						$State=false;

			}else{
						$error= "Successfully Update Your System Information";
						$State=true;
			}
			$Validator['success'] = $State;
			$Validator['data'] = $error;
		}

		
		echo json_encode($Validator);

?>
