
<?php include('../config/constants.php');?>

<!-- login and sign up form-->
<!DOCTYPE html>
<html>
  <head>
    <title>
      
    </title>
    <link rel="stylesheet" type="text/css" href="signupcss.css">
  </head>
  <body>
    <div class="wrapper">
      <strong >Log In To Your Account</strong>
    </div>
    <div class="form">

  <!-- login -->
  <div class="login-form">
    <!-- cansel btn -->
     
    <!-- heading--> 
    
    <strong>Log In</strong>
    <!-- inputs-->
    <form action="" method="POST">
       <input type="text" name="email" placeholder="enter email"required>

      <input type="password" name="password" placeholder="enter password"required>
      <!-- submit -->
      <input type="submit" name="login" value="Log in">
    </form>
    <div class="form-btns">
      <!-- forget password and sign up btn -->
      <a href="javascript:void(0);" class="forget">Forget Your Password?
      </a>
       <a href="signin.php" class="forget">Don't have an account?</a>
    </div>

  </div>




</div>
<!-- insert data into db -->

   <?php
   //check whether the submit btn is clicked or not
    if (isset($_POST['login'])) {
      //process for login
      //1.get the data frm login form
      //$username = $_POST['username'];
      
    

      $email =  mysqli_real_escape_string($conn,$_POST['email']);
      $password2 =  md5($_POST['password']);

      $password = mysqli_real_escape_string($conn,$password2);


     
      
      //2.sql to check whether the user with the username and password exists or not
      $sql1 = "SELECT * FROM tbl_user WHERE email='$email' AND password='$password'";
      //3.execute the querry
      $res1 = mysqli_query($conn, $sql1);
      
      //4.count rows to check whether the user exists or not
      $count1 = mysqli_num_rows($res1);
      if ($count1==1) {
          //  e = $row1['role'];
            $row1 = mysqli_fetch_assoc($res1);
            //$userid = 
            $_SESSION['userid'] = $row1['id'];
        // user available and login success
            $_SESSION['login'] = "<div class='success'>Login Successful.</div>";
            $_SESSION['user'] = $username;//to check whether the user is logged in or not and logout will unset it
            //redirect to the dashboard
             header('location:'.SITEURL.'shopping/index.php');
           }else{
        //user not available and login failed
        $_SESSION['login'] = "<div class='error'>Login Failed.</div>";
        //redirect to the dashboard
        header('location:'.SITEURL.'shopping/login.php');
      //  echo "nope";
      }
    }else{

      //echo "error occurred during data proccessing";
    }
    ?>

  </body>
</html>
