<?php include "../../root.class.php";
$html = new html();
$html->add_styles_page();
// $html->check_user_type("ADMIN");
$db = new db_safeguard();

//GET THE NUMBER OF ROWS IN THE JOBCARDS TABLE AND ADD 1
if ($db->query("jobcards", "SELECT * FROM jobcards")->num_rows == 0) {
    $jobcard_no = 1;
} else {
    $jobcard_no = $db->query("jobcards", "SELECT * FROM jobcards")->num_rows + 1;
}

//GET THE LAST SLIP NUMBER AND ADD 1
if ($db->query("jobcards", "SELECT slip_no FROM jobcards ORDER BY jc_no DESC LIMIT 1") == 0) {
    $slip_no = 1;
} else {
    $slip_res = $db->query("jobcards", "SELECT slip_no FROM jobcards ORDER BY jc_no DESC LIMIT 1");
    $slip_no = $slip_res->fetch_assoc()['slip_no'] + 1;
}

//get today's date and add 2 hours to the time
$today = new DateTime();
$today->modify('+2 hours');
$current_date = $today->format('Y-m-d' . ' ' . 'H:i');

//get the seeion user id
$user_id = $_SESSION['user_id'];

$created_by = new input();
$created_by->class("inputs");
$created_by->style("display: none;");
$created_by->name("user_id");
$created_by->value($user_id);
$created_by->id("user_id");

$date = new input();
$date->class("inputs");
$date->name("date_created");
$date->style("display: none;");
$date->value($current_date);
$date->id("date_created");

$drill_co_ordinates = new input();
$drill_co_ordinates->class("inputs");
$drill_co_ordinates->style("display: none;");
$drill_co_ordinates->name("drill_co_ordinates");
$drill_co_ordinates->value(0);
$drill_co_ordinates->id("drill_co_ordinates");

$payment_method = new input();
$payment_method->class("inputs");
$payment_method->style("display: none;");
$payment_method->name("payment_method");
$payment_method->value(0);
$payment_method->id("payment_method");

$cash_amount = new input();
$cash_amount->class("inputs");
$cash_amount->style("display: none;");
$cash_amount->name("cash_amount");
$cash_amount->value(0);
$cash_amount->id("cash_amount");

$diesel_start = new input();
$diesel_start->class("inputs");
$diesel_start->style("display: none;");
$diesel_start->name("diesel_start");
$diesel_start->value(0);
$diesel_start->id("diesel_start");

$slip_file = new input();
$slip_file->class("inputs");
$slip_file->style("display: none;");
$slip_file->name("slip_file");
$slip_file->value(0);
$slip_file->id("slip_file");

$diesel_stop = new input();
$diesel_stop->class("inputs");
$diesel_stop->style("display: none;");
$diesel_stop->name("diesel_stop");
$diesel_stop->value(0);
$diesel_stop->id("diesel_stop");

$status = new input();
$status->class("inputs");
$status->style("display: none;");
$status->name("status");
$status->value(0);
$status->id("status");

$date_time_closed = new input();
$date_time_closed->class("inputs");
$date_time_closed->style("display: none;");
$date_time_closed->name("date_time_closed");
$date_time_closed->value(0);
$date_time_closed->id("date_time_closed");

$interested_in_pump = new input();
$interested_in_pump->class("inputs");
$interested_in_pump->style("display: none;");
$interested_in_pump->name("interested_in_pump");
$interested_in_pump->value(0);
$interested_in_pump->id("interested_in_pump");

$water_flow = new input();
$water_flow->class("inputs");
$water_flow->style("display: none;");
$water_flow->name("water_flow");
$water_flow->value(0);
$water_flow->id("water_flow");

$client_merch_image = new input();
$client_merch_image->class("inputs");
$client_merch_image->style("display: none;");
$client_merch_image->name("client_merch_image");
$client_merch_image->value(0);
$client_merch_image->id("client_merch_image");

$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->required();
$jobcard_number->readonly();

$jobcard_number_label = new label();
$jobcard_number_label->for("jc_no_label");
$jobcard_number_label->value("JOBCARD NO");

$client_name = new select();
$client_name->class("inputs");
$client_name->name("client_id");
$client_name->required();
$client_name->id("client_id");
$client_name->fill_from_db("clients", "record_id", "name");

$contact_name_label = new label();
$contact_name_label->for("contact_name_label");
$contact_name_label->value("CONTACT NAME");

