<?php
session_start();

include "classes/html.class.php";
$db = new db();

$qr = new qrcode();

$html = new html("FUEL");

?>

<div class='form'>
    <form action='home.php?tran_complete' method='POST' id='form_1'>

        <h2>TRAND ID</h2>
        <input type='text' id='tran_id' class='form_input' value='<? echo $_SESSION['current_transaction']; ?>' />
        <h2>PUMP STATUS:</h2>
        <input type='text' id='status' class='form_input' value='WAITING' />
        <h2>PUMP AMOUNT:</h2>
        <input type='text' id='amount' class='form_input' value='0' />
        <br>
        <h2>FLOW RATE:</h2>
        <input type='text' id='flow_rate' class='form_input' value='0' /><br><br>
        <input type='submit' value='continue' hidden class='form_btn' id='complete' />
    </form>
</div>

</body>


</html>

<script>
    setInterval(check_status, 1000);

    function check_status() {

        if ("<? echo $responce; ?>" == "NOT A VALID TANK ID") {
            alert("DID NOT FIND THE CORRECT TANK ID, PLEASE TRY AGAIN");
            window.location.href = 'fuel_step_a.php';
        }

        tran_id = <? echo $_SESSION['current_transaction']; ?>;


        // Create an XMLHttpRequest object
        const xhttp = new XMLHttpRequest();
        // Define a callback function
        xhttp.onload = function() {
            // Here you can use the Data
            if (this.responseText != 1) {
                // alert("fuel_tran_details.ajax.php?tran_id = " + tran_id);
                ar = this.responseText.split("|");

                // alert(ar);
                if (ar[0] == "AUTHORISED") {
                    document.getElementById("status").value = "FUEL TRANSACTION OPENED";
                }
                if (ar[0] == "INI") {
                    document.getElementById("status").value = "PUMP ABOUT TO START";
                }
                if (ar[0] == 'DONE') {
                    document.getElementById("status").value = "COMPLETE";
                    document.getElementById("complete").hidden = false;
                }
                document.getElementById("amount").value = ar[1];

                document.getElementById("flow_rate").value = ar[2];

            } else {
                alert("Something Went Wrong");
            }
        }
        xhttp.open("GET", "ajax/fuel_tran_details.ajax.php?tran_id=" + tran_id);
        xhttp.send();

    }
</script>