<?php

include('../config/constant.php');
include('../includes/header.php');


session_start();
$connection=mysqli_connect("localhost","best-export","Best@123#","best-export");
if(isset($_POST['login']))
{
    $id = $_POST['id'];
  $email = $_POST['email'];
    $password = $_POST['password'];
    $sql = "SELECT * from register where email='".$email."'and password='".$password."'";
    $query = mysqli_query($connection, $sql);

$row = mysqli_num_rows($query);
    if($row == 1){
      echo "login successful";?>
    	 <script >
 alert('login successfully!');
	 document.location.href = "dashboard.php"
   
    </script><?php 
    }else{
      echo "login failed";
      header('location:register.php');
    }


}
?>
<!-- Begin Page Content-->
<body class="bg-gradient-primary">

    <div class="container">

        <!-- Outer Row -->
        <div class="row justify-content-center">

         <div class="col-xl-6 col-lg-6 col-md-6">

              <div class="card o-hidden border-0 shadow-lg my-5">
                  <div class="card-body p-0">
                        <!-- Nested Row within Card Body -->
                        <div class="row">
                        <!--<div class="col-lg-6 d-none d-lg-block bg-login-image"></div>-->
                           <div class="col-lg-12">
                            <div class="p-5">
                                    <div class="text-center">
                                        <h1 class="h4 text-gray-900 mb-4">Login Here!</h1>
                                    </div>
                                   
                                    <form  method="POST" action="index.php">
                                        <div class="form-group">
                                            <input type="email" class="form-control form-control-user" name="email"
                                                id="email" placeholder="Enter email address">
                                        </div>
                                        <div class="form-group">
                                            <input type="password" class="form-control form-control-user" name="password"
                                                id="password" placeholder="Enter Password">
                                        </div>
                                     
                                       <!-- <a href="index.php" class="btn btn-primary btn-user btn-block">
                                            Login
                                        </a>-->
                                        <div class="form-group"><button class="btn btn-primary submit-btn btn-block" type="submit" name="login">Login</button></div>
                                        
                                    </form>
                                   
                                   
                                </div>
                            </div>
                        <!--</div>
                    </div>-->
                </div>

           
            
            
            
  <!-- Button trigger modal -->

  <div class="card shadow mb-4">
    <div class="card-header py-3">
      <h6 class="m-0 font weight-bold text-primary">

        <div class="modal fade" id="registerModal" tabindex="1" role="dialog" aria-labelledby="examplemodalLabel" aria-hidden="true">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Register</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <div class="modal-body">
                <form action="../register/add_register.php" id="addRegisterForm" method="POST" enctype="multipart/form-data">
                  <div class="modal-body">
                    <div class="form-group">

                      <input type="hidden" name="id" id="id" class="form-control" placeholder="Enter Id">
                    </div>
                    <div class="form-group">
                      <label>Username</label>
                      <input type="text" name="username" id="username" class="form-control" placeholder="Enter  Username">
                    </div>
                    <div class="form-group">
                      <label>Email</label>
                      <input type="email" name="email" id="email" class="form-control" placeholder="Enter  Email">
                    </div>
                    <div class="form-group">
                      <label>Password.</label></label>
                      <input type="password" name="password" id="password" class="form-control" placeholder="Enter Password">
                    </div>
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" name="registerbtn" class="registerbtn btn-primary btn" onclick="saveRegister();">Save</button>
                  </div>
                </div></form>
              </div>
            </div>
          </div>
          <div class="card-body">
            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#registerModal"style="margin-left:150px;">
        Create an account
        </button>
       
           </div>
          </div></div>
          <!-- End of Main Content -->

        </div>
         </div>

    </div>
    
  
  <!-- Button trigger modal -->


          <!-- End of Main Content -->
          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.3.0/jquery.form.min.js" ></script>
         
          <script type="text/javascript">
           

            function saveRegister(){
              var options = {
                beforeSend: function () {
              //anything before sending request eg loader etc....
            },
            success: function (res) {
              // once receive response from backend
              //parese json
              var json = $.parseJSON(res);
              if(json.status=='success'){
                alert(json.message);
                $('#registerModal').modal('hide');
                $('#id').val('');
                $('#username').val('');
                $('#email').val('');
                $('#password').val('');
                getAllRegister();
              }
              else{
                alert(json.message);
              }
              
            },
            error: function (response) {

            }        
          };
          $('#addRegisterForm').ajaxForm(options);
        }
       </script>
    <!-- Bootstrap core JavaScript-->
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Core plugin JavaScript-->
    <script src="vendor/jquery-easing/jquery.easing.min.js"></script>

    <!-- Custom scripts for all pages-->
    <script src="js/sb-admin-2.min.js"></script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.3.0/jquery.form.min.js" ></script>

          <?php
          include('../includes/scripts.php');
          
          ?>