<?php
if (!isset($_SESSION)) {
    session_start();
}
include('includes/config.php');

$error = "";

if (isset($_POST['secret']) && $_POST['secret'] == "ahkwebsolutions" && !empty($_POST['username']) && !empty($_POST['password'])) {

    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $password = $_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);

        if (password_verify($password, $userdata['password'])) {
            if ($userdata['status'] == "verified") {

                $_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'];

                echo "<script>
                    alert('Login Successful');
                    window.location='admin';
                </script>";
                exit;

            } else {
                $error = "Your account is blocked.";
            }
        } else {
            $error = "Incorrect password.";
        }
    } else {
        $error = "User not found.";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $ahkweb['name']; ?> Login</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">

<style>
*{
    margin:0;
    padding:0;
    box-sizing:border-box;
    font-family:'Poppins',sans-serif;
}
body{
    height:100vh;
    display:flex;
    justify-content:center;
    align-items:center;
    background:linear-gradient(135deg,#0f0c29,#302b63,#24243e);
    overflow:hidden;
}
body::before,
body::after{
    content:"";
    position:absolute;
    border-radius:50%;
    filter:blur(60px);
}
body::before{
    width:300px;
    height:300px;
    background:#ffd70055;
    top:10%;
    left:10%;
}
body::after{
    width:350px;
    height:350px;
    background:#00ffff33;
    bottom:10%;
    right:10%;
}
.login-box{
    width:390px;
    padding:45px 35px;
    border-radius:30px;
    background:rgba(255,255,255,0.08);
    backdrop-filter:blur(25px);
    border:1px solid rgba(255,255,255,0.12);
    box-shadow:0 25px 50px rgba(0,0,0,0.35);
    position:relative;
    z-index:2;
}
.logo{
    text-align:center;
    color:#fff;
    font-size:28px;
    font-weight:700;
    margin-bottom:30px;
}
.subtitle{
    text-align:center;
    color:#ddd;
    font-size:13px;
    margin-top:-18px;
    margin-bottom:25px;
}
.input-group{
    margin-bottom:20px;
}
.input-group input{
    width:100%;
    height:55px;
    border:none;
    outline:none;
    border-radius:15px;
    padding:0 18px;
    font-size:15px;
    color:#fff;
    background:rgba(255,255,255,0.08);
}
.input-group input::placeholder{
    color:#ddd;
}
.password-box{
    position:relative;
}
.toggle{
    position:absolute;
    right:15px;
    top:16px;
    color:#fff;
    cursor:pointer;
    font-size:18px;
}
.btn{
    width:100%;
    height:55px;
    border:none;
    border-radius:15px;
    background:linear-gradient(90deg,#ffd700,#ffb300);
    color:#111;
    font-size:16px;
    font-weight:700;
    cursor:pointer;
    transition:0.3s;
}
.btn:hover{
    transform:translateY(-3px);
    box-shadow:0 10px 20px rgba(255,215,0,0.25);
}
.links{
    text-align:center;
    margin-top:25px;
}
.links a{
    display:block;
    color:#ddd;
    text-decoration:none;
    margin:8px 0;
    font-size:14px;
}
.links a:hover{
    color:#fff;
}
.error{
    text-align:center;
    color:#ff9999;
    margin-bottom:15px;
    font-size:14px;
}
</style>
</head>
<body>

<div class="login-box">
    <div class="logo"><?php echo $ahkweb['name']; ?></div>
    <div class="subtitle">Secure Member Login Portal</div>

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

    <form method="post">
        <input type="hidden" name="secret" value="ahkwebsolutions">

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

        <div class="input-group password-box">
            <input type="password" name="password" id="password" placeholder="Enter Password" required>
            <span class="toggle" onclick="showPass()">👁</span>
        </div>

        <button class="btn">LOGIN NOW</button>
    </form>

   
</div>

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

</body>
</html>