<?php

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

$tanks_res = $db->query("sites", "SELECT * FROM sites WHERE company_id = {$_SESSION['company_id']} ORDER BY record_id DESC");
?>

<div class="tank_item_container">
    <h1>SITES</h1>
    <input type="submit" class="app_buttons_small" value="ADD" onclick="window.location.href='add_site.php'">
</div>
<div class="tank_item_container">

    <div class="table-responsive">
        <table>
            <tr>
                <th>
                    NAME
                </th>
            </tr>

            <?php

            while ($tanks = $tanks_res->fetch_assoc()) {
                ?>

                <tr>
                    <td>
                        <input type='text' value='<?php echo $tanks['name']; ?>'
                            onchange="change(this,'name','<?php echo $tanks['record_id']; ?>')" class="table_input">
                    </td>

                </tr>
                <?php
            }
            ?>

        </table>
    </div>
</div>

<script>
    function change(input, column_name, record_id) {
        if (confirm("Are you sure you want to change this detail?")) {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function () {
                if (this.readyState == 4 && this.status == 200) {
                    var orig_color = input.style.backgroundColor;
                    if (this.responseText == "1") {
                        input.style.transition = "background-color 1.5s ease";
                        input.style.backgroundColor = "green";
                        setTimeout(function () {
                            input.style.backgroundColor = orig_color;
                        }, 1500);

                    } else {
                        input.style.transition = "background-color 1.5s ease";
                        input.style.backgroundColor = "red";
                        setTimeout(function () {
                            input.style.backgroundColor = orig_color;
                        }, 1500);
                        alert(this.responseText);
                    }
                }
            };
            xmlhttp.open("POST", "update_site_settings.ajax.php", true);
            xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlhttp.send("value=" + input.value + "&column_name=" + column_name + "&record_id=" + record_id);
        }
    }
</script>