<?php

$name = $_POST['name'];
//$name = 'imv_user_1';
$password_input = $_POST['password'];


include 'database/database.php';
include 'database/data.php';

session_start();
                                
// if (isset($_SESSION['browser_id'])) {
//     $browserId = $_SESSION['browser_id'];
// } else {
    // Generate a new browser ID
    $browserId = generateUniqueBrowserId();

    // Store the browser ID in the session for future use
    $_SESSION['browser_id'] = $browserId;
// }



    $b_id = $browserId;
    $sql = "SELECT * FROM ".$user_database." WHERE user_name='$name'";
    $result = $connection->query($sql);
        if ($result && $result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $user_name = $row['user_name'];
                $_password = $row['password'];
                $active = $row['active'];
                $c_bid = $row['browser_id'];


                if ($active > 30){ 
                    echo '<script>alert("Your account have been Locked! Contact Your Admin") ;</script>';
                    echo '<script>window.location = "login_user.html";</script>';
                } else { 
                    if ($user_name == $name && $_password == $password_input) {
                 
    
                        // Check Brower ID exist or not
                        $sql = "SELECT * FROM ".$user_database." WHERE browser_id='$b_id'";
                        $result = $connection->query($sql);
                        if ($result && $result->num_rows > 0) {
                            while ($row = $result->fetch_assoc()) {
                                // echo '<script>alert("Login Success") ;</script>';
                                // echo '<script>window.location = "sell_lk.php";</script>'; //If Exit
                            }
                        } else {//If Not
                            $sql = "UPDATE ".$user_database." SET browser_id='$b_id' WHERE user_name='$name'"; //update Brower ID
                            $insertResult = $connection->query($sql);
                            if ($insertResult) {
                                echo '<script>alert("Login Success") ;</script>';
                                echo '<script>window.location = "sell_ll.php";</script>';
    
                            } else {
                                //echo '<script>window.location = "login_user.html";</script>';
                            }
    
                        }
    
                    } else {
                        echo '<script>alert("Try Again! You have chance 3 time, after that account will be locked") ;</script>';
                        // echo '<script>window.location = "login_user.html";</script>';
                       
    
                        $sql = "SELECT * FROM ".$user_database." WHERE user_name=?";
                        $stmt = $connection->prepare($sql);
                        $stmt->bind_param("s", $name);
                        $stmt->execute();
                        $result = $stmt->get_result();
    
                        if ($result && $result->num_rows > 0) {
                            while ($row = $result->fetch_assoc()) {
                                $active = $row["active"];
                                // Assuming active is a timestamp, update it to current timestamp
                                $login_time = $active+1; 
    
                                $sql_update = "UPDATE ".$user_database." SET active=? WHERE user_name=?";
                                $stmt_update = $connection->prepare($sql_update);
                                $stmt_update->bind_param("ss", $login_time, $name);
                                $insertResult = $stmt_update->execute();
    
                                if ($insertResult) {
                                    echo '<script>window.location = "login_user.html";</script>';
                                } else {
                                    // Update failed
                                }
                                
                            }
                        } else {
                            // No matching records found
                        }
                }  
                }
                
        }
    }

            else {
                echo '<script>alert("Wrong Password Or User Name!") ;</script>';
                echo '<script>window.location = "login_user.html";</script>';
            }




            


function generateUniqueBrowserId()
{
    $randomString = bin2hex(random_bytes(16));
    $timestamp = time();

    return $randomString . $timestamp;
}
?>
