<?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("tanks", "SELECT * FROM tanks WHERE company_id = {$_SESSION['company_id']} ORDER BY site_id DESC");
?>
<div class="tank_item_container">
    <h1>TANKS SETTINGS</h1>
</div>
<div class="tank_item_container">

    <div class="table-responsive">
        <table>
            <tr>
                <th>
                    NAME
                </th>
                <th>
                    SITE
                </th>
                <th>
                    CUT OFF TIMER (Seconds)
                </th>
                <th>
                    FLOW CALIBRATION
                </th>
                <th>
                    HAS TANK
                </th>
                <th>
                    TANK TYPE
                </th>
                <th>
                    LINKED TANKS
                </th>
                <th>
                    LENGTH mm
                </th>
                <th>
                    RADIUS mm
                </th>
                <th>
                    DEVIDE LITERS
                </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>
                    <td>
                        <select class="table_input" onchange="change(this,'site_id','<?php echo $tanks['record_id']; ?>')">
                            <?php $sites_res = $db->query("sites", "SELECT * FROM sites WHERE company_id = {$_SESSION['company_id']}"); ?>
                            <option value="<?php echo $tanks['site_id']; ?>">
                                <?php echo $functions->get_site_name($tanks['site_id']); ?>
                            </option>
                            <?php while ($sites = $sites_res->fetch_assoc()) { ?>
                                <option value="<?php echo $sites['record_id']; ?>"><?php echo $sites['name']; ?></option>
                            <?php } ?>
                        </select>
                    </td>
                    <td>
                        <input type='text' value='<?php echo $tanks['pump_cutoff_time']; ?>'
                            onchange="change(this,'pump_cutoff_time','<?php echo $tanks['record_id']; ?>')"
                            class="table_input">
                    </td>
                    <td>
                        <input type='text' value='<?php echo $tanks['flow_meter_calibration']; ?>'
                            onchange="change(this,'flow_meter_calibration','<?php echo $tanks['record_id']; ?>')"
                            class="table_input">
                    </td>
                    <td>
                        <select class="table_input" onchange="change(this,'has_tank','<?php echo $tanks['record_id']; ?>')">
                            <?php if ($tanks['has_tank'] == 1)
                                echo "<option value='1'>YES</option><option value='0'>NO</option>";
                            else
                                echo "<option value='0'>NO</option><option value='1'>YES</option>"; ?>
                        </select>
                    </td>
                    <td>
                        <select class="table_input"
                            onchange="change(this,'tank_type','<?php echo $tanks['record_id']; ?>')">
                            <?php

                            if ($tanks['tank_type'] == "H_C")
                                echo "<option value='H_C'>HORIZONTAL</option><option value='V_C'>VERTICAL</option>";
                            else
                                echo "<option value='V_C'>VERTICAL</option><option value='H_C'>HORIZONTAL</option>"; ?>
                        </select>
                    </td>
                    <td>
                        <input type='text' value='<?php echo $tanks['tank_amount']; ?>'
                            onchange="change(this,'tank_amount','<?php echo $tanks['record_id']; ?>')" class="table_input">
                    </td>
                    <td>
                        <input type='text' value='<?php echo $tanks['length_mm']; ?>'
                            onchange="change(this,'length_mm','<?php echo $tanks['record_id']; ?>')" class="table_input">
                    </td>
                    <td>
                        <input type='text' value='<?php echo $tanks['radius_mm']; ?>'
                            onchange="change(this,'radius_mm','<?php echo $tanks['record_id']; ?>')" class="table_input">
                    </td>
                    <td>
                        <input type='text' value='<?php echo $tanks['devide_for_liters']; ?>'
                            onchange="change(this,'devide_for_liters','<?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_tank_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>