$contact_name = new input();
$contact_name->class("inputs");
$contact_name->type("text");
$contact_name->placeholder("CONTACT NAME");
$contact_name->name("contact_name");
$contact_name->required();
$contact_name->id("contact_name");

$address = new input();
$address->class("inputs");
$address->type("text");
$address->placeholder("ADDRESS");
$address->name("address");
$address->required();
$address->id("address");

$phone = new input();
$phone->class("inputs");
$phone->type("text");
$phone->placeholder("PHONE NUMBER");
$phone->name("contact_number");
$phone->required();
$phone->id("contact_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");

$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");

$contact_number_label = new label();
$contact_number_label->for("contact_number");
$contact_number_label->value("CONTACT NUMBER");

$other_number_label = new label();
$other_number_label->for("other_number");
$other_number_label->value("OTHER NUMBER");

$alternate_number_label = new label();
$alternate_number_label->for("alternate_number");
$alternate_number_label->value("ALTERNATE NUMBER");

$user_assigned = new select();
$user_assigned->class("inputs");
$user_assigned->name("user_assigned");
$user_assigned->required();
$user_assigned->id("user_assigned");
$user_assigned->fill_from_db("users", "record_id", "username");

$action_date = new input();
$action_date->class("inputs");
$action_date->type("datetime-local");
$action_date->name("action_date");
$action_date->required();
$action_date->id("action_date");

$action_date_lable = new label();
$action_date_lable->for("action_date");
$action_date_lable->value("ACTION DATE");

$user_assigned_label = new label();
$user_assigned_label->for("user_assigned");
$user_assigned_label->value("USER ASSIGNED");

$team_assigned = new select();
$team_assigned->class("inputs");
$team_assigned->name("team_assigned_id");
$team_assigned->required();
$team_assigned->id("team_assigned_id");
$team_assigned->fill_from_db("teams", "record_id", "name");

$team_assigned_label = new label();
$team_assigned_label->for("team_assigned");
$team_assigned_label->value("TEAM ASSIGNED");

$client_name_label = new label();
$client_name_label->for("client_name");
$client_name_label->value("CLIENT NAME");

$submit_btn = new button();
$submit_btn->value("ADD JOBCARD");
$submit_btn->onclick("add_jobcard()");

$other_number = new input();
$other_number->class("inputs");
$other_number->type("text");
$other_number->placeholder("OTHER NUMBER");
$other_number->name("other_number");
$other_number->required();
$other_number->id("other_number");

$quantity_label = new label();
$quantity_label->for("quantity");
$quantity_label->value("QTY");

$description_label = new label();
$description_label->for("description");
$description_label->value("DESCRIPTION");

$meters_label = new label();
$meters_label->for("meters");
$meters_label->value("METERS");

$amount_qty_label = new label();
$amount_qty_label->for("amount_qty");
$amount_qty_label->value("AMOUNT");

$break_found_label = new label();
$break_found_label->for("break_found");
$break_found_label->value("BREAK FOUND ON");

$break_found = new input();
$break_found->class("inputs");
$break_found->type("number");
$break_found->name("break_found_on");
$break_found->required();
$break_found->id("break_found_on");

$estimated_liters_label = new label();
$estimated_liters_label->for("estimated_liters");
$estimated_liters_label->value("ESTIMATED LITERS (PER HOUR)");

$estimated_liters = new input();
$estimated_liters->class("inputs");
$estimated_liters->type("number");
$estimated_liters->name("estimated_liters");
$estimated_liters->required();
$estimated_liters->id("estimated_liters");

$total_amount_label = new label();
$total_amount_label->for("total_amount_label");
$total_amount_label->value("TOTAL");

$total_amount = new input();
$total_amount->class("inputs");
$total_amount->type("number");
$total_amount->name("total_amount");
$total_amount->required();
$total_amount->onchange("calculate_total()");
$total_amount->id("total_amount");

$deposite_paid_label = new label();
$deposite_paid_label->for("deposite_paid");
$deposite_paid_label->value("DEPOSITE PAID");

$deposite_paid = new input();
$deposite_paid->class("inputs");
$deposite_paid->type("number");
$deposite_paid->name("deposite_amount");
$deposite_paid->required();
$deposite_paid->id("deposite_amount");

$balance_label = new label();
$balance_label->for("balance");
$balance_label->value("BALANCE");

$balance = new input();
$balance->class("inputs");
$balance->type("number");
$balance->name("balance");
$balance->required();
$balance->id("balance");

