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

// Stop making changes below this line
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'phpmailer/PHPMailer-master/src/Exception.php';
require 'phpmailer/PHPMailer-master/src/PHPMailer.php';
require 'phpmailer/PHPMailer-master/src/SMTP.php';
?>

<?php
if(isset($_POST['send']))
{	
	$_SESSION['FNAME'] = $_POST['fname'];
    $_SESSION['LNAME'] = $_POST['lname'];
    $_SESSION['EMAIL'] = $_POST['bemail'];
    $_SESSION['PHONE'] = $_POST['phone'];
    $_SESSION['ADDRESS'] = $_POST['address'];
    $_SESSION['AREA'] = $_POST['area'];
    $_SESSION['ZIP'] = $_POST['zip'];
    
	if($_POST['act_answer'] == $_POST['answer'])
	{
    	$num_registered = mysqli_num_rows(mysqli_query($connection,"SELECT * FROM `pd_brokers` WHERE (`email` = '".$_POST['bemail']."' OR `phone` = '".$_POST['phone']."')"));
    	if($num_registered>0)
    	{
    	    $_SESSION['msg_fail3'] = 'Broker already exists.';
            header("location: broker-signup.php");
            exit;
    	}
    	else
    	{
        	//UPLOAD PROFILE PHOTO
        	if($_FILES['pic']['name']!="")
        	{	
        		$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 = '';
        	}
        	
        	//UPLOAD AADHAR CAED FRONTSIDE
        	if($_FILES['pic1']['name']!="")
        	{	
        		$photopath = pathinfo($_FILES['pic1']['name']);
        		$extension = $photopath['extension'];
        		$source = $_FILES['pic1']['tmp_name'];
        		$pic1 = md5(time()).".".$extension;
        		$destination = "uploads/broker/".$pic1;
        		move_uploaded_file($source,$destination);
        	}
        	else
        	{
        		$pic1 = '';
        	}
        	
        	//UPLOAD AADHAR CAED BACKSIDE
        	if($_FILES['pic2']['name']!="")
        	{	
        		$photopath = pathinfo($_FILES['pic2']['name']);
        		$extension = $photopath['extension'];
        		$source = $_FILES['pic2']['tmp_name'];
        		$pic2 = rand(9999,99999).".".$extension;
        		$destination = "uploads/broker/".$pic2;
        		move_uploaded_file($source,$destination);
        	}
        	else
        	{
        		$pic2 = '';
        	}
        	
        	function random_strings($length_of_string)
            {
            
                // String of all alphanumeric character
                $str_result = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
            
                // Shuffle the $str_result and returns substring
                // of specified length
                return substr(str_shuffle($str_result), 
                                   0, $length_of_string);
            }
        
            // This function will generate
            // Random string of length 10
            $broker_code =  random_strings(6);
        	
        	$sql_insert = "INSERT INTO `pd_brokers` SET `fname` = '".addslashes($_POST['fname'])."',`lname` = '".addslashes($_POST['lname'])."',`phone` = '".$_POST['phone']."',`email` = '".$_POST['bemail']."',`address` = '".addslashes($_POST['address'])."',`password` = '".md5($_POST['password'])."',`original_password` = '".$_POST['password']."',`broker_code` = '".$broker_code."',`is_active` = 'Yes',`registration_date` = '".date('Y-m-d H:i:s',time())."',`area` = '".addslashes($_POST['area'])."',`zip` = '".addslashes($_POST['zip'])."',`profile_photo` = '".$pic."',`aadhar_front` = '".$pic1."',`aadhar_back` = '".$pic2."'";
        	$exe_insert = mysqli_query($connection,$sql_insert);
    	    $last_id = mysqli_insert_id($connection);
    	    
    	    $_SESSION['USER_ID'] = $last_id;
    	    
    	    //SEND EMAIL TO USER
            $name = 'Properti Dost';
            $emaila = $_POST['bemail'];
        
        	$email_account = "info@prismonline.in";
            $email_password = "~I5er1FU&bR0";
        
        	// Recipient
        	$mail = new PHPMailer;
        	$mail->isSMTP();
        	$mail->SMTPDebug = 0;
	
        	$subject = 'Registration Successful';	
        	$message = '<table border="0" width="100%" cellspacing="2" cellpadding="2">
        				<tr>
        					<td>Dear '.stripslashes($_POST['fname']).' '.stripslashes($_POST['lname']).',</td>
        				</tr>
        				<tr>
        					<td>&nbsp;</td>
        				</tr>
        				<tr>
        					<td>Thanks for your registration. Your code is <b>'.$broker_code.'</b> Please keep this code secret and do not share with others. It is also your login ID. Now you can access your account and add new property.</td>
        				</tr>
        				<tr>
        					<td>&nbsp;</td>
        				</tr>				
        				<tr>
        					<td>Thanks & Regards,<br>Properti Dost Team</td>
        				</tr>
        				</table>';

        	$mail->Host = 'mail.prismonline.in';
        	$mail->Port = 587;
        	$mail->SMTPAuth = true;
        	$mail->SMTPSecure = "tls";
        	$mail->Username = $email_account;
        	$mail->Password = $email_password;
        
        	$mail->setFrom('info@prismonline.in', "Properti Dost");
        	$mail->addReplyTo('info@prismonline.in', "");
        	$mail->addAddress($emaila);
        	$mail->Subject = $subject;
        	$TemplateMessage = $message;
        	$mail->MsgHTML($TemplateMessage);
        	$mail->send();
    	    
    	    unset($_SESSION['FNAME']);
            unset($_SESSION['LNAME']);
            unset($_SESSION['EMAIL']);
            unset($_SESSION['PHONE']);
            unset($_SESSION['ADDRESS']);
            unset($_SESSION['AREA']);
            unset($_SESSION['ZIP']);
           
    		header("location: signup-thankyou.php?user_id=".$last_id);
    	}
    }
    else
    {
        $_SESSION['msg_fail2'] = 'Your answer is incorrect. Please try again!';
        header("location: broker-signup.php");
        exit;
    }
}

