       <?php
            include "config.php";
            session_start();
            if(isset($_SESSION["admin"]['email'])){
  
              header("location:home.php");
                
             }
            
            if(isset($_POST['login'])){
        //    include "config.php";
            if(empty($_POST['email']) || empty($_POST['password'])){
            echo '<div class = "alert alert-danger"> All Field must be entered.</div>';
            die();
            }else{
            $email = mysqli_real_escape_string($conn,$_POST['email']);
            $password =  ($_POST['password']);
            
             $sql = "SELECT * FROM admin WHERE email = '{$email}' AND password = '{$password}'";
            $result = mysqli_query($conn, $sql) or die("Query Failed");
            if(mysqli_num_rows($result) > 0){
                while($row = mysqli_fetch_assoc($result)){

                    $_SESSION["admin"] = $row;
                    
                    // $_SESSION["email"] = $row['email'];
                    // $_SESSION["admin_id"] = $row['admin_id'];
                    // $_SESSION["username"] = $row['username']; 
                    // $_SESSION["name"] = $row['name'];
                    // $_SESSION['status'] = "Login Successfully";
                    // $_SESSION['status_code'] = "success";
                

            
                //   echo  $email = $_SESSION['admin']['email'];
                //   echo  $admin_id = $_SESSION['admin']['admin_id'];
                //   echo  $username = $_SESSION['admin']['username']; 
                //  echo "<script>window.location='http://localhost/studied/admin/index.php'</script>";
                header("location: index.php");
                }
            }else{
                $_SESSION['status'] = "Username and Password are not matched.";
                $_SESSION['status_code'] = "error";
                // echo '<div class = "alert alert-danger"> Username and Password are not matched.</div>';
            }
            }
            }
        ?>
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <title>Login</title>

    <!-- Bootstrap CSS CDN -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">


    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">


    <link rel="stylesheet" href="style.css">
    <!-- <script src="https://kit.fontawesome.com/a076d05399.js"></script> -->

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
     <script>
           $(document).ready(function(){
               $(".login").click(function(){
                   var email = $("#email").val();
                   var mailformat = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;    // Mail Formate for email validation
                   var pass = $("#pass").val();

                    if(mailformat.test(email) == false){
                    // alert("Enter your valid email id");
                    $(".emailError").html("Enter your valid email id");
                    // $("input:text").val("Enter your valid email id");
                    $("#email").focus();
                    return false;
                    }  
                    else if(pass == ""){
                        $(".Error").html("");
                        $(".passError").html("Please Enter your password");
                        $("#pass").focus();
                        return false;
                    }
                    else{
                        $(".Error").html("");
                       // alert("Success")
                    }
               });
           })
    </script> 

            <script>
                 function myFunction() {
                      if(contactInput.length > 10){
                          return false;
                      }
                    
                   }
            </script>

    <script>
       var otpemailInput = "";
        $(document).ready(function(){
           $("#SendOTP").click(function(){
            otpemailInput = $("#otpemailInput").val();
              var mailformat = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;    // Mail Formate for email validation
              if(mailformat.test(otpemailInput) == false){
                  $(".contactError").html("Enter your valid email id");
                  $("#otpemailInput").focus();
                   return false;
                   }
              else{
                $.ajax({
                        url: 'email-otp.php',
                        type: 'POST',
                        data: { email: otpemailInput},
                        success: function(response){
                            
                        },
                        error:function(err){
                          console.log("Error : ",err)
                        }
                     });
                  $(".Error").html("");
                  $("#forgotemail").hide();
                  $("#SendOTP").hide();
                  $("#otpNumber").show();
                  $("#varifyOTP").show();
                  }
           });

        //    $("#varifyOTP").click(function(){
        //     var otpInput = $("#otpInput").val();

        //      if(otpInput.length != "4"){
        //             $(".otpError").html("Please Enter Valide OTP Number");
        //             $("#otpInput").focus();
        //             return false;
        //         }  
        //         else{
        //             $(".Error").html("");
        //             $("#otpNumber").hide();
        //             $("#varifyOTP").hide();
        //             $("#changePass").show();
        //             $("#changePassorwd").show();
        //          }
        //        });

            //     $("#changePassorwd").click(function(){
            //      var newPassord = $("#newPassord").val();
            //      var Confirm_newPassword = $("#Confirm_newPassword").val();
            //      if(newPassord !== Confirm_newPassword){
            //         $(".conformPassError").html("Conform Password is not mached");
            //         $("#Confirm_newPassword").focus();
            //         return false; 
                     
            //     }else{
            //         $(".Error").html("");
            //         alert("Success");
            //     }
            // });


            $("#varifyOTP").click(function(){
            var otpInput = $("#otpInput").val();
            var newPassord = $('#newPassord').val();
            var Confirm_newPassword = $('#Confirm_newPassword').val();

            if(otpInput.length != "6"){
              // alert("Please Enter Valide OTP Number");
              $(".otpError").html("Please Enter Valide OTP Number");
              $("#otpInput").focus();
              return false;
            }  
            
            if(newPassord != Confirm_newPassword || newPassord == ""){
              alert("Password & confirm password does not match");
            }
            else{

             $(".Error").html("");
                                   
                    // console.log(otpemailInput);
                    // console.log(otpInput);
                    // console.log(newPassord);

                    $.ajax({
                        url: 'email-otp.php',
                        type: 'POST',
                        data: {otpemail: otpemailInput, otp:otpInput,newPassord:newPassord},
                        success: function(data){
                          console.log(data);
                         if(data == "Success"){
                          location.reload();
                         }                           
                        },
                        error:function(err){
                          console.log("Error : ",err)
                        }
                     });

                    
                    //alert("verifyOTP");
            }
                
            });

        });
    </script>

    <!-- <style>
    #changePass{
        display: none;

    }
    #changePassorwd{
        display: none;
    }
    </style> -->

