<?php
include "classes/html.class.php";
session_start();
$db = new db();
$html = new html("silo dashboard");

// $batches = $db->exec_query('batches', ['*'], '', '', '', '', "status = 'COMPLETE'", '', false);
// if ($batches->num_rows > 0) {
//     while ($batch = $batches->fetch_assoc()) {
//         $bins_trans_res = $db->exec_query('bins_trans', ['*'], '', '', '', '', "silo_id <> 0", 'order by record_id desc', false);
//         while ($bins_trans = $bins_trans_res->fetch_assoc()) {

//             $variety_res = $db->exec_query('variety', ['*'], '', '', '', '', "record_id = {$bins_trans['variety_id']}", '', false);
//             $variety = $variety_res->fetch_assoc();

//             $silo_res = $db->exec_query('silos', ['*'], '', '', '', '', "record_id = {$bins_trans['silo_id']}", '', false);
//             $silo = $silo_res->fetch_assoc();

//             $table_data = $table_data . "<tr>
//                 <td><input type='text' class='table_input' value='BA {$bins_trans['batch_id']}' readonly /></td>
//                 <td><input type='text' class='table_input' value='{$variety['name']}' readonly /></td>
//                 <td><input type='text' class='table_input' value='{$bins_trans['amount']}' readonly /></td>
//                 <td><input type='text' class='table_input' value='{$bins_trans['date']}' readonly /></td>
//                 <td><input type='text' class='table_input' value='{$silo['name']}' readonly /></td>
//                 </tr>

//             ";

//             $data = $data . ["BA {$bins_trans['batch_id']}", "{$variety['name']}", "{$bins_trans['amount']}", "{$bins_trans['date']}", "{$silo['name']}"];
//         }
//     }

// } else {
//     $table_data = "<tr>
//         <td> NO RESULTS</td>
//     </tr>";
// }


//GET ALL THE SILIOS
// $silo_name = $db->exec_query('silos', ['*'], '', '', '', '', '', '', false);

// while ($silo = $silo_name->fetch_assoc()) {

//     $batch_res = $db->exec_query('batch', ['*'], '', '', '', '', "variety_id = {$silo['variety_id']}", '', false);
//     $batch = $batch_res->fetch_assoc();



//     $data = $data . "
    
//     <table>
//         <tr> {$silo['name']} </tr>
//         <tr>
//             <td><input type='text' class='table_input' value='BA {$bins_trans['batch_id']}' readonly /></td>
//             <td><input type='text' class='table_input' value='{$variety['name']}' readonly /></td>
//             <td><input type='text' class='table_input' value='{$bins_trans['amount']}' readonly /></td>
//             <td><input type='text' class='table_input' value='{$bins_trans['date']}' readonly /></td>
//             <td><input type='text' class='table_input' value='{$silo['name']}' readonly /></td>
//         </tr>
//     </table?
//     ";

// }




$batches = $db->exec_query('batches', ['*'], '', '', '', 'order by record_id DESC', "status = 'COMPLETE'", '', false);
if ($batches->num_rows > 0) {
    while ($batch = $batches->fetch_assoc()) {
        $bins_trans_res = $db->exec_query('bins_trans', ['*'], '', '', '', '', "silo_id <> 0", 'order by record_id desc', false);
        if ($bins_trans_res->num_rows > 0) {
            while ($bins_trans = $bins_trans_res->fetch_assoc()) {

                $variety_res = $db->exec_query('variety', ['*'], '', '', '', '', "record_id = {$bins_trans['variety_id']}", '', false);
                $variety = $variety_res->fetch_assoc();

                $silo_res = $db->exec_query('silos', ['*'], '', '', '', '', "record_id = {$bins_trans['silo_id']}", '', false);
                $silo = $silo_res->fetch_assoc();

                $silo_data = $silo_data . "
                <tr>
                    <td><input type='text' class='table_input' value='BA {$bins_trans['batch_id']}' readonly /></td>
                    <td><input type='text' class='table_input' value='{$variety['name']}' readonly /></td>
                    <td><input type='text' class='table_input' value='{$bins_trans['amount']}' readonly /></td>
                    <td><input type='text' class='table_input' value='{$bins_trans['date']}' readonly /></td>
                    <td><input type='text' class='table_input' value='{$silo['name']}' readonly /></td>
                </tr>
            ";
            }
        } else {
            $silo_data = $silo_data . "
            <tr>
                <td> NO RESULTS</td>
            </tr>";
        }
    }

} else {
    $silo_data = "
    <tr>
        <td> NO RESULTS</td>
    </tr>";
}

