<?php

include "../../root.class.php";

$html = new html();
$html->add_styles_page();

$client_name = new input();
$client_name->id("client_name");
$client_name->name("client_name");
$client_name->type("text");
$client_name->placeholder("Client Name");
$client_name->class();
$client_name->value_from_db("clients", "client_name", "record_id = {$_GET['record_id']}");
$client_name->required();

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

$record_id = new input();
$record_id->id("record_id");
$record_id->name("record_id");
$record_id->type("hidden");
$record_id->value($_GET['record_id']);
$record_id->class();

$rfid = new input();
$rfid->id("client_rfid_tag");
$rfid->name("client_rfid_tag");
$rfid->type("hidden");
$rfid->value_from_db("clients", "client_rfid_tag", "record_id = {$_GET['record_id']}");
$rfid->class();

$register_rfid = new button();
$register_rfid->id("register_rfid");
$register_rfid->name("register_rfid");
$register_rfid->value("CLICK TO REGISTER RFID");
$register_rfid->class();
$register_rfid->onclick("register_rfid()");

$add_client = new button();
$add_client->id("add_client");
$add_client->name("add_client");
$add_client->value("Edit Client");
$add_client->class();
$add_client->onclick("update_client()");


?>

<div class="form_down">
    <?php
    $record_id->add();
    echo "<label>Client Name</label>";
    $client_name->add();
    echo "<label>PLEASE SELECT A TANK TO REGISTER FROM</label>";
    $db = new db_safeguard();
    $res = $db->query("tanks", "SELECT * FROM tanks WHERE company_id = {$_SESSION['company_id']}");
    while ($row = $res->fetch_assoc()) {
        echo "<button class=\"submit_btn\"  onclick=\"add_request('" . $row['record_id'] . "')\">" . $row['name'] . "</button> <br><br>";
    }
    ?>
</div>
<script>
    var rfid_received = 0;
    rfid_check = 0;

    function add_request(tank_id) {
        console.log("CLICKED");
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "add_request.php", true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.onreadystatechange = function () {
            console.log(xhr.responseText);
            if (xhr.readyState == 4 && xhr.status == 200) {
                if (xhr.responseText == "success") {
                    alert("PLEASE MOVE TO THE SELECTED TANK TO SCAN THE NEW RFID TAG");
                    rfid_check = 1;
                } else {
                    alert("SOMETHING WENT WRONG");
                    rfid_check = 0;

                }

            }
        }
        xhr.send("client_id=" + document.getElementById("record_id").value + "&tank_id=" + tank_id);
    }

    setInterval(function () {
        if (rfid_check == 1) {
            console.log("RUNNING CHECK");
            var xhr2 = new XMLHttpRequest();
            xhr2.open("POST", "check_for_change.php", true);
            xhr2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr2.onreadystatechange = function () {
                console.log(" ---> " + xhr2.responseText);

                if (xhr2.readyState == 4 && xhr2.status == 200) {
                    if (xhr2.responseText == "success") {
                        alert("RFID TAG RECEIVED");
                        window.location.href = "edit_client.php?record_id=" + document.getElementById("record_id").value;
                    }
                } else {
                }
            }
            xhr2.send("client_id=" + document.getElementById("record_id").value);
        }
    }, 1000);
</script>