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

$jobcards_res = $db->query("maintenance_allocation", "SELECT * FROM maintenance_allocation ");

if ($jobcards_res->num_rows > 0) {
    while ($jobcards = $jobcards_res->fetch_assoc()) {
        $table_data = $table_data . "
        <tr>
            <td>" . $jobcards['jobcard_id'] . "</td>
            <td>" . $jobcards['date_time_created'] . "</td>
            <td>" . $jobcards['maintenance_type'] . "</td>
            <td>" . $jobcards['spare_parts'] . "</td>
            <td>" . $jobcards['special_tools'] . "</td>
            <td>" . $jobcards['technician_assigned'] . "</td>
            <td>
                <button 
                class='submit_btn' 
                id='submit_btn_{$jobcards['jobcard_id']}' 
                jobcard_id='{$jobcards['jobcard_id']}'
                style='width: 95%;margin:1vw auto;'>
                JOBCARD
                </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 JOBCARDS</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 CREATED:
            </td>
            <td style='font-size:1.5vw;'>
                MAINTENANCE TYPE:
            </td>
            <td style='font-size:1.5vw;'>
                SPARE PARTS:
            </td>
            <td style='font-size:1.5vw;'>
                SPECIAL TOOLS:
            </td>
            <td>
                TECHNICIAN ASSIGNED:
            </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/maintenance_jobcards.php?jobcard_id=' + jobcard_id;
            });
        });
    });
</script>