<?php include "../../root.class.php";
$html = new html();
$html->add_styles_page();
// $html->check_user_type("ADMIN");
$db = new db_safeguard();

?>

<style>
    .summary_popup {
        display: none;
        position: fixed;
        z-index: 1000;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        background-color: rgba(0, 0, 0, 0.5);
    }

    .summary_content {
        background: linear-gradient(90deg, #110151, #3331e7);
        margin: 3% auto;
        padding: 20px;
        border-radius: 10px;
        width: 80%;
        height: 85%;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
        position: relative;
        overflow-y: scroll;
    }

    .close-btn {
        position: absolute;
        top: 10px;
        right: 15px;
        font-weight: bolder;
        font-size: 5vw;
        cursor: pointer;
        color: white;
    }

    table {
        width: 95%;
        border-collapse: collapse;
    }

    th,
    td {
        padding: 10px;
        border: 3px solid black;
        text-align: left;
        background-color: white;
    }
</style>

<div
    style="display: flex; flex-direction: column; align-items: center; background-color: white; margin: 4vw; border-radius: 2vw;">
    <?php
    $res_data = $db->query("jobcard_timeline", "SELECT * FROM jobcard_timeline WHERE jobcard_id = {$_GET['record_id']}  ORDER BY record_id ASC");

    if ($res_data->num_rows > 0) {

        $jobcard_res = $db->query("jobcards", "SELECT * FROM jobcards WHERE record_id = {$_GET['record_id']}");
        $jobcard = $jobcard_res->fetch_assoc();

        // echo $jobcard['record_id'];
    
        ?>

        <h1>JOBCARD SUMMARY</h1>

        <table
            style='border-radius: 2vw;padding-top: 2vw;padding-bottom: 2vw;width:60%; text-align:center; border: 2px solid black;background-color: #ffffffc4;'>
            <tr>
                <th>TYPE</th>
                <th>METERS</th>
                <th>DATE</th>
            </tr>
            <?php
            $riem_meters = 0;
            $drilling_meters = 0;
            $casing_meters = 0;
            $blasting_meters = 0;

            while ($jc_timeline = $res_data->fetch_assoc()) {

                if ($jc_timeline['type'] == "RIEM_STOP" || $jc_timeline['type'] == "RIEMING_STOP") {
                    $riem_meters += $jc_timeline['meters'];
                }

                if ($jc_timeline['type'] == "DRILLING_STOP") {
                    $drilling_meters += $jc_timeline['meters'];
                }

                if ($jc_timeline['type'] == "CASING_STOP") {
                    $casing_meters += $jc_timeline['meters'];
                }

                if ($jc_timeline['type'] == "BLASTING_START" || $jc_timeline['type'] == "BLASTING_STOP") {
                    $blasting_meters += $jc_timeline['meters'];
                }

                ?>
                <tr>
                    <td> <?php echo $jc_timeline['type'] ?> </td>
                    <td> <?php echo $jc_timeline['meters'] ?> </td>
                    <td> <?php echo $jc_timeline['date_time'] ?> </td>
                </tr>
                <?php
            }
            ?>
        </table><br><br>

        <table style="width:60%;">
            <tr>
                <td> </td>
                <td> TOTAL </td>
            </tr>
            <tr>
                <td> TOTAL RIEM METERS </td>
                <td> <?php echo $riem_meters ?> </td>
            </tr>
            <tr>
                <td> TOTAL DRILLING METERS </td>
                <td> <?php echo $drilling_meters ?> </td>
            </tr>
            <tr>
                <td> TOTAL CASING METERS </td>
                <td> <?php echo $casing_meters ?> </td>
            </tr>
            <tr>
                <td> TOTAL BLASTING METERS</td>
                <td> <?php echo $blasting_meters ?></td>
            </tr>
        </table>

        <br><br>

    </div>

    <div
        style="display: flex; flex-direction: column; align-items: center; background-color: white; margin: 4vw; border-radius: 2vw; padding: 2vw;">
        <table
            style='border-radius: 2vw;padding-top: 2vw;padding-bottom: 2vw;width:60%; text-align:center; border: 2px solid black;background-color: #ffffffc4;'>
            <tr>
                <th></th>
                <th>VALUE</th>
            </tr>
            <tr>
                <td>Interested in Pump: </td>
                <td><?php echo $jobcard['Interested_in_pump'] == 1 ? "YES" : "NO"; ?></td>
            </tr>
            <tr>
                <td>Water Flow: </td>
                <td> <?php echo $jobcard['water_flow']; ?> </td>
            </tr>
            <tr>
                <td>Compressor Hours: </td>
                <td> <?php echo $jobcard['compressor_hours'] ?> </td>
            </tr>
            <tr>
                <td>Diesel Start: </td>
                <td> <?php echo $jobcard['diesel_start'] ?> </td>
            </tr>
            <tr>
                <td>Diesel Stop: </td>
                <td> <?php echo $jobcard['diesel_stop'] ?> </td>
            </tr>

            </tr>
        </table>
    </div>

    <div
        style="display: flex; flex-direction: column; align-items: center; background-color: white; margin: 4vw; border-radius: 2vw; padding: 2vw;">

        <script src="https://cdn.jsdelivr.net/npm/signature_pad@2.3.2/dist/signature_pad.min.js"></script>
        <?php

        if (file_exists("../jobcards/signatures/employee-jc" . $record['jc_no'] . "-" . $_SESSION['user_id'] . "-signature.png")) {

            ?>
            <div class='jobcard_work_signature' style='margin-bottom:1vw;'>
                <h2>EMPLOYEE SIGNATURE</h2>
                <input type='image'
                    src='../jobcards/signatures/employee-jc<?php echo $record['jc_no']; ?>-<?php echo $_SESSION['user_id']; ?>-signature.png'
                    alt='employee signature' class="signature_image">
            </div>
            <?php
        }


        if (file_exists("../jobcards/signatures/client-jc" . $record['jc_no'] . "-" . $_SESSION['user_id'] . "-signature.png")) {
            ?>

            <div class='jobcard_work_signature' style="margin-bottom:1vw;">
                <h2>CLIENT SIGNATURE</h2>

                <input type="image"
                    src="../jobcards/signatures/client-jc<?php echo $record['jc_no']; ?>-<?php echo $_SESSION['user_id']; ?>-signature.png"
                    alt="client signature" class="signature_image">
            </div>
            <?php
        }
        ?>

        <br>
        <?php
        $done_btn = new button();
        $done_btn->value("DONE");
        $done_btn->onclick("done()");
        $done_btn->add();
        ?>
    </div>

    <?php

    } else {
        ?>
    <table
        style='border-radius: 2vw;padding-top: 2vw;padding-bottom: 2vw;width:50%; text-align:center; border: 2px solid black;background-color: #ffffffc4;'>
        <tr>
            <th>NAME</th>
            <th>VALUE</th>
        </tr>
        <tr>
            <td></td>
            <td> DATA NOT FOUND </td>
            <td></td>
        </tr>

    </table>

    <?php
    }
    ?>

<?php

$done_ajax = new js_ajax();
$done_ajax->function_name("done");
$done_ajax->on_success("DONE");
$done_ajax->redirect('../home.php');

?>