<?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 jc_no DESC");
// echo "SELECT * FROM jobcards WHERE status = 0 ORDER BY jc_no 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>
            <button class="submit_btn" onclick="detailed_view('<?php echo $jobcards['jc_no']; ?>')">MORE DETAILS</button>
        </div>
    </div>

    <div id="floating-block">
        <?php
        $last_meters = $db->query("jobcard_timeline", "SELECT * FROM jobcard_timeline WHERE jobcard_id={$jobcards['jc_no']}
        ORDER BY record_id DESC");

        // echo "SELECT * FROM jobcard_timeline WHERE jobcard_id={$_GET['record_id']} ORDER BY record_id DESC";
    
        $riem_meters = 0;
        $drilling_meters = 0;
        $casing_meters = 0;
        $blasting_meters = 0;

        while ($meters_data = $last_meters->fetch_assoc()) {
            if (
                $meters_data['type'] == "RIEM_STOP" || $meters_data['type'] == "RIEMING_STOP" || $meters_data['type'] ==
                "RIEMING_PAUSED"
            ) {
                $riem_meters += $meters_data['meters'];
            }

            if ($meters_data['type'] == "DRILLING_STOP" || $meters_data['type'] == "DRILLING_PAUSE") {
                $drilling_meters += $meters_data['meters'];
            }

            if ($meters_data['type'] == "CASING_STOP" || $meters_data['type'] == "CASING_PAUSE") {
                $casing_meters += $meters_data['meters'];
            }

            if ($meters_data['type'] == "BLASTING_START" || $meters_data['type'] == "BLASTING_STOP") {
                $blasting_meters += $meters_data['meters'];
            }
        }

        ?>
        <div>
            <h3>CURRENT TOTAL METERS: </h3>
            <h3>TOTAL RIEMING: <?php echo $riem_meters; ?> METERS</h3>
            <h3>TOTAL DRILLING: <?php echo $drilling_meters; ?> METERS</h3>
            <h3>TOTAL CASING: <?php echo $casing_meters; ?> METERS</h3>
            <h3>TOTAL BLASTING: <?php echo $blasting_meters; ?> METERS</h3>
        </div>

    </div>

    <script>

        //auto refresh every 1 minute
        // setTimeout(function () {
        //     location.reload();
        // }, 60000);

        function detailed_view(jobcard_no) {
            var detailed_view = document.getElementById("detailed_view");
            var detailed_view_content = document.getElementById("detailed_view_content");
            detailed_view.style.display = "flex";
        }
        function close_detailed_view() {
            var detailed_view = document.getElementById("detailed_view");
            detailed_view.style.display = "none";
        }
    </script>
    <?php
}

?>

<style>
    .detailed_view {
        position: fixed;
        top: 2vw;
        left: 0;
        width: 99%;
        height: 95%;
        background-color: navy;
        display: flex;
        justify-content: center;
        align-items: center;
        z-index: 9999;
        border-radius: 1vw;
    }

    .detailed_view_content {
        background-color: whitesmoke;
        position: fixed;
        top: 4vw;
        left: 2vw;
        padding: 20px;
        width: 50%;
        height: 83%;
        border-radius: 10px;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
        overflow-y: scroll;
    }

    .last_action_div {
        background-color: whitesmoke;
        position: fixed;
        bottom: 3vw;
        right: 2vw;
        padding: 20px;
        width: 30%;
        height: 15%;
        border-radius: 10px;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
        /* overflow-y: scroll; */
    }

    .meters_div {
        background-color: whitesmoke;
        position: fixed;
        top: 4vw;
        right: 6vw;
        padding: 20px;
        width: 25%;
        height: 30%;
        border-radius: 10px;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
        /* overflow-y: scroll; */
    }

    .close-btn {
        position: absolute;
        top: 10px;
        right: 10px;
        cursor: pointer;
        font-size: 7vw;
    }

    table {
        width: 100%;
        border-collapse: collapse;
    }

    th,
    td {
        padding: 10px;
        border: 3px solid black;
        text-align: left;
        background-color: white;
    }
</style>

