<?php 
// 1. सबसे पहले सेशन शुरू करें और टाइमज़ोन सेट करें
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
date_default_timezone_set('Asia/Kolkata');
$current_time = date('M d, Y, g:i:s A', time());

// 2. कॉन्फ़िगरेशन शामिल करें
include('includes/config.php');

// 3. यदि उपयोगकर्ता पहले से लॉग इन है, तो उसे रीडायरेक्ट करें
if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true){
    ?>
    <script> 
        window.location.href = "crs/index";
    </script>
    <?php
    exit;
}  

// 4. बैकएंड लॉगिन प्रोसेसिंग वैरिएबल्स
$error_script = "";

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"){
                
                // सेशन डेटा सेट करें
                $_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'];
                
                // 🔐 टोकन जनरेट और डेटाबेस अपडेट
                $token = bin2hex(random_bytes(16));
                $_SESSION['token'] = $token;
                mysqli_query($conn, "UPDATE usertable SET session_token='$token' WHERE userid='".$userdata['userid']."'");

                // 🔐 सेशन आईडी को री-जेनरेट करें (सुरक्षा के लिए)
                session_regenerate_id(true);

                $error_script = "
                    Swal.fire({
                        icon: 'success',
                        title: 'Success!',
                        text: 'You Are Logged In Successfully!',
                        timer: 1500,
                        showConfirmButton: false
                    }).then(() => {
                        window.location.href = 'crs/index';
                    });
                ";
            } else {
                $error_script = "
                    Swal.fire({
                        icon: 'error',
                        title: 'Oops!',
                        text: 'Your Account is Blocked or inactive. Please Contact Admin.'
                    });
                ";
            }
        } else {
            $error_script = "
                Swal.fire({
                    icon: 'error',
                    title: 'Oops!',
                    text: 'Wrong Password! Please try again.'
                });
            ";
        }
    } else {
        $error_script = "
            Swal.fire({
                icon: 'error',
                title: 'Oops!',
                text: 'Entered username does not exist!'
            });
        ";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Secure Member Login</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.css">
    
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Poppins', sans-serif;
        }

        body {
            background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .login-container {
            background: rgba(255, 255, 255, 0.08);
            backdrop-filter: blur(12px);
            -webkit-backdrop-filter: blur(12px);
            border: 1px solid rgba(255, 255, 255, 0.15);
            padding: 40px;
            width: 100%;
            max-width: 450px;
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
            color: #fff;
        }

        .login-header {
            text-align: center;
            margin-bottom: 35px;
        }

        .login-header .logo-icon {
            font-size: 3rem;
            color: #00f2fe;
            margin-bottom: 10px;
            text-shadow: 0 0 15px rgba(0, 242, 254, 0.5);
        }

        .login-header h2 {
            font-size: 1.8rem;
            font-weight: 600;
            letter-spacing: 0.5px;
            margin-bottom: 5px;
        }

        .login-header p {
            font-size: 0.85rem;
            color: #cbd5e1;
        }

        .form-group {
            position: relative;
            margin-bottom: 25px;
        }

        .form-group i.input-icon {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: #cbd5e1;
            font-size: 1.1rem;
            transition: 0.3s;
        }

        .form-control {
            width: 100%;
            padding: 15px 45px 15px 45px;
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.1);
            border-radius: 10px;
            outline: none;
            color: #fff;
            font-size: 0.95rem;
            transition: all 0.3s ease;
        }

        .form-control:focus {
            background: rgba(255, 255, 255, 0.15);
            border-color: #00f2fe;
            box-shadow: 0 0 10px rgba(0, 242, 254, 0.2);
        }

        .form-control::placeholder {
            color: #94a3b8;
        }

        .form-control:focus ~ i.input-icon {
            color: #00f2fe;
        }

        .password-toggle {
            position: absolute;
            right: 15px;
            top: 50%;
            transform: translateY(-50%);
            cursor: pointer;
            color: #cbd5e1;
            transition: 0.3s;
        }

        .password-toggle:hover {
            color: #00f2fe;
        }

        .login-btn {
            width: 100%;
            padding: 15px;
            background: linear-gradient(135deg, #00f2fe 0%, #4facfe 100%);
            border: none;
            border-radius: 10px;
            color: #fff;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 5px 15px rgba(79, 172, 254, 0.4);
        }

        .login-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(0, 242, 254, 0.6);
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
        }

        .login-btn:active {
            transform: translateY(0);
        }

        .form-footer {
            text-align: center;
            margin-top: 25px;
            font-size: 0.85rem;
            color: #cbd5e1;
        }

        .form-footer a {
            color: #00f2fe;
            text-decoration: none;
            font-weight: 500;
        }

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

    <div class="login-container">
        <div class="login-header">
            <div class="logo-icon"><i class="fa-solid(-shield-halved"></i><i class="fa-solid fa-user-lock"></i></div>
            <h2>Welcome Back</h2>
            <p>Please enter your credentials to login</p>
        </div>

        <form action="" method="POST" autocomplete="off">
            <input type="hidden" name="secret" value="ahkwebsolutions">

            <div class="form-group">
                <i class="fa-solid fa-user input-icon"></i>
                <input type="text" name="username" class="form-control" placeholder="User ID / Email / Phone" required>
            </div>

            <div class="form-group">
                <i class="fa-solid fa-lock input-icon"></i>
                <input type="password" id="password" name="password" class="form-control" placeholder="Password" required>
                <i class="fa-solid fa-eye password-toggle" id="togglePassword"></i>
            </div>

          
            <button type="submit" class="login-btn">Sign In</button>
        </form> 

        
        <div class="form-footer">
            <p style="margin-bottom: 8px;">
                Don't have an account? <a href="register"><i class="fa-solid fa-user-plus" style="margin-right: 3px;"></i> Click to Register</a>
            </p>
            <p>
                Forgot your credentials? <a href="forgot-password"><i class="fa-solid fa-unlock-keyhole" style="margin-right: 3px;"></i> Forgot Password?</a>
            </p>
        </div>
        

        <div class="form-footer">
            <p>Server Time: <?php echo date('g:i A', time()); ?></p>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

    <script>
        // 1. पासवर्ड शो/हाइड करने का स्क्रिप्ट
        const togglePassword = document.querySelector('#togglePassword');
        const password = document.querySelector('#password');

        togglePassword.addEventListener('click', function () {
            const type = password.getAttribute('type') === 'password' ? 'text' : 'password';
            password.setAttribute('type', type);
            this.classList.toggle('fa-eye-slash');
        });

        // 2. PHP से SweetAlert मैसेजेस को ट्रिगर करना
        $(document).ready(function() {
            <?php echo $error_script; ?>
        });
    </script>
	<script disable-devtool-auto src="https://cdn.jsdelivr.net/npm/disable-devtool@latest"></script>

</body>
</html>