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

<?php
if(isset($_POST['send']))
{	
	//UPLOAD IMAGE
	if($_FILES['pic']['name']!="")
	{	
		unlink("uploads/broker/".$_POST['hid_pic']);
		
		$photopath = pathinfo($_FILES['pic']['name']);
		$extension = $photopath['extension'];
		$source = $_FILES['pic']['tmp_name'];
		$pic = time().".".$extension;
		$destination = "uploads/broker/".$pic;
		move_uploaded_file($source,$destination);
	}
	else
	{
		$pic = $_POST['hid_pic'];
	}
	
	$sql_update = "UPDATE `pd_brokers` SET `fname` = '".addslashes($_POST['fname'])."',`lname` = '".addslashes($_POST['lname'])."',`email` = '".addslashes($_POST['bemail'])."',`phone` = '".addslashes($_POST['phone'])."',`address` = '".addslashes($_POST['address'])."',`area` = '".addslashes($_POST['area'])."',`zip` = '".addslashes($_POST['zip'])."',`profile_photo` = '".$pic."' WHERE `user_id` = '".$_SESSION['USER_ID']."'";
	$exe_update = mysqli_query($connection,$sql_update);
	
	$_SESSION['ep_succ_msg'] = 'Profile updated successfully.';
	header("location: edit-profile.php");
	exit;
}

$getUser = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `pd_brokers` WHERE `user_id` = '".$_SESSION['USER_ID']."'"));
?>

		<script language="javascript" type="text/javascript">
		function echeck(str) 
		{
			var at="@"
			var dot="."
			var lat=str.indexOf(at)
			var lstr=str.length
			var ldot=str.indexOf(dot)
			if (str.indexOf(at)==-1){
				alert("Invalid email address");
			   return false;
			}
	
	
			if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
				alert("Invalid email address");
			   return false;
			}

			if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
				alert("Invalid email address");
				return false;
			}

			if (str.indexOf(at,(lat+1))!=-1){
				alert("Invalid email address");
				return false;
			}

			if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
				alert("Invalid email address");
				return false;
			 }

			if (str.indexOf(dot,(lat+2))==-1){
				alert("Invalid email address");
				return false;
			}

			if (str.indexOf(" ")!=-1){
				alert("Invalid email address");
				return false;
			}
			return true;					
		}
		
		function form_validation()
		{
			if(document.getElementById('fname').value == "")
			{
				alert("Please enter first name");
				document.getElementById('fname').focus();
				return false;
			}
			
			if(document.getElementById('lname').value == "")
			{
				alert("Please enter last name");
				document.getElementById('lname').focus();
				return false;
			}
			
			if(document.getElementById('phone').value == "")
			{
				alert("Please enter phone no.");
				document.getElementById('phone').focus();
				return false;
			}

			if(document.getElementById('bemail').value == "")
			{
				alert("Please enter email address");
				document.getElementById('bemail').focus();
				return false;
			}
		   
			if(echeck(document.getElementById('bemail').value)==false){
				document.getElementById('bemail').focus();
				return false;
			}
			
			if(document.getElementById('address').value == "")
			{
				alert("Please enter address");
				document.getElementById('address').focus();
				return false;
			}
			
			if(document.getElementById('area').value == "")
			{
				alert("Please enter area/location");
				document.getElementById('area').focus();
				return false;
			}
			
			if(document.getElementById('zip').value == "")
			{
				alert("Please enter pincode");
				document.getElementById('zip').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 Profile</h1>
						<?php if(isset($_SESSION['ep_succ_msg'])) { ?>
						<p><img src="images/tick.png" alt="" /> <?php echo $_SESSION['ep_succ_msg'];?></p>
						<?php 
						unset($_SESSION['ep_succ_msg']);
						} 
						?>	
						<form id="" action="" method="post" enctype="multipart/form-data" class="contact-form" onsubmit="return form_validation();">
						<input type="hidden" name="hid_pic" id="hid_pic" value="<?php echo stripslashes($getUser['profile_photo']);?>">
						<div class="row">
							<div class="col-lg-12 form-group">
								<input name="fname" id="fname" placeholder="Enter First Name *" class="common-input form-control" type="text" value="<?php echo stripslashes($getUser['fname']);?>">
							</div>
							<div class="col-lg-12 form-group">
								<input name="lname" id="lname" placeholder="Enter Last Name *" class="common-input form-control" type="text" value="<?php echo stripslashes($getUser['lname']);?>">
							</div>
							<div class="col-lg-12 form-group">
								<input name="phone" id="phone" placeholder="Enter Phone No. *" class="common-input form-control" type="text" maxlength="10" value="<?php echo stripslashes($getUser['phone']);?>">
							</div>
							<div class="col-lg-12 form-group">
								<input name="bemail" id="bemail" placeholder="Enter Email Address *" class="common-input form-control" type="text" value="<?php echo stripslashes($getUser['email']);?>">
							</div>
							<div class="col-lg-12 form-group">
								<textarea name="address" id="address" class="common-input form-control" placeholder="Enter Address *"><?php echo stripslashes($getUser['address']);?></textarea>
							</div>
							<div class="col-lg-12 form-group">
								<input name="area" id="area" placeholder="Enter Area/Location *" class="common-input form-control" type="text" value="<?php echo stripslashes($getUser['area']);?>">
							</div>
							<div class="col-lg-12 form-group">
								<input name="zip" id="zip" placeholder="Enter Pincode *" class="common-input form-control" type="text" value="<?php echo stripslashes($getUser['zip']);?>">
							</div>
							<div class="col-lg-12 form-group">
								<input name="pic" id="pic" type="file"><br><img src="uploads/broker/<?php echo stripslashes($getUser['profile_photo']);?>" alt="" style="height:150px; width:150px;">
							</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>
							</div>
						</div>
						<input type="hidden" name="send" id="send" value="1" />
						</form>	
					</div>
				</div>
			</div>	
		</section>
		<!-- content area end -->

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