<?php 
date_default_timezone_set('Asia/Kolkata');
$current_time = date('M d, Y, g:i:s A', time());
?>
<?php 
include('includes/config.php');
if(isset($_SESSION['loggedin']) == true){
  ?>
  <script> 
  window.setTimeout(function() {
  window.location.href = "crs/index";
  }, 1500);
  </script>
  
    <?php
}  
if(isset($_POST['secret']) && $_POST['secret'] == "ahkwebsolutions" && !empty($_POST['username']) && !empty($_POST['password'])){
  $username = mysqli_real_escape_string($conn,$_POST['username']);
  $password = mysqli_real_escape_string($conn,$_POST['password']);

  $res = mysqli_query($conn,"SELECT * FROM `usertable` WHERE phone='$username'  OR emailid='$username' OR userid='$username' ");

  if(mysqli_num_rows($res) == 1 ){
    $userdata = mysqli_fetch_assoc($res);
    $vpass = password_verify($password,$userdata['password']);
    if($vpass){
      if($userdata['status'] == "verified"){
        
        ?>
			<script>
				$(function(){
					Swal.fire(
						'Success!',
						'You Are Logged In Successfully!',
						'success'
					)
				});
			</script>
			<?php
      
      $_SESSION['loggedin'] = true;
      $_SESSION['userid'] = $userdata['userid'];
      $_SESSION['name'] = $userdata['name'];
      $_SESSION['emailid'] = $userdata['emailid'];
      $_SESSION['phone'] = $userdata['phone'];
      $_SESSION['walletamount'] = $userdata['walletamount'];
      $_SESSION['usertype'] = $userdata['usertype'];
     
      ?>
      <script>
      window.setTimeout(function() {
      window.location.href = "crs/index";
      }, 1500);
      </script>
      
        <?php

        // 
      }else{
        ?>
        <script>
          $(function(){
            Swal.fire(
              'Opps!',
              'Your Account is Blocked or inactive Please Contact With Admin',
              'error'
            )
          });
        </script>
        <?php

      }


    }else{
      ?>
      <script>
        $(function(){
          Swal.fire(
            'Opps!',
            'Password Wrong!',
            'error'
          )
        });
      </script>
      <?php
    }

  }else{
    ?>
    <script>
      $(function(){
        Swal.fire(
          'Opps!',
          'Entered username Wrong or Not Exist',
          'error'
        )

      });

    </script>
    <?php
  }



}
?>


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>  Login</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<style>
*{margin:0;padding:0;box-sizing:border-box;font-family:'Poppins',sans-serif;}

body{
  min-height:100vh;
  display:flex;
  justify-content:center;
  align-items:center;
  background: radial-gradient(circle,#1a1a2e,#0f3460,#000);
}

/* Card */
.login-box{
  width:100%;
  max-width:360px;
  padding:30px;
  border-radius:15px;
  background:rgba(255,255,255,0.08);
  backdrop-filter:blur(15px);
  box-shadow:0 0 30px rgba(0,0,0,0.6);
  text-align:center;
  color:#fff;
}

/* Input */
.input-group{
  position:relative;
  margin-bottom:20px;
}

.input-group input{
  width:100%;
  height:45px;
  padding:0 45px;
  border:none;
  border-radius:10px;
  background:rgba(255,255,255,0.15);
  color:#fff;
}

/* Icons */
.input-group i{
  position:absolute;
  top:50%;
  transform:translateY(-50%);
}

.fa-user, .fa-lock{ left:15px; }

.toggle-pass{
  right:15px;
  cursor:pointer;
}

/* Button */
.btn{
  width:100%;
  height:45px;
  border:none;
  border-radius:10px;
  background:linear-gradient(45deg,#ff416c,#ff4b2b);
  color:#fff;
  cursor:pointer;
}

/* Error */
.msg{
  background:#ff4b2b;
  padding:10px;
  border-radius:8px;
  margin-bottom:15px;
  font-size:13px;
}

/* Links */
.links{
  margin-top:15px;
  font-size:13px;
}

.links a{
  color:#00f2ff;
  text-decoration:none;
}

.links a:hover{
  text-decoration:underline;
}
</style>
</head>

<body>

<div class="login-box">
  <h2>👑   LOGIN</h2>

  <?php if($msg!=""){ echo "<div class='msg'>$msg</div>"; } ?>

  <form method="post">

    <div class="input-group">
      <i class="fa fa-user"></i>
      <input type="text" name="username" placeholder="Username / Email" required>
    </div>

    <div class="input-group">
      <i class="fa fa-lock"></i>
      <input type="password" id="password" name="password" placeholder="Password" required>
      <i class="fa fa-eye toggle-pass" onclick="togglePass()"></i>
    </div>

    <input type="hidden" name="secret" value="ahkwebsolutions">

    <button class="btn">Login</button>
  </form>

  <!-- ✅ Register Link Added -->
  <div class="links">
    <p>New User? <a href="register">Create Account →</a></p>
  </div>

</div>

<script>
function togglePass(){
  let p = document.getElementById("password");
  p.type = (p.type==="password") ? "text" : "password";
}
</script>

</body>
</html>