<?php session_start(); ob_start(); ?><link href="assets/css/lib/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="style.css">
<title> CRM Login</title>
<?php  include("header.php");  ?>
<?php 



   $strError= array();

   if (isset($_POST['submit_btn'])) {

    
        
          $username = mysqli_real_escape_string($connection, $_POST['username']);
		  
          if (empty($username)) {
            $strError[]= "Please enter username...!!";
          }
          $password = mysqli_real_escape_string($connection, $_POST['password']);
		  
          if (empty($password)) {
             $strError[]= "Please enter passowrd...!!";
          }

		  
	      
	
		  
		  
          if (empty($strError)) {
			  
           
		    
            $sqlSelect = "SELECT * FROM users WHERE email = '".$username."'" ;

            $result = mysqli_query($connection,$sqlSelect);
            $count = mysqli_num_rows($result);

            while ($list = mysqli_fetch_assoc($result)) {
             
              $pass=$list['password'];
               $_SESSION['LOGINID'] = $list['id'];
              $_SESSION['LOGINNM'] = $list['email'];
            }
            
           

if (password_verify($password, $pass)) {
    
    
   
     
    if($count > 0){

              $ip_address = $_SERVER['REMOTE_ADDR'];
              $browser_name = explode(" ",$_SERVER['HTTP_USER_AGENT']) ;
              $mybrowser = $browser_name[6];

                $lg_id=$_SESSION['LOGINID'];      


               $insertQry = "INSERT INTO `login_log` (`id`, `user_id`, `ip_address`, `browser_name`, `login_time`) VALUES (NULL, '$lg_id', '$ip_address', '$mybrowser', CURRENT_TIMESTAMP);";	
               
             /* echo $sqlUpdate;
              exit;*/

              $sqlUpdated = mysqli_query($connection, $insertQry);  

              date_default_timezone_set('Asia/Dhaka');
$date_c = date('Y-m-d H:i:s', time());
$sqlUpdate = "INSERT INTO `activity_log` (`id`, `user`, `activity`, `time`) VALUES (NULL, '$lg_id', 'user logged in', '$date_c')";
$sqlUpdate = mysqli_query($connection, $sqlUpdate); 			  
                header("Location:dashboard.php?flag=success");
                
              
            }

} 


            
            else{
              $strError[]= "Invalid Username Or Password !! ";
            }

          }
          

  }



?>
<body class="bg-default">
<div class="container">
    <div class="login_main clearfix">
        <div class="login_wrap clearfix">
            <?php   
                // Setting the value of logout variable
                if(isset($_GET['logout'])){
                         $logout = $_GET['logout'] ;
                }else{
                  $logout = "";
                }
                

                if( $logout == "success"){ ?>

            <div class="alert alert-success">
                <p>You are logged out!</p>
                <?php header("refresh:2 , url=index.php"); ?>
            </div>
            <?php } ?>

            <?php 
                //Setting the value of the logout variable
                if(isset($_GET['admin'])){
                       $logout = $_GET['admin'] ;
                }else{
                        $logout = "";
                }
                

                if( $logout == "updated"){ ?>

            <div class="alert alert-success">
                <p>Admin credentials updated successfully!</p>
                <?php header("refresh:2 , url=index.php"); ?>
            </div>
            <?php } ?>
            <div class="col-md-6 col-md-offset-3">
                   <h3 class="panel-title"> <center>
						 <img src="assets/images/logo_wd.png" width="30%" />
						 </center>
						 </h3>
                <div class="panel panel-primary">
                    <div class="panel-heading">
					     
                        <h3 class="panel-title">Management Login</h3>
                    </div>
                    <div class="panel-body">
                        <?php for($i=0; $i< count($strError) ; $i++) { ?>


                        <div class="alert alert-danger">
                            <?php echo $strError[$i].nl2br("\n"); ?>
                        </div>

                        <?php } ?>

                        <form action="<?php echo $_SERVER['PHP_SELF'];  ?>" method="post">
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-md-3">
                                        <label for="username"><p>Email:</p></label>
                                    </div>
                                    <div class="col-md-9">
                                        <input type="text" class="form-control" placeholder="Enter Username" name="username">
                                    </div>
                                </div>


                            </div>
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-md-3">
                                        <label for="password"><p>Password:</p></label>
                                    </div>
                                    <div class="col-md-9">
                                        <input type="password" class="form-control" placeholder="Enter Password" name="password">
                                    </div>
                                </div>


                            </div>
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-md-3">

                                    </div>
                                    <div class="col-md-9">
                                        <button type="submit" name="submit_btn" class="btn btn-primary">Submit</button>
                                    </div>
                                </div>


                            </div>


                        </form>
                    </div>
                </div>
               



            </div>

        </div>
    </div>
</div>
</body>