</head>

<body>

    <!-----------------------Login-------------------------------------->

    <div class="center logo_image_form_login">
    <!-- <img src="../images/logo/logo_hasan.jpeg"> -->
        <div class="container" id="loginForm">

            <!-- <label for="" class="close-btn fas fa-times" title="close"></label> -->

            <div class="text">Login <span id="heading">Form</span></div>

            <form action="<?php $_SERVER['PHP_SELF']; ?>" method ="POST">

                <div class="data">
                    <label>Email or Phone</label>
                    <input type="text" name="email"  id="email" class="form-control" placeholder="Enter Email" >
                    <span class="emailError Error" style="color: red;"></span> 
                </div>

                <div class="data">
                    <label id="passLable">Password</label>
                    <input type="password" name="password" id="pass" class="form-control" placeholder="Password">
                      <span class="passError Error" style="color: red;"></span> 
                </div>

                <div class="forgot-pass"><a href="#" data-toggle="modal" data-target="#myModal">Forgot Password?</a></div>

                <div class="login_btn">
                    <button type="submit" class="login" name="login">login</button>
                </div>

            </form>

         </div>
        
     </div>
     <!-----------------------Login Form- End------------------------------------->

     <!-----------------------Forgot Form Start------------------------------------->
           
                <div class="container">
                
                    <!-- The Modal -->
                    <div class="modal" id="myModal">
                    <div class="modal-dialog">
                        <div class="modal-content">
                        
                        <!-- Modal Header -->
                        <div class="modal-header">
                            <h4 class="modal-title">Forgot Password</h4>
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                        </div>
                        
                        <!-- Modal body -->
                        <div class="modal-body">
                              
                             <!-- <div class="form-group" id="contactNumber">
                                <label>Contact Number</label>
                                <input type="number" class="form-control" id="contactInput" placeholder="Please Enter Mobile Number" onkeypress="myFunction()">
                                <span class="contactError Error" style="color: red;"></span> 
                            </div> -->
                            <form action="forgot_pass.php" method="POST">

                            <div class="form-group" id="forgotemail">
                                <label>Email Address</label>
                                <input type="email" class="form-control" id="otpemailInput" name="email" placeholder="Please Enter Email">
                                <span class="contactError Error" style="color: red;"></span> 
                            </div>

                            <div class="form-group" id="otpNumber">
                                <label>OTP Number</label>
                                <input type="number" class="form-control" id="otpInput" name="otpInput" placeholder="Please Enter OTP Number">
                                <span class="otpError Error" style="color: red;"></span> 
                            <!-- </div> -->

                            <!-- <div class="form-group" id="changePass"> -->
                                <label>New Password</label>
                                <input type="text" class="form-control" id="newPassord" name="newPassord" placeholder="Please Enter New Password">


                                <label>Confirm New Password</label>
                                <input type="text" class="form-control InputField" name="Confirm_newPassword" id="Confirm_newPassword" placeholder="Please Confirm New Password">
                                <span class="conformPassError AllError" style="color: red;"></span>
                            </div>
                            
                        </div>
                        
                        <!-- Modal footer -->
                        <div class="modal-footer">
                            <button type="button" class="btn btn-success" id="SendOTP"  name="sendotp" >Send OTP</button>
                            <button type="button" class="btn btn-success" id="varifyOTP"  name="varifyOTP" >Varify OTP</button>
                            <!-- <button type="button" class="btn btn-success" id="changePassorwd"  name="changePassorwd">Change Password</button> -->
                            <button type="button" class="btn btn-danger" id="closebtn" data-dismiss="modal">Close</button>
                        </div>

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



      <!-----------------------Forgot Form End------------------------------------->
 <script src="js/sweetalert.min.js"></script>    
  <?php 
    if(isset($_SESSION['status']) && $_SESSION['status']!='')
    {
  ?>
  <script>
    swal({
      title: "<?php echo $_SESSION['status']; ?>",
      //text: "You clicked the button!",
      icon: "<?php echo $_SESSION['status_code']; ?>",
      button: "OK, Done",
     });
  </script> 
   <?php 
    unset($_SESSION['status']);
    }
   ?>

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>

</html>