<?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;
}

///////////// 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_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;
}

$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>

        <input type="text" class="inputs" name="user_created_id" id="user_created_id" value="<?php echo $_SESSION['user_id'] ?>" hidden/>
        <input type="text" class="inputs" name="jc_no" id="jc_no" value="<?php echo $jobcard_no ?>" readonly style="font-size: 1em; width: 80%;" hidden/>

        <label for="type_label" class="create_label">TYPE</label>

        <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 = 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);

                $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_jc" id="contact_number_jc" 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>
                </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;">
                </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;">
            <h2 style="margin: 1vw;">REPAIR DETAILS</h2>

                <div style="display: flex; flex-direction: row; width: 95%;">
                    <label for="pump_repair_label" class="labels" style="font-size: 1.5em; width: 40%;">PUMP REPAIR ID :
                    </label>
                    <?php
                    $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;");

                    $pump_repair_id->add();
                    ?>
                </div>

                <div style="display: flex; flex-direction: row; width: 95%;">
                    <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>

            <select id="client_type" name="client_type" style="width: 85%;" class="create_inputs" onchange="check_client_type()">
                <option>CLIENT TYPE</option>
                <option value="NEW">NEW CLIENT</option>
                <option value="EXISTING">EXISTING CLIENT</option>
            </select>
            <script>
                function check_client_type(){
                    client_type = document.getElementById('client_type').value;

                    if (client_type === 'NEW'){
                        document.getElementById('new_client').style.display = "flex";
                        document.getElementById('existing_client').style.display = "none";

                    } else if (client_type === 'EXISTING') {
                        document.getElementById('new_client').style.display = "none";
                        document.getElementById('existing_client').style.display = "flex";

                    } else {
                        document.getElementById('new_client').style.display = "none";
                        document.getElementById('existing_client').style.display = "none";

                    }
                }
            </script>
            
            
            <div id="existing_client" style="display: none; flex-direction: column; width: 95%;">
                    <h2>EXISTING CLIENT</h2>
                    <div style="display: flex; flex-direction: row; width: 100%;">
                        <label for="pump_slip_label" class="labels" style="font-size: 1.5em; width: 40%;">PUMP SLIP : </label>
                        <input type="text" id="pump_slip" name="pump_slip" style="font-size: 1em; width: 100%; margin: 0.5vw auto;" class="inputs" />
                    </div>

                    <div style="display: flex; flex-direction: row; width: 100%;">
                        <label for="drilling_slip_label" class="labels" style="font-size: 1.5em; width: 40%;">DRILLING SLIP : </label>
                        <input type="text" id="drilling_slip" name="drilling_slip" style="font-size: 1em; width: 100%; margin: 0.5vw auto;" class="inputs" />
                    </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="install_data">
                            <?php
                            $install_list_res = $db->query("pump_installation", "SELECT * FROM pump_installation ORDER BY record_id DESC");

                            $install_list = "";

                            while ($install = $install_list_res->fetch_assoc()) {
                                $install_list .= "<option value='{$install['record_id']}'>{$install['jobcard_no']} ~ {$install['client_name']} ~ {$install['area']}</option>";
                            }

                            echo $install_list;
                            ?>
                        </datalist>
                        <input list="install_data" id="installation_id" name="installation_id" class="inputs"
                        style="width: 100%; font-size: 1em; margin: 0.5vw auto;">
                    </div>
            </div>

            <div id="new_client" style="display: none; flex-direction: column; width: 95%;">
                    <h2>NEW CLIENT</h2>
                    <div style="display: flex; flex-direction: row; width: 100%;">
                        <label for="pump_slip_label" class="labels" style="font-size: 1.5em; width: 40%;">PUMP SLIP : </label>
                        <input type="text" id="pump_slip" name="pump_slip" style="font-size: 1em; width: 100%; margin: 0.5vw auto;" class="inputs" />
                    </div>

                    <div style="display: flex; flex-direction: row; width: 100%;">
                        <label for="drilling_slip_label" class="labels" style="font-size: 1.5em; width: 40%;">DRILLING SLIP : </label>
                        <input type="text" id="drilling_slip" name="drilling_slip" style="font-size: 1em; width: 100%; margin: 0.5vw auto;" class="inputs" />
                    </div>

                    <div style="display: flex; flex-direction: row; width: 100%;">
                        <label for="address_label" class="labels" style="font-size: 1.5em; width: 40%;">AREA : </label>
                        <input type="text" id="address_repair" name="address_repair" style="font-size: 1em; width: 100%; margin: 0.5vw auto;" class="inputs" />
                    </div>

                    <div style="display: flex; flex-direction: row; width: 100%;">
                        <label for="contact_number_label" class="labels" style="font-size: 1.5em; width: 40%;">CONTACT NUMBER : </label>
                        <input type="text" id="contact_number_repair" name="contact_number_repair" style="font-size: 1em; width: 100%; margin: 0.5vw auto;" class="inputs" />
                    </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;">
            <label for="additional_notes_label" class="create_team_label" style="font-size: 1.5em; margin-top: 0.5vw;"></label>
            <textarea id="additional_notes" name="additional_notes" row="8" class="create_textarea" style="height: 10vw;"></textarea>

            <button onclick="return pre_sumbit()" style="font-size: 1.5em; padding: 2vw;" id="submit" class="submit_btn">SUBMIT</button>
        </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("end_section").style.display = "flex";
                document.getElementById("pump_repair_jobcard").style.display = "flex";
                document.getElementById("pump_install_jobcard").style.display = "none";
                document.getElementById("default").style.display = "none";
                document.getElementById("lead_status").style.display = "none";
                document.getElementById("jobcard_status").style.display = "none";

            } 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 el = document.getElementById("job_status");
            if (!el) return;  // guard added
            var status = el.value;
            var team = document.getElementById("assign_teams");
            if (status == "ASSIGN TO TEAM") {
                team.style.display = "flex";
            } else {
                team.style.display = "none";
            }
        }

        // AFTER — use the correct IDs and guard them:
        function pre_sumbit() {
            var jobcard_type = document.getElementById("jobcard_type").value;

            if (!jobcard_type) {
                alert("Please select a jobcard type.");
                return false;
            }

            if (jobcard_type == "PUMP REPAIR") {
                var repair_team = document.getElementById("repair_team_id").value;
                var installation = document.getElementById("installation_id").value;
                var client_type = document.getElementById("client_type").value;

                if (repair_team == "") {
                    alert("Please select a repair team.");
                    document.getElementById("repair_team_id").style.border = "2px solid red";
                    return false;
                }

                if (client_type == "EXISTING" && installation == "") {
                    alert("Please select an installation.");
                    document.getElementById("installation_id").style.border = "2px solid red";
                    return false;
                }
                return true;
            }

            if (jobcard_type == "PUMP INSTALL") {
                var drilling_invoice = document.getElementById("drilling_invoice").value;
                var install_team = document.getElementById("install_team_id").value;

                if (drilling_invoice == "") {
                    alert("Please enter a drilling jobcard number.");
                    document.getElementById("drilling_invoice").style.border = "2px solid red";
                    return false;
                }
                if (install_team == "") {
                    alert("Please select an install team.");
                    document.getElementById("install_team_id").style.border = "2px solid red";
                    return false;
                }
                return true;
            }

            // LEAD and JOBCARD both need area + contact
            var area = document.getElementById("area").value;
            var contact_number = document.getElementById("contact_number").value;

            if (area == "") {
                document.getElementById("area").focus();
                document.getElementById("area").style.border = "2px solid red";
                alert("Please enter an area.");
                return false;
            }
            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;
            }

            if (jobcard_type == "JOBCARD") {
                var job_status = document.getElementById("job_status").value;
                var team = document.getElementById("team_assigned_id").value;
                if (job_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;
                }
            }

            return true;
        }

    </script>

</form>
