<?php
session_start();

include "../../html.class.php";
include "../../root.class.php";
include "../../classes/db.class.php";
$html = new html();
$functions = new functions();

$user_res = $db->query("users", "SELECT * FROM users WHERE record_id = {$_GET['record_id']}");
$user = $user_res->fetch_assoc();

$user_record_id = new input();
$user_record_id->type("hidden");
$user_record_id->name("record_id");
$user_record_id->id("record_id");
$user_record_id->value($_GET['record_id']);
// HTML FORM ELEMENTS
$username = new input();
$username->class("inputs");
$username->type("text");
$username->placeholder("username");
$username->name("username");
$username->required();
$username->id("username");
$username->value_from_db("users", "username", "record_id = {$_GET['record_id']}");

$password = new input();
$password->class("inputs");
$password->type("text");
$password->placeholder("password");
$password->name("password");
$password->id("password");

$company_id = new input();
$company_id->class("inputs");
$company_id->type("hidden");
$company_id->placeholder("company id");
$company_id->name("company_id");
$company_id->required();
$company_id->id("company_id");
$company_id->value($_SESSION['company_id']);

$user_type = new select();
$user_type->class("inputs");
$user_type->name("access_type");
$user_type->id("access_type");
$user_type->add_option("", "");
$user_type->add_option("ALL", "MANAGMENT");
$user_type->add_option("OPERATOR", "FUEL OPERATOR");
$user_type->required();
$user_type->value_from_db("users", "access_type", "record_id = {$_GET['record_id']}");

$status = new select();
$status->class("inputs");
$status->name("status");
$status->id("status");
$status->add_option("", "STATUS");
$status->add_option("1", "ACTIVE");
$status->add_option("0", "INACTIVE");
$status->value_from_db("users", "status", "record_id = {$_GET['record_id']}");

$submit_btn = new button();
$submit_btn->value("UPDATE");
$submit_btn->class("app_buttons");
$submit_btn->onclick("edit_user()");

$tanks = new input();
$tanks->id("tanks");
$tanks->type("hidden");
$tanks->name("tanks");
$tanks->value_from_db("users", "tanks", "record_id = {$_GET['record_id']}");

$delete_btn = new button();
$delete_btn->value("DELETE");
$delete_btn->class('app_buttons_small');
$delete_btn->onclick("delete_user()");

?>

<div class="tank_item_container form_down">
    <h1>EDIT USER</h1>
    <?php
    $company_id->add();
    $user_record_id->add();
    echo "<label for='username'>Username</label>";
    $username->add();

    echo "<label for='password'>Password</label>";
    $password->add();

    echo "<label for='status'>Status</label>";
    $status->add();

    echo "<label for='user_type'>Access Level</label>";
    $user_type->add();

    echo "<label for='user_type'>TANK ACCESS</label><br>";
    $tanks->add();

    // All button
    echo "<button type='button' class='app_buttons_small' onclick='toggle_all()' id='allBtn'>Select All</button><br><br>";

    $tanks_res = $db->query("tanks", "SELECT * FROM tanks WHERE record_id IN ({$_SESSION['tanks']}) AND company_id = {$_SESSION['company_id']} ORDER BY site_id ASC");
    while ($tank = $tanks_res->fetch_assoc()) {

        if (strpos($user['tanks'], $tank['record_id']) !== false) {
            echo "<input class='inputs tank-item' type='text' value='{$tank['name']}' readonly 
             data-id='{$tank['record_id']}'
             style='background-color:#0099ff;cursor:pointer;' 
             onclick='add_tank({$tank['record_id']},this)' /><br>";
        } else {
            echo "<input class='inputs tank-item' type='text' value='{$tank['name']}' readonly 
            data-id='{$tank['record_id']}'
            style='background-color:#FFFFFF;cursor:pointer;' 
            onclick='add_tank({$tank['record_id']},this)' /><br>";
        }
    }
    ?>
    <script>
        function add_tank(record_id, input) {
            const tanksField = document.getElementById("tanks");
            let selected = tanksField.value.split(",").filter(v => v.trim() !== "");

            if (input.style.backgroundColor === "rgb(255, 255, 255)" || input.style.backgroundColor === "#FFFFFF") {
                // Selecting tank
                selected.push(record_id);
                input.style.backgroundColor = "#0099ff";
            } else {
                // Deselecting tank
                selected = selected.filter(id => id != record_id);
                input.style.backgroundColor = "#FFFFFF";
            }

            tanksField.value = selected.join(",");
        }

        function toggle_all() {
            const tanksField = document.getElementById("tanks");
            const inputs = document.querySelectorAll(".tank-item");
            const allBtn = document.getElementById("allBtn");
            let selected = [];

            if (allBtn.innerText === "Select All") {
                // Select everything
                inputs.forEach(input => {
                    input.style.backgroundColor = "#0099ff";
                    selected.push(input.getAttribute("data-id"));
                });
                allBtn.innerText = "Clear All";
            } else {
                // Clear everything
                inputs.forEach(input => {
                    input.style.backgroundColor = "#FFFFFF";
                });
                selected = [];
                allBtn.innerText = "Select All";
            }

            tanksField.value = selected.join(",");
        }
    </script>
    <br>
    <?php
    $submit_btn->add();
    ?>
    <br>

    <br>
    <?php
    $delete_btn->add();
    ?>
</div>
<div class="spacer"></div>

<?php
$ajax = new js_ajax();
$ajax->function_name("edit_user");
$ajax->submit_btn_id("submit");
$ajax->update("users");
$ajax->on_success("SUCCESSFULLY UPDATED THE USER");




$ajax_delete = new js_ajax();
$ajax_delete->function_name("delete_user");
$ajax_delete->submit_btn_id("submit");
$ajax_delete->delete("users");
$ajax_delete->on_success("SUCCESSFULLY DELETED THE USER");
$ajax_delete->redirect("home.php");
?>