<?php 
require_once("header.php");
require_once("session_check.php");
?>

<?php
if(isset($_POST['send']))
{	
	//UPLOAD IMAGE
	if($_FILES['pic']['name']!="")
	{	
		$photopath = pathinfo($_FILES['pic']['name']);
		$extension = $photopath['extension'];
		$source = $_FILES['pic']['tmp_name'];
		$pic = time().".".$extension;
		$destination = "uploads/property/".$pic;
		move_uploaded_file($source,$destination);
	}
	else
	{
		$pic = '';
	}
	
	if($_POST['bhk']!="")
	{
	    if($_POST['bhk'] == '1BHK')
	    {
	        $bedroom = 1;
	    }
	    if($_POST['bhk'] == '2BHK')
	    {
	        $bedroom = 2;
	    }
	    if($_POST['bhk'] == '3BHK')
	    {
	        $bedroom = 3;
	    }
	    if($_POST['bhk'] == '4BHK')
	    {
	        $bedroom = 4;
	    }
	}
	else
	{
	    $bedroom = 0;
	}

	$sql_insert = "INSERT INTO `pd_property` SET `broker_id` = '".$_SESSION['USER_ID']."',`type_id` = '".$_POST['type_id']."',`subtype_id` = '".$_POST['subtype_id']."',`location_id` = '".$_POST['location_id']."',`address` = '".addslashes($_POST['address'])."',`landmark` = '".addslashes($_POST['landmark'])."',`zip` = '".addslashes($_POST['zip'])."',`carpet_area` = '".$_POST['carpet_area']."',`plot_area` = '".$_POST['plot_area']."',`otype_id` = '".$_POST['otype_id']."',`bhk` = '".$_POST['bhk']."',`bedroom` = '".$bedroom."',`bathroom` = '".$_POST['bathroom']."',`balcony` = '".$_POST['balcony']."',`floor` = '".$_POST['floor']."',`price` = '".$_POST['price']."',`cover_photo` = '".$pic."',`description` = '".addslashes($_POST['description'])."',`furnishing_type` = '".$_POST['furnishing_type']."',`open_sides` = '".$_POST['open_sides']."',`property_status` = 'Inactive'";
	$exe_insert = mysqli_query($connection,$sql_insert);
	
	$last_id = mysqli_insert_id($connection);
	
	header("location: add-property-photo.php?pid=".$last_id);
	exit;
}
?>

		<script language="javascript" type="text/javascript">
		function form_validation()
		{
			if(document.getElementById('location_id').value == "")
			{
				alert("Please select location");
				return false;
			}
			
			if(document.getElementById('address').value == "")
			{
				alert("Please enter address");
				document.getElementById('address').focus();
				return false;
			}
			
			if(document.getElementById('zip').value == "")
			{
				alert("Please enter pincode");
				document.getElementById('zip').focus();
				return false;
			}
		   
			if(document.getElementById('otype_id').value == "")
			{
				alert("Please select ownership type");
				return false;
			}

			if(document.getElementById('type_id').value == 1)
			{
    			if(document.getElementById('carpet_area').value == "")
    			{
    				alert("Please enter carpet area");
    				document.getElementById('carpet_area').focus();
    				return false;
    			}
    			
    			if(document.getElementById('floor').value == "")
    			{
    				alert("Please select floor");
    				return false;
    			}
    			
    			if(document.getElementById('bhk').value == "")
    			{
    				alert("Please select BHK");
    				return false;
    			}
    			
    			if(document.getElementById('bathroom').value == "")
    			{
    				alert("Please select bathroom");
    				return false;
    			}
    			
    			if(document.getElementById('balcony').value == "")
    			{
    				alert("Please select balcony");
    				return false;
    			}
    			
    			if(document.getElementById('furnishing_type').value == "")
    			{
    				alert("Please select furnishing status");
    				return false;
    			}
			}
			if(document.getElementById('type_id').value == 2)
			{
    			if(document.getElementById('carpet_area').value == "")
    			{
    				alert("Please enter carpet area");
    				document.getElementById('carpet_area').focus();
    				return false;
    			}
    			
    			if(document.getElementById('floor').value == "")
    			{
    				alert("Please select floor");
    				return false;
    			}
    			
    			if(document.getElementById('bathroom').value == "")
    			{
    				alert("Please select bathroom");
    				return false;
    			}
    			
    			if(document.getElementById('furnishing_type').value == "")
    			{
    				alert("Please select furnishing status");
    				return false;
    			}
			}
			
			if(document.getElementById('type_id').value == 3)
			{
    			if(document.getElementById('plot_area').value == "")
    			{
    				alert("Please enter total area");
    				document.getElementById('carpet_area').focus();
    				return false;
    			}
    			
    			if(document.getElementById('open_sides').value == "")
    			{
    				alert("Please select open sides");
    				return false;
    			}
			}
			
			if(document.getElementById('price').value == "")
			{
				alert("Please enter price");
				document.getElementById('price').focus();
				return false;
			}
			
			if(document.getElementById('pic').value == '')
        	{
        		alert("Please upload cover photo");
        		return false;
        	}
        	
        	if(document.getElementById('description').value == "")
			{
				alert("Please enter description");
				document.getElementById('description').focus();
				return false;
			}
		}
		
		function sub_type(pval)
		{
           	var str = {type_id:pval};
        	$.ajax({
        		type: "post",
        		url: "ajax_property_type.php",
        		data: str,
        		success: function(data) 
        		{
        		    $('#subtype').html(data);
        		    if(pval == 2)
        		    {
        		        $('#div_cp').show();
        		        $('#div_cp1').hide();
        		        $('#div_bhk').hide();
        		        $('#div_floor').show();
        		        $('#div_bath').show();
        		        $('#div_bal').hide();
        		        $('#div_fur').show();
        		        $('#div_open').hide();
        		    }
        		    if(pval == 3)
        		    { 
        		        $('#div_cp').hide();
        		        $('#div_cp1').show();
        		        $('#div_bhk').hide();
        		        $('#div_bath').hide();
        		        $('#div_bal').hide();
        		        $('#div_floor').hide();
        		        $('#div_fur').hide();
        		        $('#div_open').show();
        		    }
        		    if(pval == 1)
        		    {
        		        $('#div_cp').show();
        		        $('#div_cp1').hide();
        		        $('#div_bhk').show();
        		        $('#div_bath').show();
        		        $('#div_bal').show();
        		        $('#div_floor').show();
        		        $('#div_fur').show();
        		        $('#div_open').hide();
        		    }
        		}
        	});
		}
		</script>

		<!-- content area start -->
		<section id="about">
			<div class="container mt-10 pb-10 pt-0">		
				<div class="row">
					<div class="col-lg-4">
						<?php require_once("user_left_panel.php");?>
					</div>
					<div class="col-lg-8">
					    <h1 class="title mt-5 mb-15">Add New Property</h1>
						<p class="text-left" style="color:#ff8e00;">Step 1 of 2</p>
						<form id="" action="" method="post" class="contact-form" enctype="multipart/form-data" onsubmit="return form_validation();">
						<div class="row">
							<div class="col-lg-12 form-group">
								<select name="location_id" id="location_id" class="common-input form-control">
								   <option value="">Select Location *</option>
							    <?php
								$sql_location = "SELECT * FROM `pd_locations` WHERE `location_status` = 'Active' ORDER BY `location_id` ASC";
								$exe_location = mysqli_query($connection,$sql_location);
								while($arr_location = mysqli_fetch_array($exe_location))
								{
									?>
									<option value="<?php echo $arr_location['location_id'];?>"><?php echo stripslashes($arr_location['location']);?></option> 
									<?php
								}
								?>
								</select>
							</div>
							<div class="col-lg-6 form-group">
								<select name="type_id" id="type_id" class="common-input form-control" onchange="sub_type(this.value);">
							    <?php
								$sql_type = "SELECT * FROM `pd_property_type` WHERE `type_status` = 'Active' ORDER BY `type_id` ASC";
								$exe_type = mysqli_query($connection,$sql_type);
								while($arr_type = mysqli_fetch_array($exe_type))
								{
									?>
									<option value="<?php echo $arr_type['type_id'];?>">Category - <?php echo stripslashes($arr_type['type']);?></option> 
									<?php
								}
								?>
								</select>
							</div>
							<div class="col-lg-6 form-group" id="subtype">
								<select name="subtype_id" id="subtype_id" class="common-input form-control">
							    <?php
                                $sql_type1 = "SELECT * FROM `pd_property_subtype` WHERE `type_id`  = '1' AND `subtype_status` = 'Active' ORDER BY `subtype_id` ASC";
                                $exe_type1 = mysqli_query($connection,$sql_type1);
                                while($arr_type1 = mysqli_fetch_array($exe_type1))
                                {
                                	?>
                                	<option value="<?php echo $arr_type1['subtype_id'];?>"><?php echo stripslashes($arr_type1['subtype']);?></option> 
                                	<?php
                                }
                                ?>
								</select>
							</div>
							<div class="col-lg-12 form-group">
								<input name="address" id="address" placeholder="Enter Address *" class="common-input form-control" type="text">
							</div>
							<div class="col-lg-6 form-group">
								<input name="landmark" id="landmark" placeholder="Enter Landmark" class="common-input form-control" type="text">
							</div>
							<div class="col-lg-6 form-group">
								<input name="zip" id="zip" placeholder="Enter Pincode *" class="common-input form-control" type="text">
							</div>
							<div class="col-lg-6 form-group">
								<select name="otype_id" id="otype_id" class="common-input form-control">
								    <option value="">Select Ownership Type</option> 
							    <?php
                                $sql_type2 = "SELECT * FROM `pd_ownership_type` WHERE `otype_status` = 'Active' ORDER BY `otype_id` ASC";
                                $exe_type2 = mysqli_query($connection,$sql_type2);
                                while($arr_type2 = mysqli_fetch_array($exe_type2))
                                {
                                	?>
                                	<option value="<?php echo $arr_type2['otype_id'];?>"><?php echo stripslashes($arr_type2['otype']);?></option> 
                                	<?php
                                }
                                ?>
								</select>
							</div>
							<div class="col-lg-6 form-group" id="div_cp">
								<input name="carpet_area" id="carpet_area" placeholder="Enter Carpet Area in sqft *" class="common-input form-control" type="text">
							</div>
							<div class="col-lg-6 form-group" id="div_cp1" style="display:none;">
								<input name="plot_area" id="plot_area" placeholder="Enter Total Area in sqft *" class="common-input form-control" type="text">
							</div>
							<div class="col-lg-6 form-group" id="div_floor">
								<select name="floor" id="floor" class="common-input form-control">
							        <option value="">Select Floor *</option>
									<option value="0">Ground</option> 
									<option value="1st">1st</option> 
									<option value="2nd">2nd</option> 
									<option value="3rd">3rd</option>
									<option value="4th">4th</option> 
									<option value="5th">5th</option> 
									<option value="6th">6th</option>
                                    <option value="7th">7th</option>
									<option value="8th">8th</option>
									<option value="9th">9th</option>
									<option value="10th">10th</option>
								</select>
							</div>
							<div class="col-lg-6 form-group" id="div_bhk">
								<select name="bhk" id="bhk" class="common-input form-control">
							        <option value="">Select BHK *</option>
							        <option value="1BHK">1BHK</option>
									<option value="2BHK">2BHK</option> 
									<option value="3BHK">3BHK</option> 
									<option value="4BHK">4BHK</option> 
								</select>
							</div>
							<div class="col-lg-6 form-group" id="div_bath">
								<select name="bathroom" id="bathroom" class="common-input form-control">
							        <option value="">Select Bathroom *</option>
									<option value="1">1</option>
									<option value="2">2</option>
									<option value="3">3</option> 
								</select>
							</div>
							<div class="col-lg-6 form-group" id="div_bal">
								<select name="balcony" id="balcony" class="common-input form-control">
							        <option value="">Select Balcony *</option>
									<option value="1">1</option>
									<option value="2">2</option>
									<option value="3">3</option> 
								</select>
							</div>
							<div class="col-lg-6 form-group" id="div_fur">
								<select name="furnishing_type" id="furnishing_type" class="common-input form-control">
							        <option value="">Select Furnishing Status *</option>
									<option value="Fully Furnished">Fully Furnished</option>
									<option value="Semi Furnished">Semi Furnished</option>
									<option value="Un Furnished">Un Furnished</option>
									<option value="Bareshell">Bareshell</option>
								</select>
							</div>
							<div class="col-lg-6 form-group" id="div_open" style="display:none;">
								<select name="open_sides" id="open_sides" class="common-input form-control">
							        <option value="">Select Open Sides *</option>
									<option value="1">1</option>
									<option value="2">2</option>
									<option value="3">3</option>
                                    <option value="4">4</option>
                                </select>
							</div>
							<div class="col-lg-6 form-group">
								<input name="price" id="price" placeholder="Enter Price *" class="common-input form-control" type="text">
							</div>
							<div class="col-lg-12 form-group">
								<textarea name="description" id="description" placeholder="Enter Property Description *" class="common-input form-control" style="height:200px;"></textarea>
							</div>
							<div class="col-lg-12 form-group">
							    <p style="text-align:left;">Upload Cover Photo *</p>
								<input name="pic" id="pic" type="file">
							</div>
							<div class="col-lg-12 form-group">
								<button class="btn btn-flat btn-theme-colored  border-left-theme-color-2-4px" type="submit" style="float: left;">Save & Continue</button>
							</div>
						</div>
						<input type="hidden" name="send" id="send" value="1" />
						</form>	
					</div>
				</div>
			</div>	
		</section>
		<!-- content area end -->

<?php require_once("footer.php");?>