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

$dehusking = $db->query("batches", "SELECT * FROM batches WHERE status = 'DEHUSK' ORDER BY date DESC");

if ($dehusking->num_rows > 0) {
    while ($dehusk = $dehusking->fetch_assoc()) {

        $variety_res = $db->query("variety", "SELECT * FROM variety WHERE record_id = '{$dehusk['variety_id']}'");
        $variety_data = $variety_res->fetch_assoc();

        $table_data .= "
        <tr>
            <td>BA {$dehusk['batch_id']}</td>
            <td>{$variety_data['name']}</td>
            <td>{$dehusk['date']}</td>
            <td>{$dehusk['moisture']}%</td>
            <td>{$dehusk['weight']}</td>
            <td>
                <button 
                    class='submit_btn' 
                    id='submit_btn_{$dehusk['record_id']}'
                    batch-id='{$dehusk['record_id']}'
                    style='width: 95%;margin:1vw auto;'>
                    DEHUSK
                </button>
            </td> 
        </tr>
        ";
    }
} else {
    $table_data = "<tr>
        <td>  </td>
        <td>  </td>
        <td> NO RESULTS</td>
        <td>  </td>
        <td>  </td>
        <td>  </td>
    </tr>";
}
$dehusking_ajax = new js_ajax();
$dehusking_ajax->function_name("dehusking");
$dehusking_ajax->submit_btn_id("submit_btn");
$dehusking_ajax->redirect('../dehusk/dehusking.php?batch_id=' . $dehusk['record_id']);
$dehusking_ajax->on_success("DEHUSKING");


?>

<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>DEHUSK</h1>
    <table style='width:90%; text-align:center;'>
        <tr>
            <th style='font-size:1.5vw;'>
                BATCH ID:
            </th>
            <th style='font-size:1.5vw;'>
                VARIETY:
            </th>
            <th style='font-size:1.5vw;'>
                DATE RECEIVED:
            </th>
            <th style='font-size:1.5vw;'>
                MOISTURE:
            </th>
            <th style='font-size:1.5vw;'>
                WEIGHT:
            </th>
            <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 = '../dehusk/dehusking.php?batch_id=' + batchId;
            });
        });
    });
</script>