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

<?php
if(isset($_POST['send']))
{	
	//UPLOAD IMAGE
	if($_FILES['pic']['name']!="")
	{	
		unlink("uploads/property/".$_POST['hid_pic']);
		
		$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 = $_POST['hid_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_update = "UPDATE `pd_property` SET `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']."' WHERE `property_id` = '".$_POST['hid_property_id']."'";
	$exe_update = mysqli_query($connection,$sql_update);
	
	$_SESSION['pp_succ_msg'] = 'Property updated succcessfully.';
	header("location: edit-property.php?property_id=".$_POST['hid_property_id']);
	exit;
}

$getProperty = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `pd_property` WHERE `property_id` = '".$_GET['property_id']."'"));
?>

		<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('description').value == "")
			{
				alert("Please enter description");
				document.getElementById('description').focus();
				return false;
			}
		}
		</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">Edit Property</h1>
						<?php if(isset($_SESSION['pp_succ_msg'])) { ?>
						<p class="text-left"><img src="images/tick.png" alt="" /> <?php echo $_SESSION['pp_succ_msg'];?></p>
						<?php 
						unset($_SESSION['pp_succ_msg']);
						} 
						?>
						<form id="" action="" method="post" class="contact-form" enctype="multipart/form-data" onsubmit="return form_validation();">
					    <input type="hidden" name="hid_property_id" id="hid_property_id" value="<?php echo $getProperty['property_id'];?>" />
				        <input type="hidden" name="hid_pic" id="hid_pic" value="<?php echo $getProperty['cover_photo'];?>" />
						<div class="row">
							<div class="col-lg-12 form-group">
							    <p style="text-align:left;">Select Location *</p>
								<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 if($arr_location['location_id'] == $getProperty['location_id']) { ?>selected<?php } ?>><?php echo stripslashes($arr_location['location']);?></option> 
									<?php
								}
								?>
								</select>
							</div>
							<div class="col-lg-6 form-group">
							    <p style="text-align:left;">Select Category *</p>
								<select name="type_id" id="type_id" class="common-input form-control">
							    <?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'];?>" <?php if($arr_type['type_id'] == $getProperty['type_id']) { ?>selected<?php } ?>>Category - <?php echo stripslashes($arr_type['type']);?></option> 
									<?php
								}
								?>
								</select>
							</div>
							<div class="col-lg-6 form-group">
							    <p style="text-align:left;">Select Subcategory *</p>
								<select name="subtype_id" id="subtype_id" class="common-input form-control">
							    <?php
                                $sql_type1 = "SELECT * FROM `pd_property_subtype` WHERE `type_id`  = '".$getProperty['type_id']."' 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 if($arr_type1['subtype_id'] == $getProperty['subtype_id']) { ?>selected<?php } ?>><?php echo stripslashes($arr_type1['subtype']);?></option> 
                                	<?php
                                }
                                ?>
								</select>
							</div>
							<div class="col-lg-12 form-group">
							    <p style="text-align:left;">Address *</p>
								<input name="address" id="address" placeholder="Enter Address *" class="common-input form-control" type="text" value="<?php echo stripslashes($getProperty['address']);?>">
							</div>
							<div class="col-lg-6 form-group">
							    <p style="text-align:left;">Landmark</p>
								<input name="landmark" id="landmark" placeholder="Enter Landmark" class="common-input form-control" type="text" value="<?php echo stripslashes($getProperty['landmark']);?>">
							</div>
							<div class="col-lg-6 form-group">
							    <p style="text-align:left;">Pincode *</p>
								<input name="zip" id="zip" placeholder="Enter Pincode *" class="common-input form-control" type="text" value="<?php echo stripslashes($getProperty['zip']);?>">
							</div>
							<div class="col-lg-6 form-group">
							    <p style="text-align:left;">Ownership Type *</p>
								<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 if($arr_type2['otype_id']==$getProperty['otype_id']) { ?>selected<?php } ?>><?php echo stripslashes($arr_type2['otype']);?></option> 
                                	<?php
                                }
                                ?>
								</select>
							</div>
							<?php if($getProperty['type_id'] == 1 || $getProperty['type_id'] == 2) { ?>
							<div class="col-lg-6 form-group">
							    <p style="text-align:left;">Carpet Area in sqft *</p>
								<input name="carpet_area" id="carpet_area" placeholder="Enter Carpet Area in sqft *" class="common-input form-control" type="text" value="<?php echo stripslashes($getProperty['carpet_area']);?>">
							</div>
							<?php } ?>
							<?php if($getProperty['type_id'] == 3) { ?>
							<div class="col-lg-6 form-group">
							    <p style="text-align:left;">Total Area in sqft *</p>
								<input name="plot_area" id="plot_area" placeholder="Enter Total Area in sqft *" class="common-input form-control" type="text" value="<?php echo stripslashes($getProperty['plot_area']);?>">
							</div>
							<?php } ?>
							<?php if($getProperty['type_id'] == 1 || $getProperty['type_id'] == 2) { ?>
							<div class="col-lg-6 form-group">
							    <p style="text-align:left;">Select Floor *</p>
								<select name="floor" id="floor" class="common-input form-control">
							        <option value="">Select Floor *</option>
							        <option value="0" <?php if($getProperty['floor'] == "0") { ?>selected<?php } ?>>Ground</option>
									<option value="1" <?php if($getProperty['floor'] == "1st") { ?>selected<?php } ?>>1st</option> 
									<option value="2" <?php if($getProperty['floor'] == "2nd") { ?>selected<?php } ?>>2nd</option> 
									<option value="3" <?php if($getProperty['floor'] == "3rd") { ?>selected<?php } ?>>3rd</option>
									<option value="4" <?php if($getProperty['floor'] == "4th") { ?>selected<?php } ?>>4th</option> 
									<option value="5" <?php if($getProperty['floor'] == "5th") { ?>selected<?php } ?>>5th</option> 
									<option value="6" <?php if($getProperty['floor'] == "6th") { ?>selected<?php } ?>>6th</option>
									<option value="7" <?php if($getProperty['floor'] == "7th") { ?>selected<?php } ?>>7th</option>
									<option value="7" <?php if($getProperty['floor'] == "8th") { ?>selected<?php } ?>>8th</option>
									<option value="7" <?php if($getProperty['floor'] == "9th") { ?>selected<?php } ?>>9th</option>
									<option value="7" <?php if($getProperty['floor'] == "10th") { ?>selected<?php } ?>>10th</option>
								</select>
							</div>
							<?php } ?>
							<?php if($getProperty['type_id'] == 1) { ?>
							<div class="col-lg-6 form-group">
							    <p style="text-align:left;">Select BHK *</p>
								<select name="bhk" id="bhk" class="common-input form-control">
							        <option value="">Select BHK *</option>
									<option value="1BHK" <?php if($getProperty['bhk'] == "1BHK") { ?>selected<?php } ?>>1BHK</option>
									<option value="2BHK" <?php if($getProperty['bhk'] == "2BHK") { ?>selected<?php } ?>>2BHK</option>
									<option value="3BHK" <?php if($getProperty['bhk'] == "3BHK") { ?>selected<?php } ?>>3BHK</option> 
									<option value="4BHK" <?php if($getProperty['bhk'] == "4BHK") { ?>selected<?php } ?>>4BHK</option> 
								</select>
							</div>
							<?php } ?>
							<?php if($getProperty['type_id'] == 1 || $getProperty['type_id'] == 2) { ?>
							<div class="col-lg-6 form-group">
							    <p style="text-align:left;">Select Bathroom *</p>
								<select name="bathroom" id="bathroom" class="common-input form-control">
							        <option value="">Select Bathroom *</option>
									<option value="1" <?php if($getProperty['bathroom'] == "1") { ?>selected<?php } ?>>1</option>
									<option value="2" <?php if($getProperty['bathroom'] == "2") { ?>selected<?php } ?>>2</option>
									<option value="3" <?php if($getProperty['bathroom'] == "3") { ?>selected<?php } ?>>3</option> 
								</select>
							</div>
							<?php } ?>
							<?php if($getProperty['type_id'] == 1) { ?>
							<div class="col-lg-6 form-group">
							    <p style="text-align:left;">Select Balcony *</p>
								<select name="balcony" id="balcony" class="common-input form-control">
							        <option value="">Select Balcony *</option>
									<option value="1" <?php if($getProperty['balcony'] == "1") { ?>selected<?php } ?>>1</option>
									<option value="2" <?php if($getProperty['balcony'] == "2") { ?>selected<?php } ?>>2</option>
									<option value="3BHK" <?php if($getProperty['balcony'] == "3") { ?>selected<?php } ?>>3</option> 
								</select>
							</div>
							<?php } ?>
							<?php if($getProperty['type_id'] == 1 || $getProperty['type_id'] == 2) { ?>
							<div class="col-lg-6 form-group" id="div_fur">
							    <p style="text-align:left;">Select Furnishing Status *</p>
								<select name="furnishing_type" id="furnishing_type" class="common-input form-control">
							        <option value="">Select Furnishing Status *</option>
									<option value="Fully Furnished" <?php if($getProperty['furnishing_type'] == "Fully Furnished") { ?>selected<?php } ?>>Fully Furnished</option>
									<option value="Semi Furnished" <?php if($getProperty['furnishing_type'] == "Semi Furnished") { ?>selected<?php } ?>>Semi Furnished</option>
									<option value="Un Furnished" <?php if($getProperty['furnishing_type'] == "Un Furnished") { ?>selected<?php } ?>>Un Furnished</option>
									<option value="Bareshell" <?php if($getProperty['furnishing_type'] == "Bareshell") { ?>selected<?php } ?>>Bareshell</option>
								</select>
							</div>
							<?php } ?>
							<?php if($getProperty['type_id'] == 3) { ?>
							<div class="col-lg-6 form-group">
							    <p style="text-align:left;">Select Open Sides *</p>
								<select name="open_sides" id="open_sides" class="common-input form-control">
							        <option value="">Select Open Sides *</option>
									<option value="1" <?php if($getProperty['open_sides'] == "1") { ?>selected<?php } ?>>1</option>
									<option value="2" <?php if($getProperty['open_sides'] == "2") { ?>selected<?php } ?>>2</option>
									<option value="3" <?php if($getProperty['open_sides'] == "3") { ?>selected<?php } ?>>3</option>
                                    <option value="4" <?php if($getProperty['open_sides'] == "4") { ?>selected<?php } ?>>4</option>
                                </select>
							</div>
							<?php } ?>
							<div class="col-lg-6 form-group">
							    <p style="text-align:left;">Price *</p>
								<input name="price" id="price" placeholder="Enter Price *" class="common-input form-control" type="text" value="<?php echo stripslashes($getProperty['price']);?>">
							</div>
							<div class="col-lg-12 form-group">
							    <p style="text-align:left;">Property Description *</p>
								<textarea name="description" id="description" placeholder="Enter Property Description *" class="common-input form-control" style="height:200px;"><?php echo stripslashes($getProperty['description']);?></textarea>
							</div>
							<div class="col-lg-12 form-group">
							    <p style="text-align:left;">Change Cover Photo *</p>
								<input name="pic" id="pic" type="file">
								<p><img class="" alt="" src="uploads/property/<?php echo stripslashes($getProperty['cover_photo']);?>" style="width:200px; height:200px;"></p></p>
							</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</button>&nbsp;&nbsp;<a class="btn btn-colored btn-theme-colored2 btn-lg mt-0" href="my-property.php">Back</a>
							</div>
						</div>
						<input type="hidden" name="send" id="send" value="1" />
						</form>	
					</div>
				</div>
			</div>	
		</section>
		<!-- content area end -->

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