<?php include "../../root.class.php";
$html = new html();
$db = new db_safeguard();
$html->add_styles_page();
// $html->check_user_type("ADMIN");

//get all batches
$all_batches_res = $db->query("batches", "SELECT * FROM batches WHERE status != 'COMPLETE' AND status != 'LOADED BEFORE' AND sound_not_sound = 'YES' ORDER BY batch_id DESC");

echo "SELECT * FROM batches WHERE status != 'COMPLETE' AND status != 'LOADED BEFORE' ORDER BY batch_id DESC";
if ($all_batches_res->num_rows > 0) {
    while ($all_batches = $all_batches_res->fetch_assoc()) {

        if ($all_batches['status'] == "BATCH RECEIVED") {
            $table_data = $table_data . "<tr>
            <td>{$all_batches['batch_id']}</td>
            <td>{$all_batches['variety_id']}</td>
            <td>{$all_batches['date']}</td>
            <td>{$all_batches['moisture']}%</td>
            <td hidden id='current_status'>{$all_batches['status']}</td>
            <td>{$all_batches['weight']}</td>

            <td>
            <button 
            class='submit_btn' 
            id='submit_btn_{$all_batches['record_id']}' 
            batch-id='{$all_batches['record_id']}'
            style='width: 95%;margin:1vw auto;'>
            PROCESS
            </button>
            </td>
            </tr>
        ";
        } else if ($all_batches['status'] == "DEHUSKING") {
            $table_data = $table_data . "<tr>
            <td>BA {$all_batches['record_id']}</td>
            <td>{$all_batches['variety_id']}</td>
            <td>{$all_batches['date']}</td>
            <td>{$all_batches['moisture']}%</td>
            <td hidden id='current_status'>{$all_batches['status']}</td>
            <td>{$all_batches['weight']}</td>

            <td>
            <button 
            class='submit_btn' 
            id='submit_btn_{$all_batches['record_id']}' 
            batch-id='{$all_batches['record_id']}'
            style='width: 95%;margin:1vw auto;'>
            DEHUSK
            </button>
            </td>
            </tr>
        ";
        } else if ($all_batches['status'] == "DRYING BINS") {
            $table_data = $table_data . "<tr>
            <td>BA {$all_batches['record_id']}</td>
            <td>{$all_batches['variety_id']}</td>
            <td>{$all_batches['date']}</td>
            <td>{$all_batches['moisture']}%</td>
            <td hidden id='current_status'>{$all_batches['status']}</td>
            <td>{$all_batches['weight']}</td>
            

            <td>
            <button 
            class='submit_btn' 
            id='submit_btn_{$all_batches['record_id']}' 
            batch-id='{$all_batches['record_id']}'
            style='width: 95%;margin:1vw auto;'>
            MOVE TO SILO
            </button>
            </td>
            </tr>
        ";
        } else if ($all_batches['status'] == "SILO") {
            $table_data = $table_data . "<tr>
            <td>BA {$all_batches['record_id']}</td>
            <td>{$all_batches['variety_id']}</td>
            <td>{$all_batches['date']}</td>
            <td>{$all_batches['moisture']}%</td>
            <td hidden id='current_status'>{$all_batches['status']}</td>
            <td>{$all_batches['weight']}</td>

            <td>
            <button 
            class='submit_btn' 
            id='submit_btn_{$all_batches['record_id']}' 
            batch-id='{$all_batches['record_id']}'
            style='width: 95%;margin:1vw auto;'>
            MOVE TO CRACKING
            </button>
            </td>
            </tr>
        ";
        } else if ($all_batches['status'] == "SILO") {
            $table_data = $table_data . "<tr>
            <td>BA {$all_batches['record_id']}</td>
            <td>{$all_batches['variety_id']}</td>
            <td>{$all_batches['date']}</td>
            <td>{$all_batches['moisture']}%</td>
            <td hidden id='current_status'>{$all_batches['status']}</td>
            <td>{$all_batches['weight']}</td>

            <td>
            <button 
            class='submit_btn' 
            id='submit_btn_{$all_batches['record_id']}' 
            batch-id='{$all_batches['record_id']}'
            style='width: 95%;margin:1vw auto;'>
            MOVE TO SILO
            </button>
            </td>
            </tr>
            ";
        }
    }
} else {
    $table_data = "<tr>
        <td>  </td>
        <td>  </td>
        <td> NO BATCHES FOUND</td>
        <td>  </td>
        <td>  </td>
    </tr>";
}

?>

<style>
    body {
        background-image: url('');
        /* background-color: white; */
        background-repeat: repeat-y;
    }

    table {
        width: 95%;
        border-collapse: collapse;
    }

    th,
    td {
        padding-left: 10px;
        border: 3px solid black;
        text-align: left;
        background-color: white;
    }
</style>
<br>

<div class="form_down">
    <h1>CURRENT BATCHES IN TRANSITION</h1>
    <table style='width:90%; text-align:center;'>
        <tr>
            <td style='font-size:1.5vw;'>
                BATCH ID:
            </td>
            <td style='font-size:1.5vw;'>
                VARIETY:
            </td>
            <td style='font-size:1.5vw;'>
                DATE RECEIVED:
            </td>
            <td style='font-size:1.5vw;'>
                MOISTURE:
            </td>
            <td style='font-size:1.5vw;'>
                WEIGHT:
            </td>
            <td>

            </td>
        </tr>

        <?php echo $table_data; ?>
    </table>
    <br>
</div>

<script>
    var status = document.getElementById("current_status").innerText;
    // console.log("Current status: " + status);

    if (status === "BATCH RECEIVED") {
        document.addEventListener("DOMContentLoaded", function () {
            document.querySelectorAll(".submit_btn").forEach(function (btn) {
                btn.addEventListener("click", function () {
                    let batchId = this.getAttribute("batch-id");
                    window.location.href = '../receive/check.php?batch_id=' + batchId;
                });
            });
        });
    }

    if (status === "DEHUSKING") {
        document.addEventListener("DOMContentLoaded", function () {
            document.querySelectorAll(".submit_btn").forEach(function (btn) {
                btn.addEventListener("click", function () {
                    let batchId = this.getAttribute("batch-id");
                    window.location.href = '../dehusk/dehusking.php?batch_id=' + batchId;
                });
            });
        });
    }

    if (status === "DRYING BINS") {
        document.addEventListener("DOMContentLoaded", function () {
            document.querySelectorAll(".submit_btn").forEach(function (btn) {
                btn.addEventListener("click", function () {
                    let batchId = this.getAttribute("batch-id");
                    window.location.href = '../bins/bins.php?batch_id=' + batchId;
                });
            });
        });
    }

    if (status === "SILO") {
        document.addEventListener("DOMContentLoaded", function () {
            document.querySelectorAll(".submit_btn").forEach(function (btn) {
                btn.addEventListener("click", function () {
                    let batchId = this.getAttribute("batch-id");
                    window.location.href = '../silo/silo.php?batch_id=' + batchId;
                });
            });
        });
    }
</script>