<?php include "../../root.class.php";
$html = new html();
$html->add_styles_page();
// $html->check_user_type("ADMIN");
$db = new db_safeguard();

//GET THE NUMBER OF ROWS IN THE JOBCARDS TABLE AND ADD 1
if ($db->query("jobcards", "SELECT * FROM jobcards")->num_rows == 0) {
    $jobcard_no = 1;
} else {
    $jobcard_no = $db->query("jobcards", "SELECT * FROM jobcards")->num_rows + 1;
}

//get the current date and time
$current_date_time = date("Y-m-d H:i", strtotime("+2 hours"));

//get the seeion user id
$user_id = $_SESSION['user_id'];

$record_id = new input();
$record_id->name("record_id");
$record_id->id("record_id");
$record_id->value($_GET['record_id']);
$record_id->type("hidden");

$action_date = new input();
$action_date->class("inputs");
$action_date->type("datetime-local");
$action_date->name("action_date");
$action_date->id("action_date");
$action_date->value_from_db("jobcards", "action_date", "record_id = {$_GET['record_id']}");

$action_date_label = new label();
$action_date_label->for("action_date");
$action_date_label->value("ACTION DATE");

$contact_name = new input();
$contact_name->class("inputs");
$contact_name->type("text");
$contact_name->placeholder("CONTACT NAME");
$contact_name->name("contact_name");
$contact_name->id("contact_name");
$contact_name->value_from_db("jobcards", "contact_name", "record_id = {$_GET['record_id']}");

$address = new input();
$address->class("inputs");
$address->type("text");
$address->placeholder("ADDRESS");
$address->name("address");
$address->required();
$address->id("address");
$address->value_from_db("jobcards", "address", "record_id = {$_GET['record_id']}");

$phone = new input();
$phone->class("inputs");
$phone->type("text");
$phone->placeholder("PHONE NUMBER");
$phone->name("contact_number");
$phone->id("contact_number");
$phone->value_from_db("jobcards", "contact_number", "record_id = {$_GET['record_id']}");

$other_number = new input();
$other_number->class("inputs");
$other_number->type("text");
$other_number->placeholder("OTHER NUMBER");
$other_number->name("other_number");
$other_number->id("other_number");
$other_number->value_from_db("jobcards", "other_number", "record_id = {$_GET['record_id']}");

$contact_number_label = new label();
$contact_number_label->for("contact_number");
$contact_number_label->value("CONTACT NUMBER");

$other_number_label = new label();
$other_number_label->for("other_number");
$other_number_label->value("OTHER NUMBER");

$alternate_number_label = new label();
$alternate_number_label->for("alternate_number");
$alternate_number_label->value("ALTERNATE NUMBER");

$alternate_number = new input();
$alternate_number->class("inputs");
$alternate_number->type("text");
$alternate_number->placeholder("ALTERNATE NUMBER");
$alternate_number->name("alternate_number");
$alternate_number->id("alternate_number");
$alternate_number->value_from_db("jobcards", "alternate_number", "record_id = {$_GET['record_id']}");

$client_name = new select();
$client_name->class("inputs");
$client_name->name("client_id");
$client_name->id("client_id");
$client_name->required();
$client_name->fill_from_db("clients", "record_id", "name");

$contact_name_label = new label();
$contact_name_label->for("contact_name_label");
$contact_name_label->value("CONTACT NAME");

$date_time_closed = new input();
$date_time_closed->class("inputs");
$date_time_closed->style("display: none;");
$date_time_closed->name("date_time_closed");
$date_time_closed->value(0);
$date_time_closed->id("date_time_closed");

$jobcard_number_label = new label();
$jobcard_number_label->for("jc_no");
$jobcard_number_label->value("JOBCARD NUMBER");

$jobcard_number = new input();
$jobcard_number->class("inputs");
$jobcard_number->type("text");
$jobcard_number->readonly();
$jobcard_number->name("jc_no");
$jobcard_number->id("jc_no");
$jobcard_number->value_from_db("jobcards", "jc_no", "record_id = {$_GET['record_id']}");

