<?php include "../../root.class.php";
$html = new html();
$html->add_styles_page();
$html->prevent_enter_script();
$db = new db_safeguard();


$record_id = new input();
$record_id->name("record_id");
$record_id->id("record_id");
$record_id->class("inputs");
$record_id->value($_GET['record_id']);
$record_id->type("hidden");
$record_id->required();

$jc_number = new input();
$jc_number->name("jc_number");
$jc_number->id("jc_number");
$jc_number->class("inputs");
$jc_number->placeholder("JOB CARD NUMBER");
$jc_number->type("text");
$jc_number->value_from_db("job_cards", "jc_number", "record_id = {$_GET['record_id']}");
$jc_number->required();

$jobcard_number = new input();
$jobcard_number->name("job_card_no");
$jobcard_number->id("job_card_no");
$jobcard_number->class("inputs");
$jobcard_number->placeholder("JOB CARD NUMBER");
$jobcard_number->type("hidden");
$jobcard_number->value_from_db("job_cards", "jc_number", "record_id = {$_GET['record_id']}");
$jobcard_number->required();


$project_name = new input();
$project_name->name("project_name");
$project_name->id("project_name");
$project_name->class("inputs");
$project_name->placeholder("PROJECT NAME");
$project_name->type("text");
$project_name->value_from_db("job_cards", "project_name", "record_id = {$_GET['record_id']}");
$project_name->required();

$status = new select();
$status->name("status");
$status->id("status");
$status->class("inputs");
$status->required();
$status->add_option("", "STATUS");
$status->add_option("1", "ACTIVE");
$status->add_option("0", "INACTIVE");
$status->value_from_db("job_cards", "status", "record_id = {$_GET['record_id']}");

$account_manager_id = new select();
$account_manager_id->name("account_manager_id");
$account_manager_id->id("account_manager_id");
$account_manager_id->class("inputs");
$account_manager_id->add_option("", "PROJECT MANAGER");
$account_manager_id->fill_from_db("users", "record_id", "username");
$account_manager_id->required();
$account_manager_id->value_from_db("job_cards", "account_manager_id", "record_id = {$_GET['record_id']}");

$submit = new button();
$submit->name("submit");
$submit->id("submit");
$submit->class("submit_btn");
$submit->value("EDIT");
$submit->onclick("edit_jobcard()");


?>

<div class="form_down">
    <h2>EDIT JOB CARD</h2>
    <?php
    $record_id->add();
    $jc_number->add();
    $project_name->add();
    $status->add();
    $account_manager_id->add();
    $submit->add();
    ?>
</div>



<?php
$ajax = new js_ajax();
$ajax->function_name("edit_jobcard");
$ajax->submit_btn_id("submit");
$ajax->update("job_cards");
$ajax->on_success("SUCCESSFULLY UPDATED THE JOB CARD");
$ajax->redirect("jobcard_home.php");

$call = new call_functions();

$jobcard_res = $db->query("job_cards", "SELECT * FROM job_cards WHERE record_id = {$_GET['record_id']}");
$jobcard = $jobcard_res->fetch_assoc();
$order_trans_res = $db->query("order_trans", "SELECT * FROM order_trans WHERE job_card_no = '{$jobcard['jc_number']}' ORDER BY record_id DESC");
?>
<br>
<div class="form_down">
    <h1>DISPATCHED</h1>
    <div class="row">
        <input type="text" class="inputs" style="background-color:grey; color:white;" value="DISPATCH" readonly/>
        <input type="text" class="inputs" style="border:1px solid lightgreen;background-color:lightgreen; color:white;"
            value="RETURNS" readonly/>
    </div>
    
    <?php
    while ($order_trans = $order_trans_res->fetch_assoc()) {
        $stock_res = $db->query("stock", "SELECT * FROM stock WHERE record_id = {$order_trans['stock_id']}");
        $stock = $stock_res->fetch_assoc();
        $stock_desctioption = $stock['description'];
        $date_time = $order_trans['date_time'];
        $user = $call->get_username($order_trans['user_id']);
        $amount = $order_trans['amount'];
        ?>
        <div
            style="display:flex;flex-direction: row;flex-wrap: wrap; border-radius:2vw;align-content: center;justify-content: flex-start;align-items: center; <?php if ($amount > 0) {
                echo "background-color:lightgreen;";
            } else {
                echo "background-color:grey;";
            } ?>;">
            <textarea class="inputs" style='min-width:20vw;min-height:10vh;'
                readonly><?php echo $stock_desctioption; ?></textarea>
            <input class="inputs" style='max-width:10vw;' readonly value="<?php echo $amount; ?>">
            <input class="inputs" style='max-width:15vw;' readonly value="<?php echo $date_time; ?>">
            <input class="inputs" style='max-width:40vw;' readonly value="<?php echo $user; ?>">
        </div>
        <br><br>
        <?php
    }
    ?>
</div>


<?php
$jobcard_res = $db->query("job_cards", "SELECT * FROM job_cards WHERE record_id = {$_GET['record_id']}");
$jobcard = $jobcard_res->fetch_assoc();
$order_res = $db->query("orders", "SELECT * FROM orders WHERE jc_number = '{$jobcard['jc_number']}' ORDER BY record_id DESC");
?>
<br>
<div class="form_down">
    <h1>RECEIVED</h1>
    <?php
    while ($order = $order_res->fetch_assoc()) {
        $order_trans_res = $db->query("order_trans", "SELECT * FROM order_trans WHERE order_id = '{$order['record_id']}' ORDER BY record_id DESC");
        while ($order_trans = $order_trans_res->fetch_assoc()) {
            $stock_res = $db->query("stock", "SELECT * FROM stock WHERE record_id = {$order_trans['stock_id']}");
            $stock = $stock_res->fetch_assoc();
            $stock_desctioption = $stock['description'];
            $date_time = $order_trans['date_time'];
            $user = $call->get_username($order_trans['user_id']);
            $amount = $order_trans['amount'];
            ?>
            <div
                style="display:flex;flex-direction: row;flex-wrap: wrap;align-content: center;justify-content: flex-start;align-items: center;">
                <textarea class="inputs" style='min-width:20vw;min-height:10vh;'
                    readonly><?php echo $stock_desctioption; ?></textarea>
                <input class="inputs" style='max-width:20vw;' readonly value="<?php echo $amount; ?>">
                <input class="inputs" style='max-width:10vw;' readonly value="<?php echo $date_time; ?>">
                <input class="inputs" style='max-width:15vw;' readonly value="<?php echo $user; ?>">
                <input class="inputs" style='max-width:40vw;' readonly value="<?php echo $user; ?>">
            </div>
            <br><br>
            <?php
        }
    }

    ?>
</div>