//GET THE TOTAL NUMBER OF SILOS
$silo_count_res = $db->exec_query('silos', ['*'], '', '', '', '', 1, '', false);
$silo_count = $silo_count_res->num_rows;

$silo_table = $silo_table . "
    <table style='width:100%; text-align:center;'>
        <tr style='font-size: 1.3vw;'>
            <th>BATCH ID</th>
            <th>VARIETY</th>
            <th>AMOUNT</th>
            <th>DATE</th>
            <th>SILO</th>
        <tr>
        $silo_data 
    </table>
    ";

// create a table of each and every silo
// for ($i = 1; $i <= $silo_count; $i++) {
//     $silo_table;
// }

?>

<!-- <div class='form' style="padding-bottom: 0%;width: 90%;">

    <h2 style="font-size: 2vw;">CURRENT BATCHES IN SILO </h2>
    <hr><br>

    <table style='width:100%; text-align:center;'>
        <tr style="font-size: 1.3vw;">
            <th>BATCH ID</th>
            <th>VARIETY</th>
            <th>AMOUNT</th>
            <th>DATE</th>
            <th>SILO</th>
        </tr>
        <php echo $table_data; ?>
    </table>
    <br>
</div>

<div class="form" style="padding-bottom: 0%;width: 90%;">
    <br>
    <php echo $silo_table; ?>
</div>

<br><br><br> -->

<div id="table-container"></div>
<br><br>

<script>
    //create a 30 second timer to refresh the page
    setInterval(function () {
        location.reload();
    }, 10000);

    function table() {
        var i = 1;
        var newDiv = document.createElement("div");
        newDiv.id = "silo_container";
        newDiv.className = "form";
        newDiv.style.width = '90%';
        newDiv.style.paddingBottom = '0%';

        var table = document.createElement("table");
        table.id = "silo_table_" + i;
        table.style.width = "100%";
        table.style.textAlign = "center";
        table.style.fontSize = "1.3vw";

        var table_body = document.createElement("tbody");

        // var table_data = document.createElement("tr");
        const tablesHTML = `<?php echo $silo_table; ?>`;
        table_body.innerHTML = tablesHTML;
        table.appendChild(table_body);

        newDiv.appendChild(table);

        // form.appendChild(table);

        document.getElementById("table-container").appendChild(newDiv);
    }

    onload = table();
</script>

<!-- <div id="table-container"></div>

<script>

function table() {
    const newDiv = document.createElement("div");
    newDiv.id = "silo_container";
    newDiv.className = "form";
    newDiv.style.width = '90%';
    newDiv.style.paddingBottom = '0%';

    const table = document.createElement("table");
    table.id = "silo_table_1"; // You can add logic to increment if needed
    table.style.width = "100%";
    table.style.textAlign = "center";
    table.style.fontSize = "1.3vw";

    const table_header = document.createElement("tr");
    const table_headings = ["BATCH ID", "VARIETY", "AMOUNT", "DATE", "SILO"];
    table_headings.forEach(text => {
        const th = document.createElement("th");
        th.textContent = text;
        table_header.appendChild(th);
    });
    table.appendChild(table_header);

    // Add example data row
    var row = document.createElement("tr");
    var cell = document.createElement("td");

    for (var i = 0; i < 5; i++) {
        var cell = document.createElement("td");
        var input = document.createElement("input");
        input.type = "text";
        input.readOnly = true;
        // input.innerHTML = <php echo $data; ?>;
        const data = <?php echo $data; ?>;
        
        data.forEach(text => {
        const td = document.createElement("td");
        td.textContent = text;
        table_data.appendChild(td);
        });

        cell.appendChild(input);
        row.appendChild(cell);
    }
    table.appendChild(row);

    // const table_data = document.createElement("tr");
    // const data = <php echo $silo_data; ?>;
    // table_data.innerHTML = data;
    // table.appendChild(table_data);

    newDiv.appendChild(table);
}

onload = table();
</script> -->