<div id="detailed_view" class="detailed_view" style="display: none;">
    <span class="close-btn" onclick="close_detailed_view()">&times;</span>

    <div id="detailed_view_content" class="detailed_view_content">

        <h2>JOB CARD TIMELINE</h2>
        <?php
        // $last_meters = $db->query("jobcard_timeline", "SELECT * FROM jobcard_timeline WHERE jobcard_id={$jobcards['jc_no']} ORDER BY record_id DESC");
        
        // echo "SELECT * FROM jobcard_timeline WHERE jobcard_id={$_GET['record_id']} ORDER BY record_id DESC";
        
        $riem_meters = 0;
        $drilling_meters = 0;
        $casing_meters = 0;
        $blasting_meters = 0;

        while ($meters_data = $last_meters->fetch_assoc()) {
            if (
                $meters_data['type'] == "RIEM_STOP" || $meters_data['type'] == "RIEMING_STOP" || $meters_data['type'] ==
                "RIEMING_PAUSED"
            ) {
                $riem_meters += $meters_data['meters'];
            }

            if ($meters_data['type'] == "DRILLING_STOP" || $meters_data['type'] == "DRILLING_PAUSE") {
                $drilling_meters += $meters_data['meters'];
            }

            if ($meters_data['type'] == "CASING_STOP" || $meters_data['type'] == "CASING_PAUSE") {
                $casing_meters += $meters_data['meters'];
            }

            if ($meters_data['type'] == "BLASTING_START" || $meters_data['type'] == "BLASTING_STOP") {
                $blasting_meters += $meters_data['meters'];
            }
        }

        ?>
        <table>
            <tr>
                <th>USER</th>
                <th>TYPE</th>
                <th>SERIAL NO</th>
                <th>DATE TIME</th>
                <th>METERS</th>
            </tr>
            <?php
            while ($timeline = $jobcard_timeline->fetch_assoc()) {

                $user_name_res = $db->query("users", "SELECT * FROM users WHERE record_id = '$timeline[user_id]'");
                $user_name = $user_name_res->fetch_assoc();
                ?>
                <tr>
                    <td><?php echo $user_name['username']; ?></td>
                    <td><?php echo $timeline['type']; ?></td>
                    <td><?php echo $timeline['serial_number']; ?></td>
                    <td><?php echo $timeline['date_time']; ?></td>
                    <td><?php echo $timeline['meters']; ?></td>
                </tr>
                <?php
            }
            ?>
        </table>
        <div>
            <h3>CURRENT TOTAL METERS: </h3>
            <h3>TOTAL RIEMING: <?php echo $riem_meters; ?> METERS</h3>
            <h3>TOTAL DRILLING: <?php echo $drilling_meters; ?> METERS</h3>
            <h3>TOTAL CASING: <?php echo $casing_meters; ?> METERS</h3>
            <h3>TOTAL BLASTING: <?php echo $blasting_meters; ?> METERS</h3>
        </div>
    </div>
    <div>

    </div>
    <div id="total_meters" class="meters_div">
        <?php
        // $last_meters = $db->query("jobcard_timeline", "SELECT * FROM jobcard_timeline WHERE jobcard_id={$jobcards['jc_no']} ORDER BY record_id DESC");
        
        // echo "SELECT * FROM jobcard_timeline WHERE jobcard_id={$_GET['record_id']} ORDER BY record_id DESC";
        
        $riem_meters = 0;
        $drilling_meters = 0;
        $casing_meters = 0;
        $blasting_meters = 0;

        while ($meters_data = $last_meters->fetch_assoc()) {
            if (
                $meters_data['type'] == "RIEM_STOP" || $meters_data['type'] == "RIEMING_STOP" || $meters_data['type'] ==
                "RIEMING_PAUSED"
            ) {
                $riem_meters += $meters_data['meters'];
            }

            if ($meters_data['type'] == "DRILLING_STOP" || $meters_data['type'] == "DRILLING_PAUSE") {
                $drilling_meters += $meters_data['meters'];
            }

            if ($meters_data['type'] == "CASING_STOP" || $meters_data['type'] == "CASING_PAUSE") {
                $casing_meters += $meters_data['meters'];
            }

            if ($meters_data['type'] == "BLASTING_START" || $meters_data['type'] == "BLASTING_STOP") {
                $blasting_meters += $meters_data['meters'];
            }
        }

        ?>
        <div>
            <h3>CURRENT TOTAL METERS: </h3>
            <h3>TOTAL RIEMING: <?php echo $riem_meters; ?> METERS</h3>
            <h3>TOTAL DRILLING: <?php echo $drilling_meters; ?> METERS</h3>
            <h3>TOTAL CASING: <?php echo $casing_meters; ?> METERS</h3>
            <h3>TOTAL BLASTING: <?php echo $blasting_meters; ?> METERS</h3>
        </div>
    </div>
    <div id="last_action" class="last_action_div">
        <?php
        // $last_action = $db->query("jobcard_timeline", "SELECT * FROM jobcard_timeline WHERE jobcard_id={$jobcards['jc_no']} ORDER BY record_id DESC LIMIT 1");
        // $last_action_data = $last_action->fetch_assoc();
        
        ?>
        <h2>LAST ACTION: <?php echo $last_action_data['type']; ?></h2>
    </div>
</div>