$a= rand(0,99);
$b = rand(0,99);
$c = $a+$b;
?>

			<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;
				}
				
				if(document.getElementById('pic1').value == "")
				{
					alert("Please upload aadhar card frontside");
					return false;
				}
				
				if(document.getElementById('pic2').value == "")
				{
					alert("Please upload aadhar card backside");
					return false;
				}
				
				if(document.getElementById('pic').value == "")
				{
					alert("Please upload profile photo");
					return false;
				}
				
				if(document.getElementById('password').value == "")
				{
					alert("Please enter password");
					document.getElementById('password').focus();
					return false;
				}
				
				if(document.getElementById('cpassword').value == "")
				{
					alert("Please enter confirm password");
					document.getElementById('cpassword').focus();
					return false;
				}
				
				if(document.getElementById('password').value != document.getElementById('cpassword').value)
				{
					alert("Password and confirm password both should be same");
					document.getElementById('cpassword').focus();
					return false;
				}
				
				if(document.getElementById('answer').value == "")
				{
					alert("Please enter answer");
					document.getElementById('answer').focus();
					return false;
				}
			}
			</script>

			<!-- page template area start -->
			<section id="about">
				<div class="container mt-10 pb-10 pt-0">
					<div class="row">
					    <div class="col-md-5">
							<div class="">
								<?php require_once("left_panel.php");?>
							</div>
						</div>
						<div class="col-md-7">
							<h1 class="title mt-5 mb-15" style="text-align:left;">Brokerdost <span style="color:#ff8e00;">Porichoi</span></h1>
							<?php if(isset($_SESSION['msg_fail2'])) { ?>
							<p class="text-left"><img src="images/cross.png" alt="" /> <?php echo $_SESSION['msg_fail2'];?></p>
							<?php 
							unset($_SESSION['msg_fail2']);
							} 
							?>
							<?php if(isset($_SESSION['msg_fail3'])) { ?>
							<p class="text-left"><img src="images/cross.png" alt="" /> <?php echo $_SESSION['msg_fail3'];?></p>
							<?php 
							unset($_SESSION['msg_fail3']);
							} 
							?>
							<form id="contact_form" name="contact_form" class="" action="" method="post" enctype="multipart/form-data" onSubmit="return form_validation();">
							<div class="row">
								<div class="col-sm-6">
									<div class="form-group">
										<input name="fname" id="fname" class="form-control" type="text" placeholder="Enter First Name *" value="<?php echo $_SESSION['FNAME'];?>">
									</div>
								</div>
								<div class="col-sm-6">
									<div class="form-group">
										<input name="lname" id="lname" class="form-control" type="text" placeholder="Enter Last Name *" value="<?php echo $_SESSION['LNAME'];?>">
									</div>
								</div>
								<div class="col-sm-6">
									<div class="form-group">
										<input name="phone" id="phone" class="form-control" maxlength="10" type="text" placeholder="Enter Phone No. *" value="<?php echo $_SESSION['PHONE'];?>">
									</div>
								</div>
								<div class="col-sm-6">
									<div class="form-group">
										<input name="bemail" id="bemail" class="form-control" type="text" placeholder="Enter Email Address *" value="<?php echo $_SESSION['EMAIL'];?>">
									</div>
								</div>
							</div>
							<div class="form-group">
								<textarea name="address" id="address" class="form-control required" rows="5" placeholder="Enter Address *"><?php echo $_SESSION['ADDRESS'];?></textarea>
							</div>
							<div class="row">
								<div class="col-sm-6">
									<div class="form-group">
										<input name="area" id="area" class="form-control" type="text" placeholder="Enter Area/Location *" value="<?php echo $_SESSION['AREA'];?>">
									</div>
								</div>
								<div class="col-sm-6">
									<div class="form-group">
										<input name="zip" id="zip" class="form-control" type="text" placeholder="Enter Pincode *" value="<?php echo $_SESSION['ZIP'];?>">
									</div>
								</div>
							</div>
							<div class="row">
								<div class="col-sm-6">
									<div class="form-group">
									    <p>Upload Aadhar Card Frontside *</p>
										<input name="pic1" id="pic1" type="file">
									</div>
								</div>
								<div class="col-sm-6">
									<div class="form-group">
									    <p>Upload Aadhar Card Backside *</p>
										<input name="pic2" id="pic2" type="file">
									</div>
								</div>
							</div>
							<div class="form-group">
								<p>Upload Profile Photo *</p>
								<input name="pic" id="pic" type="file">
							</div>
							<div class="row">
								<div class="col-sm-6">
									<div class="form-group">
										<input name="password" id="password" class="form-control" type="password" placeholder="Enter Password *" value="">
									</div>
								</div>
								<div class="col-sm-6">
									<div class="form-group">
										<input name="cpassword" id="cpassword" class="form-control" type="password" placeholder="Enter Confirm Password *" value="">
									</div>
								</div>
							</div>
							<div class="form-group">
							    <p><?php echo $a;?>+<?php echo $b;?> = ?</p>
								<div class="form-group">
									<input name="answer" id="answer" class="form-control" type="text" placeholder="Enter Answer *">
								</div>
							</div>
							<div class="form-group">
								<input type="hidden" name="send" id="send" value="1" />
								<input type="hidden" name="act_answer" id="act_answer" value="<?php echo $c;?>" />
								<button type="submit" class="btn btn-flat btn-theme-colored border-left-theme-color-2-4px">Register Now</button>
							</div>
							</form>
							<p><a href="broker-login.php" style="color:#e53935">Back To Brokerdost Login</a></p>
						</div>
					</div>
				</div>
			</section>
			<!-- page template area end -->

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