<?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:70%; 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" || $jc_timeline['type'] == "RIEMING_PAUSED") {
                    $riem_meters += $jc_timeline['meters'];
                }

                if ($jc_timeline['type'] == "DRILLING_STOP" || $jc_timeline['type'] == "DRILLING_PAUSE") {
                    $drilling_meters += $jc_timeline['meters'];
                }

                if ($jc_timeline['type'] == "CASING_STOP" || $jc_timeline['type'] == "CASING_PAUSE") {
                    $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>

    </div>

    <div
        style="display: flex; flex-direction: column; align-items: center; background-color: white; margin: 4vw; border-radius: 2vw; padding: 2vw;">

        <table style="width:60%;">
            <tr>
                <th> TOTALS </th>
                <th> METERS </th>
            </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>
            <tr>
                <td> TOTAL </td>
                <td> <?php echo $riem_meters + $drilling_meters + $casing_meters + $blasting_meters ?> </td>
            </tr>
        </table>

        <br><br>

        <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>
                <td>Water Strike: </td>
                <td> <?php echo $jobcard['water_strike'] ?> </td>
            </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

        $jobcard_data_res = $db->query("jobcards", "SELECT * FROM jobcards WHERE record_id = {$_GET['record_id']}");
        $jobcard_data = $jobcard_data_res->fetch_assoc();

        // echo "../jobcards/signatures/employee-jc" . $jobcard_data['jc_no'] . "-" . $_SESSION['user_id'] . "-signature.png";
        if (file_exists("../jobcards/signatures/employee-jc" . $jobcard_data['jc_no'] . "-" . $_SESSION['user_id'] . "-signature.png")) {

            ?>
            <div class='jobcard_work_signature'
                style='margin-bottom:1vw; display:flex; flex-direction:column; align-items:center;'>
                <h2>EMPLOYEE SIGNATURE</h2>
                <input type='image'
                    src='../jobcards/signatures/employee-jc<?php echo $jobcard_data['jc_no']; ?>-<?php echo $_SESSION['user_id']; ?>-signature.png'
                    alt='employee signature' class="signature_image"
                    style="width: 30vw;  height:auto; border:2px solid black; border-radius:1vw;">
            </div>
            <?php
        } else {
            ?>
            <div class='jobcard_work_signature'
                style='margin-bottom:1vw; display:flex; flex-direction:column; align-items:center;'>
                <h2>EMPLOYEE SIGNATURE</h2>
                <div
                    style="width:30vw; height:20vw; border:2px solid black; border-radius:1vw; display:flex; justify-content:center; align-items:center; background-color:#f0f0f0;">
                    <span style="color:#888888; font-size:1.5vw;">NO SIGNATURE FOUND</span>
                </div>
                <?php
        }

        // echo "<br><br>";
    
        // echo "../jobcards/signatures/client-jc" . $jobcard_data['jc_no'] . "-" . $_SESSION['user_id'] . "-signature.png";
        if (file_exists("../jobcards/signatures/client-jc" . $jobcard_data['jc_no'] . "-" . $_SESSION['user_id'] . "-signature.png")) {
            ?>

                <div class='jobcard_work_signature'
                    style="margin-bottom:1vw; display:flex; flex-direction:column; align-items:center;">
                    <h2>CLIENT SIGNATURE</h2>

                    <input type="image"
                        src="../jobcards/signatures/client-jc<?php echo $jobcard_data['jc_no']; ?>-<?php echo $_SESSION['user_id']; ?>-signature.png"
                        alt="client signature" class="signature_image"
                        style="width: 30vw;  height:auto; border:2px solid black; border-radius:1vw;">
                </div>
                <?php
        } else {
            ?>
                <div class='jobcard_work_signature'
                    style="margin-bottom:1vw; display:flex; flex-direction:column; align-items:center;">
                    <h2>CLIENT SIGNATURE</h2>
                    <div
                        style="width:30vw; height:20vw; border:2px solid black; border-radius:1vw; display:flex; justify-content:center; align-items:center; background-color:#f0f0f0;">
                        <span style="color:#888888; font-size:1.5vw;">NO SIGNATURE FOUND</span>
                    </div>
                    <?php
        }
        ?>
            </div>

            <div
                style="display: flex; flex-direction: column; align-items: center; background-color: white; margin: 4vw; border-radius: 2vw; padding: 2vw;">
                <?php
                // echo "../jobcards/client_merch/". $jobcard_data['client_merch_image'];
                if (file_exists("../jobcards/client_merch/" . $jobcard_data['client_merch_image'])) {
                    ?>
                    <div style="display:flex; flex-direction:column; align-items:center; margin-top:2vw;">
                        <h2>CLIENT MERCHANDISE IMAGE</h2>
                        <input type="image" src="../jobcards/client_merch/<?php echo $jobcard_data['client_merch_image']; ?>"
                            alt="client merchandise image"
                            style="width:30vw; height:auto; border:2px solid black; border-radius:1vw;">
                    </div>
                    <?php

                }

                // echo "<br><br>";
            
                // echo "../jobcards/samples/". $jobcard_data['samples_image'];
                if (file_exists("../jobcards/samples/" . $jobcard_data['samples_image'])) {
                    ?>
                    <div style="display:flex; flex-direction:column; align-items:center; margin-top:2vw;">
                        <h2>SAMPLES IMAGE</h2>
                        <input type="image" src="../jobcards/samples/<?php echo $jobcard_data['samples_image']; ?>"
                            alt="samples image" style="width:30vw; height:auto; border:2px solid black; border-radius:1vw;">
                    </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("JOBCARD DONE");
        $done_ajax->redirect('../home.php');

        ?>