<?php include "../../root.class.php";
$html = new html();
$html->add_styles_page();
$db = new db_safeguard();

$current_date_time = date("Y-m-d");


$all_jobcards = $db->query("jobcards", "SELECT * FROM jobcards WHERE status = 0 ORDER BY team_assigned_id DESC");
// echo "SELECT * FROM jobcards WHERE status = 0 ORDER BY team_assigned_id DESC";

?>
<br>
<h1>CURRENT ACTIVE JOBCARDS</h1>

<h2><?php echo $teams['name']; ?></h2>
<?php
while ($jobcards = $all_jobcards->fetch_assoc()) {

    $username_res = $db->query("users", "SELECT * FROM users WHERE record_id = '$jobcards[user_id]' AND username != 'DEV'");

    $team_name_res = $db->query("teams", "SELECT * FROM teams WHERE record_id = '$jobcards[team_assigned_id]' AND status = 'ACTIVE'");

    ?>
    <div
        style="display: flex; flex-direction: row; flex-wrap: wrap; justify-content: space-around; align-content: space-around;">
        <div class="searched_data">

            <div class="group_div">
                <label class="label" for="jobcard_id">JOBCARD ID</label>
                <textarea type="text" class="inputs"><?php echo $jobcards['jc_no']; ?></textarea>
            </div>
            <div class="group_div">
                <label class="label" for="date_time">DATE CREATED</label>
                <textarea type="text" class="inputs"><?php echo $jobcards['date_created']; ?></textarea>
            </div>
            <div class="group_div">
                <label class="label" for="date_time">ACTION DATE</label>
                <textarea type="text" class="inputs"><?php echo $jobcards['action_date']; ?></textarea>
            </div>
            <div class="group_div">
                <label class="label" for="date_time">ADDRESS</label>
                <textarea type="text" class="inputs"><?php echo $jobcards['address']; ?></textarea>
            </div>
            <?php if ($username = $username_res->fetch_assoc()) {
                ?>
                <div class="group_div">
                    <label class="label" for="user">USER</label>
                    <textarea type="text" class="inputs"><?php echo $username['username']; ?></textarea>
                </div>
                <?php
            }


            $jobcard_timeline = $db->query("jobcard_timeline", "SELECT * FROM jobcard_timeline ORDER BY record_id DESC");
            // echo "SELECT * FROM jobcard_timeline ORDER BY record_id DESC";
        
            if ($timeline = $jobcard_timeline->fetch_assoc()) {
                ?>
                <div class="group_div">
                    <label class="label" for="type">LAST ACTION TIME</label>
                    <textarea type="text" class="inputs"><?php echo $timeline['date_time']; ?></textarea>
                </div>
                <?php
                if ($timeline['type'] == 'REIMING_STOP' || $timeline['type'] == 'DRILLING_STOP' || $timeline['type'] == 'BLASTING_STOP' || $timeline['type'] == 'CASING_STOP') {
                    ?>
                    <div class="group_div">
                        <label class="label" for="type">CURRENT ACTION</label>
                        <textarea type="text" class="inputs"><?php echo $timeline['type']; ?></textarea>
                    </div>
                    <?php
                } elseif ($timeline['type'] == 'REIMING_START' || $timeline['type'] == 'DRILLING_START' || $timeline['type'] == 'BLASTING_START' || $timeline['type'] == 'CASING_START') {
                    ?>
                    <div class="group_div">
                        <label class="label" for="type">CURRENT ACTION</label>
                        <textarea type="text" class="inputs"><?php echo $timeline['type']; ?></textarea>
                    </div>
                    <?php
                } elseif ($timeline['type'] == 'ARRIVED' || $timeline['type'] == 'DEPARTED') {
                    ?>
                    <div class="group_div">
                        <label class="label" for="type">CURRENT ACTION</label>
                        <textarea type="text" class="inputs"><?php echo $timeline['type']; ?></textarea>
                    </div>
                    <?php
                } else {
                    ?>
                    <div class="group_div">
                        <label class="label" for="type">CURRENT ACTION</label>
                        <textarea type="text" class="inputs">NOT STARTED </textarea>
                    </div>
                    <?php
                }
            }

            if ($team_name = $team_name_res->fetch_assoc()) {
                ?>
                <div class="group_div">
                    <label class="label" for="team">TEAM</label>
                    <textarea type="text" class="inputs"><?php echo $team_name['name']; ?></textarea>
                </div>
                <?php
            } ?>

            <div class="group_div">
                <label class="label" for="type">STATUS</label>
                <?php
                if ($jobcards['status'] == 0) {
                    ?>
                    <textarea type="text" class="inputs">PENDING</textarea>

                    <?php

                } else {
                    ?>
                    <textarea type="text" class="inputs">COMPLETED</textarea>

                    <?php
                }
                ?>
            </div>
        </div>
    </div>

    <script>

        //auto refresh every 1 minute
        setTimeout(function () {
            location.reload();
        }, 60000);

    </script>
    <?php
}
?>