<?php include "../../root.class.php";
$html = new html();
$html->add_styles_page();
$db = new db_safeguard();

$current_date_time = date("Y-m-d H:i", strtotime("+2 hours"));


///////////// DRILLING JOBCARD FORM /////////////

$leads_res = $db->query("leads", "SELECT jc_no FROM leads ORDER BY jc_no DESC LIMIT 1");

if ($leads_res->num_rows == 0) {
    $jobcard_no = 1;
} else {
    $leads = $leads_res->fetch_assoc();
    $jobcard_no = $leads['jc_no'] + 1;
}

$date = new input();
$date->class("inputs");
$date->type("hidden");
$date->name("date_created");
$date->id("date_created");
$date->style("display: none;");
$date->value($current_date_time);

$jobcard_number = new input();
$jobcard_number->class("inputs");
$jobcard_number->type("text");
$jobcard_number->placeholder("JOBCARD NO");
$jobcard_number->name("jc_no");
$jobcard_number->id("jc_no");
$jobcard_number->value($jobcard_no);
$jobcard_number->readonly();

$jobcard_number_label = new label();
$jobcard_number_label->for("jc_no_label");
$jobcard_number_label->value("JOBCARD NO");

$phone = new input();
$phone->class("inputs");
$phone->type("text");
$phone->placeholder("PHONE NUMBER");
$phone->name("contact_number");
$phone->id("contact_number1");
$phone->onchange("check_number()");

