<?php
    date_default_timezone_set('Asia/Kolkata');
    $page = "v_login" ;
    include 'head.php';
    session_start();
 //vendor login_master

    if(isset($_POST['forgot'])){
        $username = $config->real_escape_string($_POST['username']);
        $_SESSION['tmp_name'] = $username;
        $sql = "SELECT * FROM master_login WHERE Master_Username = '$username' AND Master_Type = 2 ";
        $res = $config->query($sql);
        if($res->num_rows > 0){
            $row = $res->fetch_assoc();
            $user_id = $row['Master_id'];
            $user_name = $row['Master_Name'];
            $reset_link = password_hash($username,PASSWORD_BCRYPT);
            $req_date = date('Y-m-d H:i:s');
            $exp_date = date('Y-m-d H:i:s',strtotime('+ 1hour'));
            $sql = "INSERT INTO user_reset(user_id, reset_link, req_date, exp_date, status) VALUES('$user_id','$reset_link', '$req_date', '$exp_date', 1) ;" ;
            if($config->query($sql)){
                $res_sql = $config->query("UPDATE master_login SET reset_link = '$reset_link', reset_status = 1 WHERE Master_id = '$user_id' ");
                $to = $username;
                $subject = "Password reset at Addmyentry.com ";
                $headers = "MIME-Version: 1.0" . "\r\n";
            	$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
            	// More headers
            	$headers .= "From: admin@addmyentry.com \r\n";
            	include 'email/direct-mail.php' ;
                // include 'email/simple.php' ;
            	if(mail($to,$subject,$txt,$headers)){
            	    $_SESSION['status'] = "Reset link send to your email" ;
            	    $_SESSION['alert'] = "success" ;
            	}else{
            	    $_SESSION['status'] = "Failed" ;
            	    $_SESSION['alert'] = "error" ;
            	}
            }
        }else{
            $_SESSION['err'] = "Invalid username" ;
        }
    }


?>
	<body class="account-page">

		<!-- Main Wrapper -->
		<div class="main-wrapper">
		
			<!-- Header -->
			<?php include('./header.php') ?>
			<!-- /Header -->
			
			<!-- Page Content -->
			<div class="content">
				<div class="container-fluid">
					
					<div class="row">
						<div class="col-md-8 offset-md-2">
							
							<!-- Login Tab Content -->
							<div class="account-content">
								<div class="row align-items-center justify-content-center">
									<div class="col-md-7 col-lg-6 login-left">
										<img src="assets/img/login-banner.png" class="img-fluid" alt="Doccure Login">	
									</div>
										<div class="col-md-12 col-lg-6 login-right">
										<div class="login-header">
											<h3>Reset Your Password</h3>
											<p class="small text-muted">Enter your <span>registered email</span> to get a password reset link</p>
										</div>
										
										<!-- Forgot Password Form -->
										<form method="post">
											<div class="form-group form-focus">
												<input type="email" class="form-control floating" name="username" value="<?php	if(isset($_SESSION['tmp_name'])){ echo $_SESSION['tmp_name'] ; unset($_SESSION['tmp_name']); } ?>" >
												<label class="focus-label">Email</label>
											</div>
											<div class="text-right">
												<a class="forgot-link" href="vendor_login.php">Remember your password?</a>
											</div>
											<button class="btn btn-primary btn-block btn-lg login-btn" type="submit" name="forgot">Reset Link</button>
											<?php	if(isset($_SESSION['err']) && !empty($_SESSION['err'])){ ?>
											<span class="text-danger"><?= $_SESSION['err'] ?></span>
											<?php unset($_SESSION['err']); } ?>
										</form>
										<!-- /Forgot Password Form -->
										
									</div>
								</div>
							</div>
							<!-- /Login Tab Content -->
								
						</div>
					</div>

				</div>

			</div>		
			<!-- /Page Content -->
   
			<!-- Footer -->
			<?php include('./footer.php') ?>
			<!-- /Footer -->
		   
		</div>
		<!-- /Main Wrapper -->
	    <!-- custom style -->
	    <style>
	        .show-icon{
                position: absolute;
                cursor: pointer;
                right: 10px;
                top: 50%;
                transform: translateY(-50%);
                font-size: 13px;
                color: #9d9b9b;
            }
	    </style>
	    <!-- custom script -->
        <script>
            const password = document.getElementById('password');
            const icon = document.getElementById('pass-icon');
            function showhide(){
                if(password.type === "password"){
                    password.setAttribute("type","text");
                    icon.setAttribute("class","fas fa-eye show-icon");
                }else{
                    password.setAttribute("type","password");
                    icon.setAttribute("class","fas fa-eye-slash show-icon");
                }
            }
            
        </script>
        <script>
    		<?php if(isset($_SESSION['status']) && !empty($_SESSION['status'])) { ?>
    		    swal({
                  title: "<?= $_SESSION['status'] ?>",
                  text: "",
                  icon: "<?= $_SESSION['alert'] ?>",
                  button: {
                    className: "agree",
                    },
                });
                $('.agree').on('click', function() {
                    window.location.href="vendor_login.php";
                });
            <?php unset($_SESSION['status']) ; } ?>
        </script>
		<!-- jQuery -->
		<script src="assets/js/jquery.min.js"></script>
		
		<!-- Bootstrap Core JS -->
		<script src="assets/js/popper.min.js"></script>
		<script src="assets/js/bootstrap.min.js"></script>
		
		<!-- Custom JS -->
		<script src="assets/js/script.js"></script>
		
	</body>

<!-- doccure/login.html  30 Nov 2019 04:12:20 GMT -->
</html>