$total_paid_label = new label();
$total_paid_label->for("total_paid");
$total_paid_label->value("TOTAL PAID");

$total_paid = new input();
$total_paid->class("inputs");
$total_paid->type("number");
$total_paid->name("total_paid");
$total_paid->required();
$total_paid->id("total_paid");

$slip_no_label = new label();
$slip_no_label->for("slip_no_label");
$slip_no_label->value("SLIP NO");

$slip_number = new input();
$slip_number->class("inputs");
$slip_number->type("number");
$slip_number->name("slip_no");
$slip_number->required();
$slip_number->readonly();
$slip_number->id("slip_no");
$slip_number->value($slip_no);

$slip_label = new label();
$slip_label->for("slip");
$slip_label->value("SLIP");

$slip = new file_upload();
$slip->class("inputs");
$slip->name("slip_file");
$slip->id("slip_file");
$slip->file_save_path("../images/slip/");

$invoice = new input();
$invoice->class("inputs");
$invoice->style("display: none;");
$invoice->name("invoice_id");
$invoice->value(0);
$invoice->id("invoice_id");

$deposite_paid = new input();
$deposite_paid->class("inputs");
$deposite_paid->style("display: none;");
$deposite_paid->name("deposite_amount");
$deposite_paid->value(0);
$deposite_paid->id("deposite_amount");

$estimated_liters = new input();
$estimated_liters->class("inputs");
$estimated_liters->style("display: none;");
$estimated_liters->name("estimated_liters");
$estimated_liters->value(0);
$estimated_liters->id("estimated_liters");

$break_found = new input();
$break_found->class("inputs");
$break_found->style("display: none;");
$break_found->name("break_found_on");
$break_found->value(0);
$break_found->id("break_found_on");

$process_label = new label();
$process_label->for("process");
$process_label->value("PROCESS");

$process = new select();
$process->class("inputs");
$process->name("process_id");
$process->required();
$process->id("process_id");
$process->add_option(" ", "SELECT PROCESS");
$process->add_option("1", "WATER DRILLING");
$process->add_option("2", "DRILLING EXCLUDES CASING");
$process->add_option("3", "CASING");
$process->add_option("4", "BOREHOLE PUMP INSTALLATION");
// $process->fill_from_db("process", "record_id", "name");

$add_row_btn = new button();
$add_row_btn->value("ADD ROW");
$add_row_btn->onclick("add_row()");

?>

<div class="form_down">
    <h1>CREATE JOBCARD</h1>

    <script>
        function add_number() {
            //create a input with a placeholder called other
            var other = document.createElement("input");
            other.type = "text";
            other.placeholder = "OTHER";
            other.name = "other";
            other.id = "other";
            other.required = true;
            document.getElementById("other_div").appendChild(other);
        }
    </script>
    <?php
    $jobcard_number_label->add();
    $jobcard_number->add();
    
    $action_date_lable->add();
    $action_date->add();
    $date->add();
    $client_name_label->add();
    $client_name->add();
    $contact_name_label->add();
    $contact_name->add();

    ?>
    <div style="display: flex; width: 45%; justify-content: space-between;">
        <?php
        $contact_number_label->add();
        $other_number_label->add();
        $alternate_number_label->add();
        ?>
    </div>
    <div>
        <?php
        $phone->add();
        $other_number->add();
        $alternate_number->add();
        ?>
    </div>

    <?php
    $address->add();
    $team_assigned_label->add();
    $team_assigned->add();
    $user_assigned_label->add();
    $user_assigned->add();
    $slip_no_label->add();
    $slip_number->add();
    $slip_label->add();
    ?>

    <div id="images">
        <?php

        $slip->add();

        ?>
    </div>

    <?php
    // $process_label->add();
    // $process->add();
    $created_by->add();
    $drill_co_ordinates->add();
    $payment_method->add();
    $cash_amount->add();
    $diesel_start->add();
    $slip_file->add();
    $diesel_stop->add();
    $status->add();
    $date_time_closed->add();
    $interested_in_pump->add();
    $water_flow->add();
    $client_merch_image->add();
    $invoice->add();
    $deposite_paid->add();
    $estimated_liters->add();
    $break_found->add();

    $submit_btn->add();
    ?>
</div>

<?php

$ajax = new js_ajax();
$ajax->function_name("add_jobcard");
$ajax->submit_btn_id("submit");
$ajax->insert("jobcards");
$ajax->on_success("JOBCARD CREATED SUCCESSFULLY");