<?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("work_requests", "SELECT jc_no FROM work_requests 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->name("jc_no");
$jobcard_number->id("jc_no");
$jobcard_number->value($jobcard_no);
$jobcard_number->readonly();
$jobcard_number->style("font-size: 1em; width: 80%; display: none;");

$type_label = new label();
$type_label->for("type_label");
$type_label->value("TYPE");
$type_label->class("create_label");

$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");
$status->addAttribute("style", "font-size: 1em; width: 80%;");

$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']);

$additional_notes_label = new label();
$additional_notes_label->for("additional_notes_label");
$additional_notes_label->value("ADDITIONAL NOTES");
$additional_notes_label->class("create_team_label");
$additional_notes_label->addAttribute("style", "font-size: 1.5em; margin-top: 0.5vw;");

$additional_notes = new textarea();
$additional_notes->class("create_textarea");
$additional_notes->name("additional_notes");
$additional_notes->id("additional_notes");
$additional_notes->rows(5);


$submit_btn = new button();

///////////// END DRILLING JOBCARD FORM ////////////////

///////////// PUMP JOBCARD FORM ////////////////////////

$pump_res = $db->query("work_requests", "SELECT jc_no FROM work_requests 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->name("jobcard_no");
$pump_jobcard_number->id("jobcard_no");
$pump_jobcard_number->value($install_pump_jc_no);

// $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_invoice->addAttribute("style", "font-size: 1em; width: 100%; margin: 0.5vw auto;");


$drilling_invoice_res = $db->query("jobcards", "SELECT * FROM jobcards WHERE interested_in_pump = 'YES' ORDER BY jc_no DESC");

$install_team_list = "";

while ($drill_invoice = $drilling_invoice_res->fetch_assoc()) {
    $install_team_list .= "<option value='{$drill_invoice['jc_no']}'>{$drill_invoice['jc_no']}~{$drill_invoice['client_name']}~{$drill_invoice['address']}</option>";
}

$drilling_invoice_res = $db->query("pump_installation", "SELECT * FROM pump_installation WHERE status = '1' ORDER BY record_id DESC", "");

$invoice_list = "";

while ($invoice = $drilling_invoice_res->fetch_assoc()) {
    $invoice_list .= "<option value='{$invoice['jobcard_no']}'>{$invoice['jobcard_no']}</option>";
}

$install_team = new select();
$install_team->class("inputs");
$install_team->name("install_team_id");
$install_team->id("install_team_id");
$install_team->fill_from_db("teams", "record_id", "name", "status = 'ACTIVE'");
$install_team->addAttribute("style", "font-size: 1em; width: 100%; margin: 0.5vw auto;");

$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 = new input();
$drilling_jobcard_id->class("inputs");
$drilling_jobcard_id->type("text");
$drilling_jobcard_id->name("drilling_jobcard_id");
$drilling_jobcard_id->id("drilling_jobcard_id");
$drilling_jobcard_id->value($pump_install_id);
$drilling_jobcard_id->addAttribute("style", "font-size: 1em; width: 60%;");


///////////// END PUMP JOBCARD FORM ////////////////////

///////////// PUMP REPAIR JOBCARD FORM /////////////////

$installation = new select();
$installation->class("inputs");
$installation->name("installation_id");
$installation->id("installation_id");
$installation->fill_from_db("pump_installation", "record_id", "jobcard_no");
$installation->addAttribute("style", "font-size: 1em; width: 100%;  margin: 0.5vw auto;");

$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 = new input();
$pump_repair_id->class("inputs");
$pump_repair_id->type("text");
$pump_repair_id->name("pump_repair_id");
$pump_repair_id->id("pump_repair_id");
$pump_repair_id->value($repair_id);
$pump_repair_id->addAttribute("style", "font-size: 1em; width: 100%;  margin: 0.5vw auto;");

$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'");
$repair_team_id->addAttribute("style", "font-size: 1em; width: 100%;  margin: 0.5vw auto;");

$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");
$drilling_repair_invoice->addAttribute("style", "font-size: 1em; width: 100%; margin: 0.5vw auto;");

///////////// END PUMP REPAIR JOBCARD FORM /////////////

?>

<form action="create_lead.ajax.php" method="post">
    <div class="create_form_down">
        <h1 class="create_h1">NEW JOBCARD <?php echo $jobcard_no; ?></h1>

        <?php
        $user_id->add();
        $jobcard_number->add();
        $type_label->add();
        ?>

        <select name="jobcard_type" id="jobcard_type" onchange="check_jobcard_type()" class="create_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="default" style="display:none; flex-direction: column; align-items: center; width: 100%;">
            <div
                style="width: 93%; background-color: white; border-top-left-radius: 2vw; border-top-right-radius: 2vw; display: flex; flex-direction: column;padding: 1vw;">
                <h2 class="create_h2">CLIENT DETAILS</h2>
                <?php
                $date->add();
                ?>
                <div style="display: flex; flex-direction: row; width: 100%;">
                </div>
                <div style="display: flex; flex-direction: row; width: 100%;">
                    <label for="jc_no_label" class="create_details_label">CONTACT NUMBER :
                    </label>
                    <input type="text" name="contact_number" id="contact_number" class="create_details_input">
                </div>
                <div style="display: flex; flex-direction: row; width: 100%;">
                    <label for="jc_no_label" class="create_details_label">OTHER NUMBER : </label>
                    <input type="text" name="other_number" id="other_number" class="create_details_input">
                </div>
                <div style="display: flex; flex-direction: row; width: 100%;">
                    <label for="jc_no_label" class="create_details_label">ALTERNATE NUMBER :
                    </label>
                    <input type="text" name="alternate_number" id="alternate_number" class="create_details_input">
                </div>
                <div style="display: flex; flex-direction: row; width: 100%;">
                    <label for="jc_no_label" class="create_details_label">AREA : </label>
                    <input type="text" name="area" id="area" class="create_details_input">
                </div>

                <div id="lead_status" style="display:none; flex-direction: column; align-items: center; width: 100%;">
                    <div style="display: flex; flex-direction: row; width: 100%;">
                        <label for="status_label" class="create_details_label"> STATUS :
                        </label>

                        <select name="status" id="status" onchange="check_option()" class="create_details_input">
                            <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>
                        </select>
                    </div>14308.00 - b1-111
                    5201.00 - b1-112

                </div>

                <div id="jobcard_status"
                    style="display:none; flex-direction: column; align-items: center; width: 100%;">
                    <div style="display: flex; flex-direction: row; width: 100%;">
                        <label for="status_label" class="create_details_label"> STATUS :
                        </label>
                        <select name="job_status" id="job_status" onchange="check_option()"
                            class="create_details_input">
                            <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" selected>ASSIGN TO TEAM</option>
                        </select>
                    </div>

                    <div id="assign_teams" style="display: none; flex-direction: column; width: 100%;">
                        <label for="team_assigned" class="create_team_label">TEAM ASSIGNED : </label>
                        <?php
                        $team_assigned = new select();
                        $team_assigned->class("create_details_input");
                        $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->add();
                        ?>
                    </div>
                </div>
            </div>
        </div>

        <div id="pump_install_jobcard"
            style="display:none; flex-direction: column; align-items: center; width: 95%; background-color: white; border-top-left-radius: 2vw; border-top-right-radius: 2vw;">
            <div style="width: 90%;">
                <h2 style="margin: 1vw;">INSTALLATION DETAILS</h2>
                <div style="display: flex; flex-direction: row; width: 100%;">
                    <label for="drilling_jobcard_label" class="labels" style="font-size: 1.5em; width: 45%;">
                        JOBCARD ID : </label>
                    <input type=" text" name="drilling_jobcard_id" id="drilling_jobcard_id" class="inputs"
                        style="width: 100%; font-size: 1em; margin: 0.5vw auto;" readonly
                        value="<?php echo $pump_install_id; ?>">
                </div>

                <div style="display: flex; flex-direction: row; width: 100%;">
                    <label for="drilling_invoice_label" class="labels" style="font-size: 1.5em; width: 45%;">DRILLING
                        JOBCARD NO : </label>
                    <datalist id="invoice_list">
                        <?php
                        echo $install_team_list;
                        ?>
                    </datalist>
                    <input list="invoice_list" id="drilling_invoice" name="drilling_invoice" class="inputs"
                        style="width: 100%; font-size: 1em; margin: 0.5vw auto;">
                    <?php
                    // $drilling_invoice->addAttribute("list", "install_team_id");
                    // $drilling_invoice->add();
                    
                    ?>
                </div>
                <div style="display: flex; flex-direction: row; width: 100%;">
                    <label for="drilling_date_label" class="labels" style="font-size: 1.5em; width: 45%;">PUMP SLIP :
                    </label>
                    <input type="text" name="drilling_date" id="drilling_date" class="inputs"
                        style="width: 100%; font-size: 1em; margin: 0.5vw auto;">
                </div>
                <div style="display: flex; flex-direction: row; width: 100%;">
                    <label for="install_team_label" class="labels" style="font-size: 1.5em; width: 45%;">INSTALL TEAM
                        : </label>
                    <select name="install_team_id" id="install_team_id"
                        style="font-size: 1em; width: 100%; margin: 0.5vw auto;" class="inputs">
                        <option value="">--SELECT--</option>
                        <option value="26" selected>Install Team</option>
                    </select>
                </div>
            </div>
        </div>

        <div id="pump_repair_jobcard"
            style="display:none; flex-direction: column; align-items: center; width: 95%; background-color: white; border-top-left-radius: 2vw; border-top-right-radius: 2vw;">
            <div style="width: 90%;">
                <h2 style="margin: 1vw;">REPAIR DETAILS</h2>
                <div style="display: flex; flex-direction: row; width: 100%;">
                    <label for="pump_repair_label" class="labels" style="font-size: 1.5em; width: 40%;">PUMP REPAIR ID :
                    </label>
                    <?php
                    $pump_repair_id->add();
                    ?>
                </div>
                <div style="display: flex; flex-direction: row; width: 100%;">
                    <label for="intallation_label" class="labels" style="font-size: 1.5em; width: 40%;">INSTALLATION :
                    </label>
                    <datalist id="intall_data">
                        <?php
                        echo $install_pump_jc;
                        ?>
                    </datalist>
                    <input list="invoice_list" id="installation_id" name="installation_id" class="inputs"
                        style="width: 100%; font-size: 1em; margin: 0.5vw auto;">
                </div>
                <div style="display: flex; flex-direction: row; width: 100%;">
                    <label for="" class="labels" style="font-size: 1.5em; width: 40%;">REPAIR TEAM : </label>
                    <select name="repair_team_id" id="repair_team_id"
                        style="font-size: 1em; width: 100%; margin: 0.5vw auto;" class="inputs">
                        <option value="">--SELECT--</option>
                        <option value="26" selected>Install Team</option>
                    </select>
                </div>
            </div>
        </div>

        <div id="end_section"
            style="display:none; flex-direction: column; align-items: center; width: 95%; background-color: white; border-bottom-left-radius: 2vw; border-bottom-right-radius: 2vw;">
            <?php
            $additional_notes_label->add();
            $additional_notes->add();

            $submit_btn->value("SUBMIT");
            $submit_btn->onclick("pre_sumbit()");
            $submit_btn->style("font-size: 1.5em; padding: 2vw;");
            $submit_btn->add();
            ?>
        </div>

        <script>

            function check_jobcard_type() {
                var jobcard_type = document.getElementById("jobcard_type");

                if (jobcard_type.value == "LEAD") {
                    document.getElementById("default").style.display = "flex";
                    document.getElementById("lead_status").style.display = "flex";
                    document.getElementById("jobcard_status").style.display = "none";
                    document.getElementById("end_section").style.display = "flex";
                    document.getElementById("pump_install_jobcard").style.display = "none";
                    document.getElementById("pump_repair_jobcard").style.display = "none";
                } else if (jobcard_type.value == "JOBCARD") {
                    document.getElementById("default").style.display = "flex";
                    document.getElementById("lead_status").style.display = "none";
                    document.getElementById("jobcard_status").style.display = "flex";
                    document.getElementById("end_section").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("pump_install_jobcard").style.display = "flex";
                    document.getElementById("default").style.display = "none";
                    document.getElementById("lead_status").style.display = "none";
                    document.getElementById("jobcard_status").style.display = "none";
                    document.getElementById("end_section").style.display = "flex";
                    document.getElementById("pump_repair_jobcard").style.display = "none";

                } else if (jobcard_type.value == "PUMP REPAIR") {
                    document.getElementById("pump_install_jobcard").style.display = "none";
                    document.getElementById("pump_repair_jobcard").style.display = "flex";
                    document.getElementById("default").style.display = "none";
                    document.getElementById("lead_status").style.display = "none";
                    document.getElementById("jobcard_status").style.display = "none";
                    document.getElementById("end_section").style.display = "flex";

                } else {
                    document.getElementById("pump_install_jobcard").style.display = "none";
                    document.getElementById("pump_repair_jobcard").style.display = "none";
                    document.getElementById("default").style.display = "none";
                    document.getElementById("lead_status").style.display = "none";
                    document.getElementById("jobcard_status").style.display = "none";
                    document.getElementById("end_section").style.display = "none";
                }
            }

            check_option();

            function check_option() {
                var status = document.getElementById("job_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.length == 0) {
                        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 == "") {
                //     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>

</form>