<?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;
    echo $cargo;


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


    //             if ($active > 3){ 
    //                 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 ".$cargo." 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>';
                                

    //                             //Browser id တူခဲ့ရင် Id 0 ဖြစ်
    //                             $sql = "UPDATE ".$cargo." SET browser_id='0',active='0' WHERE user_name!='$name'"; //update Brower ID
    //                             $insertResult = $connection->query($sql);
    //                             if ($insertResult) {
    //                                 echo '<script>alert("Login Success") ;</script>';
    //                                 echo '<script>window.location = "login_user.html";</script>';
    //                                 echo '<script>window.location = "product_refill.php";</script>'; //If Exit
        
    //                             } else {
    //                                 //echo '<script>window.location = "login_user.html";</script>';
    //                             }
    //                         }
    //                     } else {//If Not
    //                         $sql = "UPDATE ".$cargo." SET browser_id='$b_id',active='0' WHERE cargo='$name'"; //update Brower ID
    //                         $insertResult = $connection->query($sql);
    //                         if ($insertResult) {
    //                             echo '<script>alert("Login Success") ;</script>';
    //                             echo '<script>window.location = "product_refill.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 ".$cargo." WHERE cargo=?";
    //                     $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 ".$cargo." SET active=? WHERE cargo=?";
    //                             $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;
}
?>
