<?php
		include_once('../config.php');  
		
		
		if(isset($_POST['submit']))
		{
			$result = mysql_query("select * from users where  ( email='$_POST[login_credentials]' || ph='$_POST[login_credentials]' )  and password='$_POST[password]' "); 

			if(mysql_num_rows($result)>0)
			{
				$row = mysql_fetch_object($result);      
				$_SESSION['cust_id'] = $row->cust_id; 
				$_SESSION['email'] = $row->email;   
				$_SESSION['phone']= $row->ph; 
				$_SESSION['login_service'] = 1;  
			}
		}
		
		
		$submit = 0;
		if(isset($_POST['post_service']))
		{     
			$submit = 1;
			
			if($_POST['location_id'])
				$location_id = $_POST['location_id'];
			else
				$location_id = 0;
			
			if($_POST['sub_category_id'])
				$sub_category_id = $_POST['sub_category_id'];
			else
				$sub_category_id = 0;
			
			$posted_date =  date('Y-m-d H:i:s');
			$tabel_name = "city_tabel_".$_POST['city_id']; 

			$_POST['post_id'] = 2;
			
			if($_POST['privacy'])
					$_POST['privacy'] = 1;
			else
				$_POST['privacy'] = 0; 
			
			
			$_POST['cust_id'] = $_SESSION['cust_id'];
			$_POST['contact_email'] = $_SESSION['email'];
			$_POST['contact_mobile'] = $_SESSION['phone'];
			
				
			if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$tabel_name."'"))==1) 
			{      
				mysql_query("insert into $tabel_name   (category_id,sub_category_id,description,location_id,contact_email,contact_mobile,cust_id,posted_date,company,privacy,post_id) values  ($_POST[category_id],$sub_category_id,'$_POST[description]',$location_id,'$_POST[contact_email]','$_POST[contact_mobile]','$_POST[cust_id]','$posted_date','$_POST[company]',$_POST[privacy],$_POST[post_id]) ");    
			}
			else 
			{     
				mysql_query("CREATE TABLE $tabel_name ( id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, posted_date varchar(200) NOT NULL, company varchar(200) NOT NULL, type tinyint(1) NOT NULL COMMENT 'work from home => 1 part time => 2 full time => 3 internship job => 4', heading text NOT NULL, category_id int(11) NOT NULL,   sub_category_id int(11) NOT NULL,  min_salary varchar(200) NOT NULL,  max_salary varchar(200) NOT NULL,  expr int(11) NOT NULL,  description text NOT NULL, location_id int(11) NOT NULL,  contact_email varchar(200) NOT NULL,  contact_mobile varchar(200) NOT NULL,  cust_id varchar(200) NOT NULL,add_view int(11) NOT NULL,privacy int(11) NOT NULL,post_id int(11) NOT NULL,user_type tinyint(1) NOT NULL)  "); 
				
				mysql_query("insert into $tabel_name   (category_id,sub_category_id,description,location_id,contact_email,contact_mobile,cust_id,posted_date,company,privacy,post_id) values  ($_POST[category_id],$sub_category_id,'$_POST[description]',$location_id,'$_POST[contact_email]','$_POST[contact_mobile]','$_POST[cust_id]','$posted_date','$_POST[company]',$_POST[privacy],$_POST[post_id]) ");       
			}   
			
			$user_job_query = mysql_query("select user_jobs  from users where cust_id='$_POST[cust_id]' ");
			$user_job_ct =  mysql_fetch_row($user_job_query);    
			mysql_query("update users set post_added = post_added+1 where cust_id='$_POST[cust_id]' "); 
			$city_id = $_POST['city_id'];
			if (!preg_match('/'.$city_id.'/',$user_job_ct[0]))
			{  
				$user_jobs =$user_job_ct[0].",".$_POST['city_id']; 
				$user_jobs = trim($user_jobs,",");  
				mysql_query("update users set user_jobs = '$user_jobs' where cust_id='$_POST[cust_id]' ");
			}   
			
			if($_FILES['service_pic']['name'])
			{
				$cust_id = $_POST['cust_id']; 
				$path  = ABS_PATH.'/uploads/'.$cust_id;
				if (!file_exists($path))
				{			 
				mkdir($path,0777);			 
				$path  = ABS_PATH.'/uploads/'.$cust_id.'/service-pic';  
				mkdir($path,0777);
				}       
				$filename = strtoupper( substr(md5(time()),0,16)); 	
				$RD = explode(".",$_FILES['service_pic']['name']);			
				$target_file = $path.'/'.$filename.'.'.end($RD); 
				$Fname = $filename.'.'.end($RD); 
				move_uploaded_file($_FILES['service_pic']['tmp_name'],$target_file);           
			}
			//mysql_query("update  users set service_pic='$Fname' where cust_id = '$_POST[cust_id]'  ");    
		}          
?>

