<?php
error_reporting (E_ALL ^ E_NOTICE);
error_reporting(E_ALL ^ E_WARNING); 
session_start();
include "Nav.php";
include 'Connection.php';
if(isset($_POST['signup'])){
$name=mysqli_real_escape_string($con,$_POST['name']);
$email=mysqli_real_escape_string($con,$_POST['email']);
$password=mysqli_real_escape_string($con,$_POST['password']);
$confirm=mysqli_real_escape_string($con,$_POST['confirm']);
$emailquery="SELECT * from registration where email ='$email'";
$query=mysqli_query($con,$emailquery);
$emailcount=mysqli_num_rows($query);
if($emailcount>0){
?>
<script>             
               swal("Email Id Already Exist",
                     "....",
                        "warning",
                        {
                           dangerMode: true,
                           buttons: false,
                           closeOnClickOutside: true,
                           timer:1000,
                                       })
                                       .then(function() {
                                 window.location = "Index.php";
                              
                        });
            </script>

<?php
}else{
if($password===$confirm){
$insertquery="INSERT into registration(name,email,password,confirm)VALUES('$name','$email','$password','$confirm')";
$iquery=mysqli_query($con,$insertquery) or die(mysqli_error($con));
if($iquery){ 
?>

<script>             
               swal("Successfully Registered",
                     "....",
                        "success",
                        {
                           dangerMode: true,
                           buttons: false,
                           closeOnClickOutside: true,
                           timer:1000,
                                       })
                                       .then(function() {
                                 window.location = "Index.php";
                              
                        });
            </script>
<?php
}else{
?>

      <script>             
               swal("Insert Unsuccessful",
                     "....",
                        "warning",
                        {
                           dangerMode: true,
                           buttons: false,
                           closeOnClickOutside: true,
                           timer:1000,
                                       })
                                       .then(function() {
                                 window.location = "Index.php";
                              
                        });
            </script>
<?php
}
}else{
?>

            <script>             
               swal("Operation Failed",
                     "....",
                        "warning",
                        {
                           dangerMode: true,
                           buttons: false,
                           closeOnClickOutside: true,
                           timer:1000,
                                       })
                                       .then(function() {
                                 window.location = "Index.php";
                              
                        });
            </script>
<?php
}
}
}
?>
<script>
function lettersOnly(input){
var regex = /[^a-z, ]/gi;
input.value = input.value.replace(regex, "");
}
</script>

<!-- ****Login form**** -->


<?php

include 'Connection.php';
if(isset($_POST['signin']))
{
    $email=$_POST['email'];
    $_SESSION['email'] = $email;

    $password=$_POST['password'];
    //$pass= password_hash($password,PASSWORD_BCRYPT);
    $email_search="SELECT * FROM registration WHERE email='$email'";
    $query=mysqli_query($con,$email_search);
    $email_count=mysqli_num_rows($query);
    if($email_count){
        $email_pass=mysqli_fetch_assoc($query);
       $db_pass=$email_pass['password'];
       $_SESSION['name']=$email_pass['name'];
       $_SESSION['email']=$email_pass['email'];
     
        if($password === $db_pass){
            $m_name = gethostname();
            $m_ip =  getHostByName(getHostName());
            $nameemp= $_SESSION['name'];
            date_default_timezone_set('Asia/Kolkata');
            $Login_time =  date('Y-m-d h:i:s');
            $sql="INSERT INTO login_logout(name, login_time,machine_name,machine_ip) VALUES('$nameemp', '$Login_time','$m_name','$m_ip')";
            $query1=mysqli_query($con,$sql) or die(mysqli_error($con));
            //  header("location:home.php");
            ?>

            <script>
               swal("Login Successful",
                     "You Have Successfully Logged in to Frontdoor...",
                        "success",
                        {
                           dangerMode: true,
                           buttons: false,
                           closeOnClickOutside: true,
                           timer:1000,
                                       })
                                       .then(function() {
                                 window.location = "add_proj.php";
                              
                        });
            </script>
      <?php
        }else{
            ?>
            
                  <script>
               
                    swal("Incorrect Password",
                     "Please Enter Correct Password",
                        "warning",
                        {
                           dangerMode: true,
                           buttons: false,
                           closeOnClickOutside: true,
                           timer:1000,
                                       })
                                       .then(function() {
                                 window.location = "Index.php";
                              
                        });
                  </script>               
                <?php
        }
    }
    else{
        ?>
                <script>             
               swal("Invalid Email",
                     "Please Enter Valid EmailId",
                        "warning",
                        {
                           dangerMode: true,
                           buttons: false,
                           closeOnClickOutside: true,
                           timer:1000,
                                       })
                                       .then(function() {
                                 window.location = "Index.php";
                              
                        });
            </script>
                <?php
    }
}


?>