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

$allocate_main_res = $db->query("maintenance_request", "SELECT * FROM maintenance_request");

if ($allocate_main_res->num_rows > 0) {
    while ($allocate_main = $allocate_main_res->fetch_assoc()) {
        $table_data = $table_data . "
        <tr>
            <td>" . $allocate_main['jobcard_id'] . "</td>
            <td>" . $allocate_main['date_requested'] . "</td>
            <td>" . $allocate_main['requested_by'] . "</td>
            <td>" . $allocate_main['impact_on_production'] . "</td>
            <td>" . $allocate_main['priority_level'] . "</td>
            <td>" . $allocate_main['status'] . "</td>
            <td>
                <button 
                class='submit_btn' 
                id='submit_btn_{$allocate_main['jobcard_id']}' 
                jobcard_id='{$allocate_main['jobcard_id']}'
                style='width: 95%;margin:1vw auto;'>
                ALLOCATE
                </button>
            </td>
        </tr>
        ";
    }
} else {
    $table_data = $table_data . "
    <tr>
        <td></td>
        <td></td>
        <td>NO DATA FOUND</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-left: 10px;
        padding-right: 10px;
        border: 3px solid black;
        text-align: left;
        background-color: white;
    }
</style>
<div class="form_down">
    <h1>MAINTENANCE SIGN OFF</h1>
    <table style='width:90%; text-align:center;'>
        <tr>
            <td style='font-size:1.5vw;'>
                JOBCARD ID:
            </td>
            <td style='font-size:1.5vw;'>
                DATE REQUESTED:
            </td>
            <td style='font-size:1.5vw;'>
                REQUESTED BY:
            </td>
            <td style='font-size:1.5vw;'>
                IMPACT ON PRODUCTION:
            </td>
            <td style='font-size:1.5vw;'>
                PRIORITY LEVEL:
            </td>
            <td>
                STATUS:
            </td>
            <td>

            </td>
        </tr>

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

<script>
    document.addEventListener("DOMContentLoaded", function () {
        document.querySelectorAll(".submit_btn").forEach(function (btn) {
            btn.addEventListener("click", function () {
                let jobcard_id = this.getAttribute("jobcard_id");
                window.location.href = '../maintenance/allocate_maintenance.php?jobcard_id=' + jobcard_id;
            });
        });
    });
</script>