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

    if(isset($_POST['login'])){
        $username = $config->real_escape_string($_POST['username']);
        $password = $config->real_escape_string($_POST['password']);
        $_SESSION['tmp_name'] = $username;
        $_SESSION['tmp_pass'] = $password;
        $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();
            $pass = $row['Master_Password'];
            if(password_verify($password,$pass)){
                $_SESSION['user_id'] = $row['Master_id'];
                $_SESSION['user_name'] = $row['Master_Name'];
                $_SESSION['user_cate'] = "vendor";
                $_SESSION['user_email'] = $row['Master_Username'];
                $_SESSION['user_phone'] = $row['Master_phone'];
                $user_id = $_SESSION['user_id'];
                $v_sql = $config->query("SELECT * FROM v_profile WHERE v_id = '$user_id' ");
                $v_res = $v_sql->fetch_assoc();
                $_SESSION['company'] = $v_res['company'];
                $_SESSION['vendor_image'] = $v_res['vp_image'];

                header('Location:vendor_dashboard.php');
            }else{
                $_SESSION['err'] = "Invalid password" ;
            }
        }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>Login <span></span></h3>
										</div>
										<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="form-group form-focus">
												<input type="password" id="password" class="form-control floating" name="password" value="<?php	if(isset($_SESSION['tmp_pass'])){ echo $_SESSION['tmp_pass'] ; unset($_SESSION['tmp_pass']); } ?>" > 
												<i class="fas fa-eye-slash show-icon" id="pass-icon" onclick="showhide()"></i>
												<label class="focus-label">Password</label>
											</div>
											<div class="text-right">
												<a href="vendor-forgot-password.php" class="forgot-link" >Forgot Password ?</a>
											</div>
											<!--<div class="forgot-link text-right d-block" style="margin-top: 10px;">-->
											<!--    <a href="vendor-forgot-password.php">Forgot Password?</a>-->
											<!--</div>-->
											<button class="btn btn-primary btn-block btn-lg login-btn" type="submit" name="login">Login</button>
											<?php	if(isset($_SESSION['err']) && !empty($_SESSION['err'])){ ?>
											<span class="text-danger"><?= $_SESSION['err'] ?></span>
											<?php unset($_SESSION['err']); } ?>
											<!--<div class="login-or">-->
											<!--	<span class="or-line"></span>-->
											<!--	<span class="span-or">or</span>-->
											<!--</div>-->
											<!--<div class="row form-row social-login">-->
											<!--	<div class="col-6">-->
											<!--		<a href="#" class="btn btn-facebook btn-block"><i class="fab fa-facebook-f mr-1"></i> Login</a>-->
											<!--	</div>-->
											<!--	<div class="col-6">-->
											<!--		<a href="#" class="btn btn-google btn-block"><i class="fab fa-google mr-1"></i> Login</a>-->
											<!--	</div>-->
											<!--</div>-->
											<div class="text-center dont-have">Don’t have an account? <a href="vendor_register.php">Register</a></div>
										</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",
                    },
                });
            <?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>