<?php
// Start the session
session_start();
?>

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
    <meta charset="utf-8">
    <title>Login</title>
    <link href="../css/demo.css" type="text/css" rel="stylesheet">
    <link rel="stylesheet" href="../css/licensekey.css" type="text/css">
</head>
<body>

<div class="sec_one"></div>
<!--//-------------------ADDED POP UP FOR THE LICENSE KEY CHECKING--------------------------------//-->
<div id="popup_token" class="popup_licensekey">
    <div class="popup-content">
        <form id="csvForm" enctype="multipart/form-data" action="verify_license.php" method="POST">
            <input type="file" name="csvfile" accept=".csv" required />
            <button type="submit">Submit</button>
            <p class="message" id="message"></p>
        </form>
    </div>
</div>
<div class="blur-background"></div>
<!--//-------------------ADDED POP UP FOR THE LICENSE KEY CHECKING ENDS--------------------------------//-->

<div class="login">
<!--------------------------------------------FORM STARTS------------------------------------------>
    <form method="POST" class="form">
        <div class="login-container">
        <img class="cezen" src="../images/LOGO.png" >
            <div class="login-box">
                <div class="log">
                <h3>Log in to CezenNAC</h3>
             <!--   <h2><?php //echo $_SESSION['difference'] ?></h2> -->
                <form method="post" class="form">
                    <label>User Name</label>
                    <input type="text" placeholder="Login with username" name="username" required> <br>
                    <label>Password</label>
                    <input type="password" placeholder="Password" name="password" required>
                    <button type="submit" name="action" value="login">Log in</button>
                </form>
                </div>
                <?php 
                 include('key.php');
                ?>

            </div>
        </div>

        <?php 
       
        include('functions.php');
        unset($_SESSION['token']);
        unset($_SESSION['username']);
        unset($_SESSION['password']);
        
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $action = $_POST['action'];

            if ($action == 'login') {
                $username = $_POST['username'];
                $password = $_POST['password'];


        //-------------------API CALL------------------------------------//

                $jsonFilePath = __DIR__ . '/../urls/api_endpoints.json';
                $jsonData = file_get_contents($jsonFilePath);
                $endpoints = json_decode($jsonData, true);
                $url = $endpoints['index'];
//---------------------------ENDS HERE----------------------------------------//

                $data = array("username" => $username, "password" => $password);
                $result = make_post_request($url, $data);
                $response = json_decode($result, true);

                if (isset($response['token'])) {
                    $_SESSION['token'] = $response['token'];
                    $_SESSION['username'] = $username;
                    $_SESSION['password'] = $password; // Stored password in session.
                    $message = "Login successful! Token stored.";
                   // echo $_SESSION['token'];
                    echo $_SESSION['acesslevel'];
                    $password_two = $_SESSION['password'];
                    $username_two =  $_SESSION['username'];
                    $token = $_SESSION['token'];
                    echo $_SESSION['token'];
                   

                    //-------------------------------------ACESS LEVEL FETCHING------------------------------------------------//

                    $_SESSION['acesslevel'] = '';

                    
        //-------------------API CALL------------------------------------//

                $jsonFilePath = __DIR__ . '/../urls/api_endpoints.json';
                $jsonData = file_get_contents($jsonFilePath);
                $endpoints = json_decode($jsonData, true);
                $url_one = $endpoints['login']. $username_two . "/password?";
//---------------------------ENDS HERE-----------------------------------------//


                    $result = make_get_request($url_one, ["Authorization: $token"]);
                    $user_list = json_decode($result, true);
                   

                    foreach ($user_list as $user) {
                    if($user['pid']==$username_two){

                       $_SESSION['acesslevel']=$user['access_level'];
                       
                    }}
                    header('Location:dashboard.php');
                    exit;
                } else {
                    $message = "Login failed!";
                }
            }
            echo $_SESSION['acesslevel'];
        }
        ?>
    </form>
    </div> <!------------------- login div before form----->

    <div class="sec_two"></div>
    <script src="../js/license_key.js"></script>
</body>
</html>