$user_assigned = new select();
$user_assigned->class("inputs");
$user_assigned->name("user_assigned");
$user_assigned->required();
$user_assigned->id("user_assigned");
$user_assigned->fill_from_db("users", "record_id", "username");

$user_assigned_label = new label();
$user_assigned_label->for("user_assigned");
$user_assigned_label->value("USER ASSIGNED");

$team_assigned = new select();
$team_assigned->class("inputs");
$team_assigned->name("team_assigned_id");
$team_assigned->required();
$team_assigned->id("team_assigned_id");
$team_assigned->fill_from_db("teams", "record_id", "name");

$team_assigned_label = new label();
$team_assigned_label->for("team_assigned");
$team_assigned_label->value("TEAM ASSIGNED");

$client_name_label = new label();
$client_name_label->for("client_name");
$client_name_label->value("CLIENT NAME");

$submit_btn = new button();
$submit_btn->value("UPDATE");
$submit_btn->onclick("update_jobcard()");

?>

<div class="form_down">
    <h1>EDIT JOBCARD</h1>

    <script>
        function add_number() {
            //create a input with a placeholder called other
            var other = document.createElement("input");
            other.type = "text";
            other.placeholder = "OTHER";
            other.name = "other";
            other.id = "other";
            other.required = true;
            document.getElementById("other_div").appendChild(other);
        }
    </script>
    <?php
    $record_id->add();
    $jobcard_number_label->add();
    $jobcard_number->add();
    $action_date_label->add();
    $action_date->add();
    $client_name_label->add();
    $client_name->add();
    $contact_name_label->add();
    $contact_name->add();

    ?>
    <div style="display: flex; width: 45%; justify-content: space-between;">
        <?php
        $contact_number_label->add();
        $other_number_label->add();
        $alternate_number_label->add();
        ?>
    </div>
    <div>
        <?php
        $phone->add();
        $other_number->add();
        $alternate_number->add();
        ?>
    </div>

    <?php
    $address->add();
    $team_assigned_label->add();
    $team_assigned->add();
    $user_assigned_label->add();
    $user_assigned->add();

    ?>

    <?php
    $submit_btn->add();
    ?>

    <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; width: 80%;">
        <?php
        $res_data = $db->query("jobcard_timeline", "SELECT * FROM jobcard_timeline WHERE jobcard_id = {$lead_data['record_id']}  ORDER BY record_id ASC");

        if ($res_data->num_rows > 0) {

            $jobcard_res = $db->query("jobcards", "SELECT * FROM jobcards WHERE record_id = {$lead_data['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:80%;">
                <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;width: 80%;">
            <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; width: 80%;">

            <script src="https://cdn.jsdelivr.net/npm/signature_pad@2.3.2/dist/signature_pad.min.js"></script>
            <?php

            // EMPLOYEE SIGNATURE
            $employee_files = glob("signatures/employee-jc{$jc_no}-*-signature.png");

            if (!empty($employee_files)) {
                $employee_sig = $employee_files[0]; // first match
                ?>
                <div class='jobcard_work_signature' style='margin-bottom:1vw;'>
                    <h2>EMPLOYEE SIGNATURE</h2>
                    <input type='image' src='<?php echo $employee_sig; ?>' alt='employee signature' class="signature_image">
                </div>
                <?php
            }


            // CLIENT SIGNATURE
            $client_files = glob("signatures/client-jc{$jc_no}-*-signature.png");

            if (!empty($client_files)) {
                $client_sig = $client_files[0]; // first match
                ?>
                <div class='jobcard_work_signature' style='margin-bottom:1vw;'>
                    <h2>CLIENT SIGNATURE</h2>
                    <input type="image" src="<?php echo $client_sig; ?>" alt="client signature" class="signature_image">
                </div>
                <?php
            }

            ?>

            <br>
        </div>

        <?php

        } else {
            ?>
        <h1>Job Card Summary</h1>
        <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
        }
        ?>
</div>

<?php

$ajax = new js_ajax();
$ajax->function_name("update_jobcard");
$ajax->submit_btn_id("submit");
$ajax->update("jobcards");
$ajax->on_success("JOBCARD UPDATED SUCCESSFULLY");
$ajax->redirect("../jobcards/home.php");

?>