$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->onchange("check_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->onchange("check_number()");


$phone1 = new input();
$phone1->class("inputs");
$phone1->type("text");
$phone1->placeholder("PHONE NUMBER");
$phone1->name("contact_number1");
$phone1->id("contact_number1");
$phone1->onchange("check_number()");

$other_number1 = new input();
$other_number1->class("inputs");
$other_number1->type("text");
$other_number1->placeholder("OTHER NUMBER");
$other_number1->name("other_number1");
$other_number1->id("other_number1");
$other_number1->onchange("check_number()");

$alternate_number1 = new input();
$alternate_number1->class("inputs");
$alternate_number1->type("text");
$alternate_number1->placeholder("ALTERNATE NUMBER");
$alternate_number1->name("alternate_number1");
$alternate_number1->id("alternate_number1");
$alternate_number1->onchange("check_number()");

$contact_number_label = new label();
$contact_number_label->for("contact_number_label");
$contact_number_label->value("CONTACT NUMBER");

$other_number_label = new label();
$other_number_label->for("other_number_label");
$other_number_label->value("OTHER NUMBER");

$alternate_number_label = new label();
$alternate_number_label->for("alternate_number_label");
$alternate_number_label->value("ALTERNATE NUMBER");

$type_label = new label();
$type_label->for("type_label");
$type_label->value("TYPE");

$type = new select();
$type->class("inputs");
$type->name("type");
$type->id("type");
$type->add_option("LEAD", "LEAD");
$type->add_option("JOBCARD", "JOBCARD");
$type->add_option("PUMP INSTALL", "PUMP INSTALL");
$type->add_option("PUMP REPAIR", "PUMP REPAIR");

$submit_btn = new button();

$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");

$area_label = new label();
$area_label->for("area");
$area_label->value("AREA");

$area = new input();
$area->class("inputs");
$area->type("text");
$area->placeholder("AREA");
$area->name("area");
$area->id("area");

$area1 = new input();
$area1->class("inputs");
$area1->type("text");
$area1->placeholder("AREA");
$area1->name("area1");
$area1->id("area1");

$status_label = new label();
$status_label->for("status_label");
$status_label->value("STATUS");

$status = new select();
$status->class("inputs");
$status->name("status");
$status->id("status");
$status->add_option("WAITING FOR DEPOSIT", "WAITING FOR DEPOSIT");
$status->add_option("FOLLOW UP", "FOLLOW UP");
$status->add_option("QUOTE SENT", "QUOTE SENT");
$status->add_option("WAITNG FOR CONFIRMATION", "WAITNG FOR CONFIRMATION");

$user_id = new input();
$user_id->class("inputs");
$user_id->type("hidden");
$user_id->name("user_created_id");
$user_id->id("user_created_id");
$user_id->value($_SESSION['user_id']);

$payment_label = new label();
$payment_label->for("payment_label");
$payment_label->value("PAYMENT MADE");

$payment = new select();
$payment->class("inputs");
$payment->name("payment_made");
$payment->id("payment_made");
$payment->add_option("YES", "YES");
$payment->add_option("NO", "NO");
$payment->onchange("payment()");


$payment1 = new select();
$payment1->class("inputs");
$payment1->name("payment_made1");
$payment1->id("payment_made1");
$payment1->add_option("YES", "YES");
$payment1->add_option("NO", "NO");
$payment1->onchange("payment(1)");


$payment_type_label = new label();
$payment_type_label->for("payment_type_label");
$payment_type_label->value("PAYMENT TYPE");

$payment_type = new select();
$payment_type->class("inputs");
$payment_type->name("payment_type");
$payment_type->id("payment_type");
$payment_type->add_option("CASH", "CASH");
$payment_type->add_option("EFT", "EFT");
$payment_type->add_option("OTHER", "OTHER");
$payment_type->onchange("payment_type_func()");


$payment_type1 = new select();
$payment_type1->class("inputs");
$payment_type1->name("payment_type1");
$payment_type1->id("payment_type1");
$payment_type1->add_option("CASH", "CASH");
$payment_type1->add_option("EFT", "EFT");
$payment_type1->add_option("OTHER", "OTHER");
$payment_type1->onchange("payment_type_func(1)");

$amount_label = new label();
$amount_label->for("amount_label");
$amount_label->value("AMOUNT");

$amount = new input();
$amount->class("inputs");
$amount->type("number");
$amount->placeholder("AMOUNT");
$amount->name("amount");
$amount->id("amount");


$amount1 = new input();
$amount1->class("inputs");
$amount1->type("number");
$amount1->placeholder("AMOUNT");
$amount1->name("amount1");
$amount1->id("amount1");

$slip_image_label = new label();
$slip_image_label->for("slip_image_label");
$slip_image_label->value("SLIP IMAGE");

$slip_image_label1 = new label();
$slip_image_label1->for("slip_image_label");
$slip_image_label1->value("SLIP IMAGE");

$slip_image = new input();
$slip_image->class("inputs");
$slip_image->name("slip_image");
$slip_image->id("slip_image");
$slip_image->type("text");
$slip_image->style("width:40vw; display: none;");

$slip_image1 = new input();
$slip_image1->class("inputs");
$slip_image1->name("slip_image");
$slip_image1->id("slip_image");
$slip_image1->type("text");
$slip_image1->style("width:40vw; display: none;");

$additional_notes_label = new label();
$additional_notes_label->for("additional_notes_label");
$additional_notes_label->value("ADDITIONAL NOTES");

$additional_notes = new textarea();
$additional_notes->class("inputs");
$additional_notes->placeholder("ADDITIONAL NOTES");
$additional_notes->name("additional_notes");
$additional_notes->id("additional_notes");
$additional_notes->style("width:50%;");

$additional_notes_label1 = new label();
$additional_notes_label1->for("additional_notes_label");
$additional_notes_label1->value("ADDITIONAL NOTES");

$additional_notes1 = new textarea();
$additional_notes1->class("inputs");
$additional_notes1->placeholder("ADDITIONAL NOTES");
$additional_notes1->name("additional_notes1");
$additional_notes1->id("additional_notes1");
$additional_notes1->style("width:50%;");
///////////// END DRILLING JOBCARD FORM ////////////////

///////////// PUMP JOBCARD FORM ////////////////////////

$pump_res = $db->query("leads", "SELECT jc_no FROM leads ORDER BY jc_no DESC LIMIT 1");

if ($pump_res->num_rows == 0) {
    $install_pump_jc_no = 1;
} else {
    $install_pump_jc = $pump_res->fetch_assoc();
    $install_pump_jc_no = $install_pump_jc['jobcard_no'] + 1;
}

$pump_jobcard_number = new input();
$pump_jobcard_number->class("inputs");
$pump_jobcard_number->type("text");
$pump_jobcard_number->placeholder("JOB CARD NUMBER");
$pump_jobcard_number->name("jobcard_no");
$pump_jobcard_number->id("jobcard_no");
$pump_jobcard_number->value($install_pump_jc_no);

$drilling_invoice_label = new label();
$drilling_invoice_label->for("drilling_invoice_label");
$drilling_invoice_label->value("DRILLING INVOICE");

$drilling_invoice = new select();
$drilling_invoice->class("inputs");
$drilling_invoice->name("drilling_invoice");
$drilling_invoice->id("drilling_invoice");
$drilling_invoice->fill_from_db("jobcards", "jc_no", "jc_no", "interested_in_pump = 'YES'");

$drilling_team_label = new label();
$drilling_team_label->for("drilling_team_label");
$drilling_team_label->value("DRILLING TEAM");

$drilling_team = new select();
$drilling_team->class("inputs");
$drilling_team->name("drilling_team_id");
$drilling_team->id("drilling_team_id");
$drilling_team->fill_from_db("teams", "record_id", "name", "status = 'ACTIVE'");



$pump_install_res = $db->query("pump_installation", "SELECT * FROM pump_installation ORDER BY record_id DESC LIMIT 1");

$no_installs = 0;

if ($pump_install_res->num_rows < 0) {
    $no_installs = 1;

    $pump_install_id = "PI_" . $no_installs;
} else {
    $pump_install = $pump_install_res->fetch_assoc();
    $no_installs = $pump_install['record_id'] + 1;

    $pump_install_id = "PI_" . $no_installs;
}

$drilling_jobcard_id_label = new label();
$drilling_jobcard_id_label->for("drilling_jobcard_id_label");
$drilling_jobcard_id_label->value("DRILLING JOB CARD ID");

$drilling_jobcard_id = new input();
$drilling_jobcard_id->class("inputs");
$drilling_jobcard_id->type("text");
$drilling_jobcard_id->placeholder("DRILLING JOB CARD ID");
$drilling_jobcard_id->name("drilling_jobcard_id");
$drilling_jobcard_id->id("drilling_jobcard_id");
$drilling_jobcard_id->value($pump_install_id);


///////////// END PUMP JOBCARD FORM ////////////////////

///////////// PUMP REPAIR JOBCARD FORM /////////////////

$installation_label = new label();
$installation_label->for("installation_label");
$installation_label->value("INSTALLATION");

$installation = new select();
$installation->class("inputs");
$installation->name("installation_id");
$installation->id("installation_id");
$installation->fill_from_db("pump_installation", "record_id", "jobcard_no");

$pump_repair_res = $db->query("pump_repair", "SELECT * FROM pump_repair ORDER BY record_id DESC LIMIT 1");

$no_repairs = 0;

if ($pump_repair_res->num_rows < 0) {
    $no_repairs = 1;

    $repair_id = "PR_" . $no_repairs;
} else {
    $pump_repair = $pump_repair_res->fetch_assoc();
    $no_repairs = $pump_repair['record_id'] + 1;

    $repair_id = "PR_" . $no_repairs;
}

$pump_repair_id_label = new label();
$pump_repair_id_label->for("pump_repair_id_label");
$pump_repair_id_label->value("PUMP REPAIR ID");

$pump_repair_id = new input();
$pump_repair_id->class("inputs");
$pump_repair_id->type("text");
$pump_repair_id->placeholder("PUMP REPAIR ID");
$pump_repair_id->name("pump_repair_id");
$pump_repair_id->id("pump_repair_id");
$pump_repair_id->value($repair_id);

$repair_team_id_label = new label();
$repair_team_id_label->for("repair_team_id_label");
$repair_team_id_label->value("REPAIR TEAM");

$repair_team_id = new select();
$repair_team_id->class("inputs");
$repair_team_id->name("repair_team_id");
$repair_team_id->id("repair_team_id");
$repair_team_id->fill_from_db("teams", "record_id", "name", "status = 'ACTIVE'");

$drilling_repair_invoice_label = new label();
$drilling_repair_invoice_label->for("drilling_invoice_label");
$drilling_repair_invoice_label->value("DRILLING INVOICE");

$drilling_repair_invoice = new select();
$drilling_repair_invoice->class("inputs");
$drilling_repair_invoice->name("drilling_repair_invoice");
$drilling_repair_invoice->id("drilling_repair_invoice");
$drilling_repair_invoice->fill_from_db("pump_installation", "jobcard_no", "jobcard_no");

///////////// END PUMP REPAIR JOBCARD FORM /////////////

?>

<form action="create_lead.ajax.php" method="post" enctype="multipart/form-data">
    <div class="form_down">
        <h1>NEW JOBCARD</h1>

        <?php
        $user_id->add();
        $type_label->add();
        ?>

        <select name="jobcard_type" id="jobcard_type" onchange="check_jobcard_type()" class="inputs">
            <option value=""></option>
            <option value="LEAD">LEAD</option>
            <option value="JOBCARD">JOBCARD</option>
            <option value="PUMP INSTALL">PUMP INSTALL</option>
            <option value="PUMP REPAIR">PUMP REPAIR</option>
        </select>

        <div id="lead" style="display:none; flex-direction: column; align-items: center; width: 100%;">
            <h2>NEW LEAD</h2>

            <?php
            $jobcard_number_label->add();
            $jobcard_number->add();
            $date->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
            $area_label->add();
            $area->add();
            $payment_label->add();
            $payment->add();

            ?>
            <div id="payment_yes" style="display: none;flex-direction: column;align-items: center;">
                <div style="display: flex;flex-direction: row;">
                    <div style="display: flex;flex-direction: column;">
                        <?php
                        $payment_type_label->add();
                        $payment_type->add();
                        ?>
                    </div>
                    <div style="display: flex;flex-direction: column;">
                        <?php
                        $amount_label->add();
                        $amount->add();
                        ?>
                    </div>
                </div>
                <div id="slip_section" style="display: none;flex-direction: column;align-items: center;">
                    <div id="slip_list" style="display: flex; flex-direction: column; align-items: center;"></div>
                    <?php
                    $slip_image_label->add();
                    ?>
                    <input type="file" class="inputs" id="slip_image" name="slip_image">
                </div>
            </div>

            <?php
            $status_label->add();
            $status->add();
            $additional_notes_label->add();
            $additional_notes->add();

            $submit_btn->value("ADD LEAD");
            $submit_btn->onclick("pre_sumbit()");
            $submit_btn->add();

            ?>
        </div>

        <div id="jobcard" style="display:none; flex-direction: column; align-items: center; width: 100%;">
            <h2>NEW JOBCARD</h2>

            <?php
            $jobcard_number_label->add();
            $jobcard_number->add();
            $date->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
                $phone1->add();
                $other_number1->add();
                $alternate_number1->add();
                ?>
            </div>

            <?php
            $area_label->add();
            $area1->add();
            $payment_label->add();
            $payment1->add();

            ?>
            <div id="payment_yes1" style="display: none;flex-direction: column;align-items: center;">
                <div style="display: flex;flex-direction: row;">
                    <div style="display: flex;flex-direction: column;">
                        <?php
                        $payment_type_label->add();
                        $payment_type1->add();
                        ?>
                    </div>
                    <div style="display: flex;flex-direction: column;">
                        <?php
                        $amount_label->add();
                        $amount1->add();
                        ?>
                    </div>
                </div>
                <div id="slip_section1" style="display: none;flex-direction: column;align-items: center;">
                    <div id="slip_list" style="display: flex; flex-direction: column; align-items: center;"></div>
                    <?php
                    $slip_image_label1->add();
                    ?>
                    <input type="file" class="inputs" id="slip_image1" name="slip_image1">
                </div>
            </div>

            <?php
            $status_label->add();
            ?>

            <select name="jobcard_status" id="jobcard_status" onchange="check_option()" class="inputs">
                <option value=""></option>
                <option value="WAITING FOR DEPOSIT">WAITING FOR DEPOSIT</option>
                <option value="FOLLOW UP">FOLLOW UP</option>
                <option value="QUOTE SENT">QUOTE SENT</option>
                <option value="WAITNG FOR CONFIRMATION">WAITNG FOR CONFIRMATION</option>
                <option value="ASSIGN TO TEAM">ASSIGN TO TEAM</option>
            </select>

            <div id="assign_teams" style="display: none; flex-direction: column;">
                <?php
                $team_assigned_label = new label();
                $team_assigned_label->for("team_assigned");
                $team_assigned_label->value("TEAM ASSIGNED");

                $team_assigned = new select();
                $team_assigned->class("inputs");
                $team_assigned->name("team_assigned_id");
                $team_assigned->id("team_assigned_id");
                $team_assigned->fill_from_db("teams", "record_id", "name", "status = 'ACTIVE'");

                $team_assigned_label->add();
                $team_assigned->add();
                ?>
            </div>

            <?php
            $additional_notes_label1->add();
            $additional_notes1->add();

            $submit_btn->value("ADD JOB CARD");
            $submit_btn->onclick("pre_sumbit()");
            $submit_btn->add();

            ?>
        </div>

        <div id="pump_install_jobcard" style="display:none; flex-direction: column; align-items: center; width: 100%;">
            <h2>PUMP INSTALL</h2>

            <?php
            $drilling_jobcard_id_label->add();
            $drilling_jobcard_id->add();

            $drilling_invoice_label->add();
            $drilling_invoice->add();

            $drilling_team_label->add();
            $drilling_team->add();

            $submit_btn->value("CREATE INSTALL");
            $submit_btn->onclick("pre_sumbit()");
            $submit_btn->add();
            ?>
        </div>

        <div id="pump_repair_jobcard" style="display:none; flex-direction: column; align-items: center; width: 100%;">
            <h2>PUMP REPAIR</h2>

            <?php
            $pump_repair_id_label->add();
            $pump_repair_id->add();

            $installation_label->add();
            $installation->add();

            $drilling_repair_invoice_label->add();
            $drilling_repair_invoice->add();

            $repair_team_id_label->add();
            $repair_team_id->add();

            $submit_btn->value("CREATE REPAIR");
            $submit_btn->onclick("pre_sumbit()");
            $submit_btn->add();
            ?>
        </div>

        <script>

            function payment(number = '') {
                var payment = document.getElementById("payment_made" + number);
                if (payment.value == "YES") {
                    document.getElementById("payment_yes" + number).style.display = "flex";
                } else if (payment.value == "NO") {
                    document.getElementById("payment_yes" + number).style.display = "none";
                } else {
                    document.getElementById("payment_yes" + number).style.display = "none";
                }
            }

            function payment_type_func(number = '') {
                var payment_type = document.getElementById("payment_type" + number);
                if (payment_type.value == "EFT") {
                    document.getElementById("slip_section" + number).style.display = "flex";
                } else {
                    document.getElementById("slip_section" + number).style.display = "none";
                }
            }

            function check_number() {
                var contact_number = document.getElementById("contact_number");
                var other_number = document.getElementById("other_number");
                var alternate_number = document.getElementById("alternate_number");

                if (contact_number.value.length > 0) {
                    if (contact_number.value.length != 10) {
                        alert("Please enter a valid contact number.");
                        contact_number.style.border = "3px solid red";
                        return false;
                    } else {
                        contact_number.style.border = "3px solid green";
                    }
                } else {
                    contact_number.style.border = "3px solid orange";
                }
            }

            let slipCounter = 0; // keep global counter

            function add_slip_image(file_save_path = "../jobcards/images/") {
                event.preventDefault();
                // Create new file input
                var new_input = document.createElement("input");
                new_input.type = "file";
                new_input.className = "inputs";
                new_input.accept = "image/jpeg,image/png";
                new_input.style.width = "40vw";

                // attach file_save_path as data attribute
                new_input.setAttribute("data-file-save-path", file_save_path);

                // Append to container
                document.getElementById("slip_list").appendChild(new_input);

                // Add change listener
                new_input.addEventListener("change", function () {
                    if (this.files && this.files[0]) {
                        var file = this.files[0];
                        var savePath = this.getAttribute("data-file-save-path");
                        var all_file_list = document.getElementById("slip_image");

                        // --- Remove previous preview ---
                        var nextElem = this.nextSibling;
                        while (nextElem && nextElem.nodeType !== 1) { // skip text nodes
                            nextElem = nextElem.nextSibling;
                        }
                        if (nextElem && nextElem.tagName === "IMG") {
                            nextElem.remove();
                        }

                        // --- Remove previous filename from all_file_list if exists ---
                        if (this.dataset.prevFile) {
                            var filenames = all_file_list.value.split(",").map(f => f.trim());
                            filenames = filenames.filter(f => f !== this.dataset.prevFile);
                            all_file_list.value = filenames.join(", ");
                        }

                        // --- Preview ---
                        var preview = document.createElement("img");
                        preview.style.maxWidth = "200px";
                        preview.style.height = "auto";
                        preview.style.display = "block";
                        preview.style.marginTop = "10px";

                        var reader = new FileReader();
                        reader.onload = function (e) {
                            preview.src = e.target.result;
                        };
                        reader.readAsDataURL(file);

                        this.parentNode.insertBefore(preview, this.nextSibling);

                        // --- Upload ---
                        var formData = new FormData();
                        // Replace spaces with underscores in filename
                        var cleanName = file.name.replace(/\s+/g, '_');

                        formData.append('fileToUpload', file);
                        formData.append('ajax_type', "file_upload");
                        formData.append('file_save_path', savePath);
                        formData.append('file_name', cleanName);

                        // Store reference to current input for use in callback
                        var currentInput = this;

                        var xhr = new XMLHttpRequest();
                        xhr.onreadystatechange = function () {
                            if (xhr.readyState === XMLHttpRequest.DONE) {
                                if (xhr.status === 200) {
                                    console.log(`Uploaded: ${file.name} → ${savePath}`);

                                    // Add new filename to slip_image
                                    if (all_file_list.value) {
                                        all_file_list.value += ", " + cleanName;
                                    } else {
                                        all_file_list.value = cleanName;
                                    }

                                    // Store current filename in dataset for next change
                                    currentInput.dataset.prevFile = cleanName;
                                    console.log(`Updated slip_image: ${all_file_list.value}`);
                                } else {
                                    console.error("Upload failed or server error:", xhr.responseText);
                                    alert("Failed to upload image. Please try again.");
                                }
                            }
                        };
                        xhr.open("POST", "../jobcards/upload.php", true);
                        xhr.send(formData);
                    }
                });

                slipCounter++;
            }

            function check_jobcard_type() {
                var jobcard_type = document.getElementById("jobcard_type");

                if (jobcard_type.value == "LEAD") {
                    document.getElementById("lead").style.display = "flex";
                    document.getElementById("jobcard").style.display = "none";
                    document.getElementById("pump_install_jobcard").style.display = "none";
                    document.getElementById("pump_repair_jobcard").style.display = "none";
                } else if (jobcard_type.value == "JOBCARD") {
                    document.getElementById("lead").style.display = "none";
                    document.getElementById("jobcard").style.display = "flex";
                    document.getElementById("pump_install_jobcard").style.display = "none";
                    document.getElementById("pump_repair_jobcard").style.display = "none";

                } else if (jobcard_type.value == "PUMP INSTALL") {
                    document.getElementById("lead").style.display = "none";
                    document.getElementById("pump_install_jobcard").style.display = "flex";
                    document.getElementById("jobcard").style.display = "none";
                    document.getElementById("pump_repair_jobcard").style.display = "none";

                } else if (jobcard_type.value == "PUMP REPAIR") {
                    document.getElementById("lead").style.display = "none";
                    document.getElementById("jobcard").style.display = "none";
                    document.getElementById("pump_install_jobcard").style.display = "none";
                    document.getElementById("pump_repair_jobcard").style.display = "flex";

                } else {
                    document.getElementById("lead").style.display = "none";
                    document.getElementById("jobcard").style.display = "none";
                    document.getElementById("pump_install_jobcard").style.display = "none";
                    document.getElementById("pump_repair_jobcard").style.display = "none";
                }
            }

            function check_option() {
                var status = document.getElementById("jobcard_status").value;
                var team = document.getElementById("assign_teams");

                if (status == "ASSIGN TO TEAM") {
                    team.style.display = "flex";
                } else {
                    team.style.display = "none";
                }
            }

            function pre_sumbit() {
                var jobcard_status = document.getElementById("jobcard_status").value;
                var jobcard_type = document.getElementById("jobcard_type").value;
                var status = document.getElementById("status").value;
                var area = document.getElementById("area").value;
                var contact_number = document.getElementById("contact_number").value;
                var team = document.getElementById("team_assigned_id").value;
                var drilling_invoice = document.getElementById("drilling_invoice").value;
                var drilling_team = document.getElementById("drilling_team_id").value;
                var installation = document.getElementById("installation_id").value;
                var drilling_invoice = document.getElementById("drilling_invoice").value;
                var repair_team = document.getElementById("repair_team_id").value;


                //PUMP REPAIR CHECK

                if (jobcard_type == "PUMP REPAIR") {
                    if (repair_invoice == "") {
                        alert("Please enter a repair invoice number.");
                        document.getElementById("repair_invoice").focus();
                        document.getElementById("repair_invoice").style.border = "2px solid red";
                        return false;
                    } else {
                        document.getElementById("repair_invoice").style.border = "2px solid orange";
                    }

                    if (repair_team == "") {
                        alert("Please select a repair team.");
                        document.getElementById("repair_team_id").focus();
                        document.getElementById("repair_team_id").style.border = "2px solid red";
                        return false;
                    } else {
                        document.getElementById("repair_team_id").style.border = "2px solid orange";
                    }

                    if (installation == "") {
                        alert("Please select an installation.");
                        document.getElementById("installation_id").focus();
                        document.getElementById("installation_id").style.border = "2px solid red";
                        return false;
                    } else {
                        document.getElementById("installation_id").style.border = "2px solid orange";
                    }
                }

                //PUMP INSTALL CHECK

                if (jobcard_type == "PUMP INSTALL") {
                    if (drilling_invoice == "") {
                        alert("Please enter a drilling invoice number.");
                        document.getElementById("drilling_invoice").focus();
                        document.getElementById("drilling_invoice").style.border = "2px solid red";
                        return false;
                    } else {
                        document.getElementById("drilling_invoice").style.border = "2px solid orange";
                    }

                    if (drilling_team == "") {
                        alert("Please select a drilling team.");
                        document.getElementById("drilling_team_id").focus();
                        document.getElementById("drilling_team_id").style.border = "2px solid red";
                        return false;
                    } else {
                        document.getElementById("drilling_team_id").style.border = "2px solid orange";
                    }
                }

                //JOBCARD CHECK

                if (jobcard_type == "JOBCARD") {
                    if (jobcard_status == "ASSIGN TO TEAM" && team == "") {

                        document.getElementById("team_assigned_id").focus();
                        document.getElementById("team_assigned_id").style.border = "2px solid red";
                        alert("Please select a team.");
                        return false;
                    } else {
                        document.getElementById("team_assigned_id").style.border = "2px solid orange";
                    }
                }

                //LEAD CHECK

                if (area == "") {
                    document.getElementById("area").focus();
                    document.getElementById("area").style.border = "2px solid red";
                    alert("Please select an area.");
                    return false;
                } else {
                    document.getElementById("area").style.border = "2px solid orange";
                }

                if (contact_number == "") {
                    document.getElementById("contact_number").focus();
                    document.getElementById("contact_number").style.border = "2px solid red";
                    alert("Please enter a contact number.");
                    return false;
                } else {
                    document.getElementById("contact_number").style.border = "2px solid orange";
                }

                if (status == "") {
                    alert("Please select a team.");

                    document.getElementById("status").focus();
                    document.getElementById("status").style.border = "2px solid red";
                    return false;
                } else {
                    document.getElementById("status").style.border = "2px solid orange";
                }

                return true;
            }
        </script>

    </div>
</form>