<?php include "../../root.class.php";
$html = new html();
$html->add_styles_page();
// $html->check_user_type("ADMIN");
$db = new db_safeguard();

$jobcard_label = new label();
$jobcard_label->for("jobcard_no_label");
$jobcard_label->value("JOBCARD NO : ");
$jobcard_label->addAttribute("style", "font-size: 1.5em; width: 40%; margin: 0.5vw auto;");

$jobcard_no = new input();
$jobcard_no->class("inputs");
$jobcard_no->name("jc_no");
$jobcard_no->id("jc_no");
$jobcard_no->value($_GET["jc_no"]);
$jobcard_no->readonly();
$jobcard_no->addAttribute("style", "font-size: 1em; width: 100%; margin: 0.5vw auto;");

$status_label = new label();
$status_label->for("status_label");
$status_label->value("STATUS : ");
$status_label->addAttribute("style", "font-size: 1.5em; width: 40%; margin: 0.5vw auto;");

$status = new select();
$status->class("inputs");
$status->name("status");
$status->id("status");
// $status->style("width: 20%;");
$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->add_option("ASSIGN TEAM", "ASSIGN TEAM");
$status->add_option("CLIENT CANCELED", "CLIENT CANCELED");
$status->onchange("assign_team()");
$status->addAttribute("style", "font-size: 1em; width: 100%; margin: 0.5vw auto;");

$team_assigned_label = new label();
$team_assigned_label->for("team_assigned");
$team_assigned_label->value("TEAM ASSIGNED : ");
$team_assigned_label->addAttribute("style", "font-size: 1.5em; width: 40%; margin: 0.5vw auto;");

$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->addAttribute("style", "font-size: 1em; width: 100%; margin: 0.5vw auto;");

$notes_label = new label();
$notes_label->for("notes_label");
$notes_label->value("ADDITIONAL NOTES");
$notes_label->addAttribute("style", "font-size: 1.5em; width: 100%; margin: 0.5vw auto;");

$notes = new textarea();
$notes->name("additional_notes");
$notes->id("additional_notes");
$notes->placeholder("Additional Notes");
$notes->addAttribute("style", "font-size: 1em; width: 95%; margin: 0.5vw auto;");
$notes->rows(5);

$contact_number = new input();
$contact_number->type("hidden");
$contact_number->name("contact_number");
$contact_number->id("contact_number");
$contact_number->placeholder("Contact Number");

$alternate_number = new input();
$alternate_number->type("hidden");
$alternate_number->name("alternate_number");
$alternate_number->id("alternate_number");
$alternate_number->placeholder("Alternate Number");

$other_number = new input();
$other_number->type("hidden");
$other_number->name("other_number");
$other_number->id("other_number");
$other_number->placeholder("Other Number");

$address = new input();
$address->type("hidden");
$address->name("address");
$address->id("address");
$address->placeholder("Address");

$slip = new input();
$slip->type("hidden");
$slip->name("slip");
$slip->id("slip");
$slip->placeholder("Slip");

$submit_btn = new button();
$submit_btn->value("ASSIGN");

$leads_data_res = $db->query("work_requests", "SELECT * FROM `work_requests` WHERE `jc_no` = '{$_GET['jc_no']}'");

while ($leads_data = $leads_data_res->fetch_assoc()) {
    $contact_number->value($leads_data["contact_number"]);
    $alternate_number->value($leads_data["alternate_number"]);
    $other_number->value($leads_data["other_number"]);
    $address->value($leads_data["address"]);
    $slip->value($leads_data["slip_image"]);
}

?>

<form action="assign_jobcard.ajax.php" method="post">
    <div class="form_down">
        <h1 style="font-size: 3em; margin-bottom: 0.5vw; margin-top: 1.5vw;">ASSIGN JOBCARDS</h1><br>

        <?php
        $contact_number->add();
        $alternate_number->add();
        $other_number->add();
        $address->add();
        $slip->add();
        ?>

        <div
            style="width: 95%; background-color: white; border-radius: 2vw; display: flex; flex-direction: column;padding: 1vw; align-items: center;">
            <div style="display: flex; flex-direction: row; width: 100%;">
                <?php
                $jobcard_label->add();
                $jobcard_no->add();
                ?>
            </div>

            <div style="display: flex; flex-direction: row; width: 100%;">
                <?php
                $status_label->add();
                $status->add();
                ?>
            </div>

            <div id="teams" style="display:none; width: 100%;">
                <div style="display: flex; flex-direction: row; width: 100%;">
                    <?php
                    $team_assigned_label->add();
                    $team_assigned->add();
                    ?>
                </div>
            </div>
            <br>
            <?php
            $notes_label->add();
            $notes->add();
            $submit_btn->add();
            ?>
        </div>

    </div>
</form>

<script>
    function assign_team() {
        var status = document.getElementById("status").value;
        if (status == "ASSIGN TEAM") {
            document.getElementById("teams").style.display = "flex";
        } else {
            document.getElementById("teams").style.display = "none";
        }
    }
</script>