<?php include "../../root.class.php";
$html = new html();
$db = new db_safeguard();
$html->add_styles_page();
// $html->check_user_type("ADMIN");

// HTML FORM ELEMENTS

$batch_res = $db->query("batches", "SELECT * FROM batches WHERE status = 'BATCH RECEIVED' AND sound_not_sound = 'YES' AND shell = 'NO' ORDER BY record_id DESC");

echo "SELECT * FROM batches WHERE status = 'BATCH RECEIVED' AND sound_not_sound = 'YES' AND shell = 'NO' ORDER BY record_id DESC";

if ($batch_res->num_rows >= 1) {
    while ($batch_info = $batch_res->fetch_assoc()) {

        $variety_res = $db->query("variety", "SELECT * FROM variety WHERE record_id ={$batch_info['variety_id']}");
        // echo "SELECT * FROM variety WHERE record_id ={$batch_info['variety_id']}";
        $variety_info = $variety_res->fetch_assoc();

        $table_data = $table_data . "    
            <tr>
                <td>{$batch_info['batch_id']}</td>
                <td>{$variety_info['name']}</td>
                <td>{$batch_info['moisture']} %</td>
                <td>{$batch_info['date']}</td>
                <td>{$batch_info['size']} mm</td>
                <td>{$batch_info['weight']} kg</td>
                <td>
                <button class='submit_btn' id='submit_btn' style='width: 95%;margin:1vw auto;' batch-id='{$batch_info['batch_id']}'>PROCESS
                </button></td>
            </tr>
        ";
    }
} else {
    $table_data = "<tr>
        <td>  </td>
        <td>  </td>
        <td> NO RESULTS </td>
        <td>  </td>
        <td>  </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: 10px;
        border: 3px solid black;
        text-align: left;
        background-color: white;
    }
</style>
<br>
<div class="form_down">
    <h1>MOVE TO DRYING BIN</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;'>
                MOISTURE:
            </td>
            <td style='font-size:1.5vw;'>
                DATE RECEIVED:
            </td>
            <td style='font-size:1.5vw;'>
                SIZE:
            </td>
            <td style='font-size:1.5vw;'>
                WEIGHT:
            </td>
            <td style='font-size:1.5vw;'>
                
            </td>
        </tr>

        <?php echo $table_data; ?>
    </table>

    <br><br>
</div>

<br><br>


<script>
    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/drying_bins.php?batch_id=' + batchId;
            });
        });
    });
</script>

<?php

$check_batch_ajax = new js_ajax();
$check_batch_ajax->function_name("check_batch");
$check_batch_ajax->submit_btn_id("submit_btn");
$check_batch_ajax->redirect('../bins/drying_bins.php?batch_id=' . $batch_info['batch_id']);
$check_batch_ajax->on_success("CHECKING BATCH");