<?php
include('../../../dbconnection/dbconnection.php');

if(isset($_POST["btn_action"])){
	if($_POST["btn_action"]==="Add-Services"){
		$title=strtolower($_POST["stitle"]);
		$desc=$_POST["desc"];

		
							
		$sql=$connect->prepare("SELECT * FROM `services` WHERE service='$title'");
	  $sql->execute();
	 $count=COUNT($sql->fetchAll());
		if($count===0){
									$folder="covers";
									$subfolder=$folder."/".$title;
				
							$files_name=$_FILES["file"]["name"];
							$type=$_FILES["file"]["type"];
							$tmp=$_FILES["file"]["tmp_name"];
							$tmpp=$_FILES["file"]["tmp_name"];
							$original=$_FILES['file']["name"];
							$profile=$_FILES['file']["name"];
							$directory=$subfolder."/";
							$ext=strtolower(end(explode(".",$files_name)));
							$allowed_all=array("jpg","jpeg","png","gif");
							$allowed_resize=array("jpg","jpeg","png");
							create_directory($folder,$subfolder);
							if(!empty(@getimagesize($tmp))){
									$ofile="large-".date(time()).time().".".$ext;
									$file="small-".date(time()).time().".".$ext;
									$large=strtolower($directory.$ofile);
									$profile=strtolower($directory.$file);
									if(in_array($ext,$allowed_all)){
										list($width,$height)=getimagesize($tmp);
										if($width>110 || $height>100){
												if(in_array($ext,$allowed_resize)){
													if($ext=="png"){$tmp=imagecreatefrompng($tmp);}
													if($ext=="jpg"){$tmp=imagecreatefromjpeg($tmp);}
													if($ext=="jpeg"){$tmp=imagecreatefromjpeg($tmp);}
													$profile=resize($width,$height,$tmp,$profile);	
													// move_uploaded_file($tmpp,$large);
												}
										
										}
										else{
											if(!empty($tmp)){
												move_uploaded_file($tmp,$profile);
												// move_uploaded_file($tmpp,$large);;
											}
											
										}
									
									
									$user=$_SESSION['id'];
									
									$status=1;
									$query = "
										INSERT INTO `services` (service,description,date,photo,status) 
													  VALUES (:title, :desc,:date,:cover,:status)
										";
										
										$statement=$connect->prepare($query);
										$statement->execute(
											array(
												":title"		=>	$title, 
												":desc" 		=>	$_POST["desc"], 
												":date"			=>	time(),
												":cover"			=>	$profile,
												":status"		=>	$status
											)
										);
										
										
										$result = $statement->fetchAll();
											if(isset($result))
											{
												echo 'New Service Created';
												
											}
									}
									else {
										// move_uploaded_file($tmp,$profile);
										// move_uploaded_file($tmpp,$large);
										echo 'Please Select an image file';
									}
									
				}else{
					
					echo "Image file Error";
				}	
		
			}else{
				
				echo "Service Title Already Exists";
			}
			
	}	
	
	
}


		function resize($width,$height,$img,$store){
		// if($width>$height){
		// $new_height=($height/$width )*100;
		$new_width=110;
		// }
		// else{
			$new_height=100;
			// $new_width=($width/$height)*100;
		// }
		
		$tmp_img=imagecreatetruecolor($new_width,$new_height);
		imagecopyresampled($tmp_img,$img,0,0,0,0,$new_width,$new_height,$width,$height);
		if(imagepng($tmp_img,$store,1)){
				chmod($store, 0777);
			return $file_name=$store;
			}
		if(imagejpeg($tmp_img,$store,100)){
				chmod($store, 0777);
		return 	$file_name=$store;
			}
	}
			
function lresize($width,$height,$img,$store){
		if($width>$height){
		$new_height=($height/$width )*300;
		$new_width=300;}
		else{
			$new_height=300;
			$new_width=($width/$height)*300;
		}
		
		$tmp_img=imagecreatetruecolor($new_width,$new_height);
		imagecopyresampled($tmp_img,$img,0,0,0,0,$new_width,$new_height,$width,$height);
		if(imagepng($tmp_img,$store,1)){
				chmod($store, 0777);
			return $file_name=$store;
			}
		if(imagejpeg($tmp_img,$store,100)){
				chmod($store, 0777);
		return 	$file_name=$store;
			}
	}
	
function create_directory($folda,$subfolda){
	
if(!is_dir($folda)){mkdir(strtolower($folda));mkdir(strtolower($subfolda));}else{if(!is_dir($subfolda)){mkdir(strtolower($subfolda));}}
		
		
}

?>