<?php include "../../root.class.php";
$html = new html();
$html->add_styles_page();
$db = new db_safeguard();
$work_order_res = $db->query("work_orders", "SELECT * FROM `work_orders` WHERE `inprogress` = 1 AND `completed` = 0 AND `sent_for_review` = 0 AND `in_review` = 0 AND `user_id_assigned` = {$_SESSION['user_id']}");
if ($work_order_res->num_rows > 0) {
    $work_order = $work_order_res->fetch_assoc();
    echo "<script>window.location.href = 'work_order.php?record_id=" . $work_order['record_id'] . "';</script>";
} else {

    ?>

    <div class="search_form">
        <h1> ACTIVE WORK ORDERS</h1>
        <?php
        echo $html->input_html('', '0', 'completed', 'completed', '', '', '', 'text', 'hidden');
        echo $html->input_html('', '0', 'in_review', 'in_review', '', '', '', 'text', 'hidden');
        echo $html->input_html('', '0', 'sent_for_review', 'in_review', '', '', '', 'text', 'hidden');
        echo $html->input_html('', $_SESSION['user_id'], 'user_id_assigned', 'user_id_assigned', '', '', '', 'text', 'hidden');
        ?>
        <button id="submit" hidden></button>
    </div>

    <?php
    echo $html->html_form_ajax("search_active_work_orders", "SELECT", "work_orders", '', false, ['project_id', 'name'], "work_order.php"); ?>
    <script>
        search_active_work_orders();
    </script>
<?php } ?>