<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
	<title>FormGet Online Responsive Form Builder</title>
	<meta name="robots" content="noindex, nofollow">
	<!-----Including CSS for different screen sizes----->
	<link rel="stylesheet" type="text/css" href="css_files/responsiveform.css">
	<link rel="stylesheet" media="screen and (max-width: 1550px) and (min-width: 1200px)" href="css_files/responsiveform4.css">
	<link rel="stylesheet" media="screen and (max-width: 1200px) and (min-width: 601px)" href="css_files/responsiveform1.css">
	<link rel="stylesheet" media="screen and (max-width: 600px) and (min-width: 351px)" href="css_files/responsiveform2.css">
	<link rel="stylesheet" media="screen and (max-width: 350px)" href="css_files/responsiveform3.css">
	<script src="css_files/ga.js" async="" type="text/javascript"></script>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.js"></script>
	
	
	
	<script> 
		function fun1(val)
		{  
			$.ajax({	
				type: "POST",
				url: "ajax-file1.php",
				data:{city:1,id:val},	
				beforeSend: function(){
					//$("#search-box").css("background","#FFF url(LoaderIcon.gif) no-repeat 165px");
				},
				success: function(val){  
					 $("#sub-location").html(val); 
				}
			});	
		}
		
		
		function fun2(val)
		{  
			$.ajax({	
				type: "POST",
				url: "ajax-file1.php",
				data:{main_service:1,id:val},	
				beforeSend: function(){
					//$("#search-box").css("background","#FFF url(LoaderIcon.gif) no-repeat 165px");
				},
				success: function(val){  
					 $("#sub-service").html(val); 
				}
			});	
		}
		
		
		 
	
	
	$(document).ready(function(){
	
	$("#form_id").validate({
		
		errorElement:'div',
		rules:{ 
		
			company:{
				required:true				 
			},
			sub_serivice:{
				required:true				 
			}
			 
		},
		messages:{	
			
			 	
			 
		}
		});
	});
	
	
	</script>
</head>
<body>
<div id="resize">
**  Resize window to see the changes **
</div>
<div id="envelope">
<header>
    <h2> <?php if($_SESSION['login_service']==0) echo 'Login'; else { ?>  Post Service <a href="logout.php"> Log Out </a> <?php  }?></h2> 
</header>
<hr>
<?php 

	if($_SESSION['login_service']==1)
	{   
		
		
	if($submit == 0)
	{
?>
<form action="" method="post" id="form_id">  
	<label>Select Service Type </label>
	<select name="category_id" onchange="fun2(this.value);" >
	<?php 
	
		$result = mysql_query("select * from service_categories order by id ");  
		while($dt = mysql_fetch_object($result))
		{   
	?>
	<option value="<?php echo $dt->id;?>"><?php echo $dt->name;?></option>
	<?php   
		}
	?> 
	</select>
	
	<div id="sub-service"> 
	<?php  
		$result = mysql_query("select * from sub_service_categories where category_id = 4 ");    
		$option = '<label>Select Service</label> <select name=sub_category_id>'; 
		while($dt = mysql_fetch_object($result)) 
		  $option .='<option value='.$dt->id.'>'.$dt->name.'</option>';   
		 
		$option .='</select>'; 
		echo $option;
	?>
			 
	
	</div>
	<label> Company Name </label>
	<input name="company" placeholder="Company Name" type="text">	
	<label>  Description </label>
	
	<label>Select City </label>
	<select name="city_id" onchange="fun1(this.value);" >
	<?php 
	
		$result = mysql_query("select * from all_cities order by id");  
		while($dt = mysql_fetch_object($result))
		{   
	?>
	<option value="<?php echo $dt->id;?>"><?php echo $dt->name;?></option>
	<?php   
		}
	?> 
	</select> 
	<div id="sub-location">
	<?php  
		$result = mysql_query("select * from city_lacations where city_id= 3 ");
		$option = '<label>Select City Locations</label> <select name=location_id>'; 
		while($dt = mysql_fetch_object($result)) 
		  $option .='<option value='.$dt->id.'>'.$dt->location_name.'</option>';   
		 
		$option .='</select>';  
		echo $option;
	?>
	
	</div>  
	<input name="description" placeholder="Description" type="text">
	<label>  Picture </label>
	<input name="pic" type="file" /> 
	<label>  Privacy </label>
	<input name="privacy" type="checkbox"  /> 
	<input value="Send Message" id="post_service"  name="post_service" type="submit" >		
</form>
<?php 
	}else
	echo  "Your service has been added";
	}else
	{
?>

<form action="" method="post" id="form_id">   
	<label>Email id or Ph </label>
	<input name="login_credentials" placeholder="Email id or Ph"  type="text" />
	
	<label>Password</label>
	<input name="password" placeholder="Password" type="password"  type="text" >	     
	
	<input value="Login"   name="submit" type="submit" >		
</form>
<?php }?>
</div> 
</body></html>