<?php include "../../root.class.php";
$html = new html();
$db = new db_safeguard();
$html->add_styles_page();
// $html->check_user_type("ADMIN");

$user_id = new input();
$user_id->class("inputs");
$user_id->style("display: none;");
$user_id->name("user_id");
$user_id->id("user_id");
$user_id->value($_SESSION['user_id']);

$drying_bins = $db->query("batches", "SELECT * FROM batches WHERE status = 'DRYING BINS' ORDER BY date DESC");

if ($drying_bins->num_rows > 0) {
    while ($drying = $drying_bins->fetch_assoc()) {
        $table_data = $table_data . "<tr>
            <td>{$drying['batch_id']}</td>
            <td>{$drying['variety_id']}</td>
            <td>{$drying['date']}</td>
            <td>{$drying['moisture']}%</td>

            <td>
            <button 
            class='submit_btn' 
            id='submit_btn_{$drying['record_id']}' 
            batch-id='{$drying['batch_id']}'
            style='width: 95%;margin:1vw auto;'>
            MOVE TO SILO
            </button>
            </td>
            </tr>
        ";
    }
} else {
    $table_data = "<tr>
        <td>  </td>
        <td>  </td>
        <td> NO RESULTS</td>
        <td>  </td>
        <td>  </td>
    </tr>";
}

$drying_bins_ajax = new js_ajax();
$drying_bins_ajax->function_name("drying_bins");
$drying_bins_ajax->submit_btn_id("submit_btn");
$drying_bins_ajax->redirect('../bins/bins.php?batch_id=' . $drying['batch_id']);
$drying_bins_ajax->on_success("DEHUSKING");

// echo $drying_bins; drying_bins

?>

<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>DRYING BINS</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>

            </td>
        </tr>

        <?php echo $table_data; ?>
    </table>
    <br>
</div>

<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/bins.php?batch_id=' + batchId;
            });
        });
    });
</script>