<?php session_start();
 include("header.php");
 require_once('dbcon.php');
?>
<head>
<!-- Bootstrap -->
    <link href="vendors/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
	</head>
	
<div class="container">
	 <div id="loginbox" style="display:block;margin-top:200px;" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">                    
            <div class="panel panel-info" >
                    <div class="panel-heading" style="background-color:#ea7d09;color:#fff;font-weight:bold;" >
                        <div class="panel-title">USER SIGN IN</div>
                        <div style="float:right;font-size: 80%; position:relative;margin-top:-20px;font-weight:bold;"><a href="resetpword.php">Forgot password?</a></div>
                    </div>     

                    <div style="padding-top:30px" class="panel-body" >

                        <div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>
                            
                         <form  class="form-horizontal" role="form" method="post" action="">
                                    
                            <div style="margin-bottom: 25px" class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                                        <input id="username" type="text" class="form-control" name="usernm" placeholder="Username" required>                                        
                            </div>
                                
                            <div style="margin-bottom: 25px" class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                                        <input  id="password" type="password" class="form-control" name="pword" placeholder="Password" required>
                                    </div>
                                    

                                
                            <div class="input-group">
                                      <div class="checkbox">
                                        <label>
                                          <input id="login-remember" type="checkbox" name="rememberme" value="1"> Remember me
                                        </label>
                                      </div>
                                    </div>


                                <div style="margin-top:10px;margin-bottom:20px;" class="form-group">
                                    <!-- Button -->

                                    <div class="col-sm-12 controls">
                                   <input type="submit" name="submit1"  value="Login" id="btn-login"  class="btn btn-success pull-left"  />
									<button id="lft" type="button" class="btn btn-error pull-right"><i class="icon-hand-right"></i><a href="index.php"> &nbsp; <b><< Back </b></a></button>
                  

                                    </div>
                                </div>
								
<hr/>
                                <div class="form-group" style="margin-top:10px;margin-bottom:5px;">
                                    <div class="col-md-12 control">
                                         <a style="background-color:#ea7d09;color:#fff;font-weight:bold;"  rel="tooltip" title="Admin Login"  href="/admin"  class="btn btn-warning btn-sm pull-left"> <b>Admin Login</b> </a>
                                        <!--div style="border-top: 1px solid#888; padding-top:15px;font-size:85%" >    
                                        <a style="margin-left:150px;background-color:#ea7d09;color:black;font-weight:bold;" class="btn btn-" rel="tooltip" title="Admin Login"  href="signup.php"  >  &nbsp;&nbsp;New User? Register Now! </a>
										
                                        </div-->
                                    </div>
                                </div>    
                            </form>     
	


                        </div>                     
                    </div>  
        </div>
		
								<?php




	
    if(isset($_POST['submit1'])){
			
		 $usr = $_POST['usernm'];   
		 $password = $_POST['pword']; 
		 
		$host = 'localhost';
        $db   = 'ki223469_temp';
        $user = 'ki223469_temp';
        $pass = 'm0bi.2018$';
        $charset = 'utf8mb4';
        
        $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
        $options = [
            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES   => false,
        ];
        $pdo = new PDO($dsn, $user, $pass, $options);
		
		//create a select query
		$stmt = $pdo->prepare('SELECT * FROM airtime_customer WHERE username = :email and user_type="normal"');
		//start a try
		try{
		    
		    $stmt->execute(['email' => $usr]);
		    //get the selected array
		    $user=$stmt->fetch();
		    if(count($user)>0){
		        
		        //verify the password
		        	if(password_verify($password, $user['password'])){
		        	   
		        	   $_SESSION['id'] = $user['customer_id'];
		        	   $_SESSION['phone'] = $user['phone'];
		        	   $_SESSION['otp_status'] = 0; 
		        	   if( $user['status'] == "Active"){
    					   $id = $user['customer_id'];
    					   //insert logs
    						$query2 = mysqli_query($con,"insert into airtime_logs(customer_id,login_date,login_time,user_actions) values('$id',Now(),Now(),'Logged into the System.') ") or die (mysqli_error());
    						if($query2){
    							// save user log id
    							$logid = mysqli_insert_id();
    							$_SESSION['log_id'] = $logid; 
    							
    							
    							?>
    								<script>
    									window.location="homepage.php";
    								</script>
    							<?php
    						}
    						
        				  }else{
        					  ?>
        						<script>
        						   alert("Sorry! Your Account has been blocked. Please contact your Account Admin");
        							window.location="login.php";
        							</script>
        						<?php
        				  }
    				    
		        	}
		        	
		    }
		    else{
		        ?>
				<script>
					alert("Error! Invalid Username or Password. Please try again."); 
				</script>
			<?php
		    }
		    
		}
		catch(PDOException $e){
		    ?>
				<script>
					alert("Error!.".$e->getMessage().". Kindly contact admin"); 
				</script>
			<?php 
		}
		 
	}

?>
</div>

   <!--?php include("footerlinks.php"); ?-->
	 
	  <script src="vendors/jquery/dist/jquery.min.js"></script>
    <!-- Bootstrap -->
    <script src="vendors/bootstrap/dist/js/bootstrap.min.js"></script>
	

