<?php

include "../../root.class.php";
$db = new db_safeguard();
$call = new call_functions();
$jc_number = (isset($_POST['jc_number']) && $_POST['jc_number'] != "") ? "AND job_card_no = '{$_POST['jc_number']}'" : "";
$po_number = (isset($_POST['po_number']) && $_POST['po_number'] != "") ? "AND order_id = {$_POST['po_number']}" : "";

$user_id = (isset($_POST['user_id']) && $_POST['user_id'] != "") ? "AND user_id = {$_POST['user_id']}" : "";



$jobcard_numbers_res = $db->query("job_cards", "SELECT * FROM job_cards WHERE `status` = 1");
echo "<datalist id='job_cards'>";
while ($jobcard = $jobcard_numbers_res->fetch_assoc()) {
    echo "<option value='" . $jobcard['jc_number'] . "'>" . $jobcard['jc_number'] . "</option>";
}

$jobcard_numbers_res = $db->query("job_cards_tech", "SELECT * FROM job_cards_tech WHERE `status` = 1");
while ($jobcard = $jobcard_numbers_res->fetch_assoc()) {
    echo "<option value='" . $jobcard['jc_number'] . "'>" . $jobcard['jc_number'] . "</option>";
}

$jobcard_accounts_res = $db->query("job_card_accounts", "SELECT * FROM job_card_accounts WHERE `status` = 1");
while ($jobcard_accounts = $jobcard_accounts_res->fetch_assoc()) {
    echo "<option value='" . $jobcard_accounts['name'] . "'>" . $jobcard_accounts['name'] . "</option>";
}
echo "</datalist>";



$sql = "SELECT * FROM order_trans WHERE 1 $jc_number $date_time $user_id $po_number ORDER BY record_id DESC LIMIT 100";
$res = $db->query("order_trans", $sql);
if ($res->num_rows > 0) {
    $index = 0;
    while ($data = $res->fetch_assoc()) {
        // build users_select
        $users_res = $db->query('users', "SELECT * FROM users WHERE record_id='{$data['user_id']}'");
        if ($users_res->num_rows > 0) {
            while ($users_data = $users_res->fetch_assoc()) {
                $select_users .= "<option value='{$users_data['record_id']}'>{$users_data['username']}</option>";
            }
        } else {
            $select_users .= "<option value='0'></option>";

        }
        // build users_select
        $users_res = $db->query('users', "SELECT * FROM users WHERE user_type='WAREHOUSE'");
        if ($users_res->num_rows > 0) {
            while ($users_data = $users_res->fetch_assoc()) {
                $select_users .= "<option value='{$users_data['record_id']}'>{$users_data['username']}</option>";
            }
        } else {
            $select_users .= "<option value='0'>No Data Found</option>";
        }
        // build purchase_orders
        $orders_res = $db->query('orders', "SELECT * FROM orders WHERE record_id='{$data['order_id']}'");
        if ($orders_res->num_rows > 0) {
            while ($orders_data = $orders_res->fetch_assoc()) {
                $select_orders .= "<option value='{$orders_data['record_id']}'>{$orders_data['po_number']}</option>";
            }
        } else {
            $select_orders .= "<option value='0'></option>";

        }
        // build purchase_orders
        $orders_res = $db->query('orders', "SELECT * FROM orders WHERE `status`=0");
        if ($orders_res->num_rows > 0) {
            while ($orders_data = $orders_res->fetch_assoc()) {
                $select_orders .= "<option value='0'></option>";
                $select_orders .= "<option value='{$orders_data['record_id']}'>{$orders_data['po_number']}</option>";
            }
        } else {
            $select_orders = "<option value=''>No Data Found</option>";
        }


        $html .= "
        <tr>
            <td><textarea readonly class='inputs'>" . $call->get_stock_name($data['stock_id']) . "</textarea></td>
            <td><input type='text' class='inputs' value='{$data['amount']}' onchange=\"update_data('amount',this,{$data['record_id']})\" /></td>
            <td><input type='text' class='inputs' value='{$data['date_time']}' readonly/></td>
            <td>
                <select name='user_id' class='inputs' onchange=\"update_data('user_id',this,{$data['record_id']})\">
                    $select_users
                </select>
            </td>
            <td>
             <select name='order' class='inputs' onchange=\"update_data('order_id',this,{$data['record_id']})\">
                $select_orders
            </select>
            </td>
            <td><input type='text' class='inputs' value='{$data['job_card_no']}' list='job_cards' onchange=\"update_data('job_card_no',this,{$data['record_id']})\"/></td>
            <td><input type='text' class='inputs' value='{$data['received']}' onchange=\"update_data('received',this,{$data['record_id']})\"/></td> 
        </tr>
            ";
        $select_orders = '';
        $select_users = '';
        $index++;
    }
} else {
    $html = "<tr><td colspan='5'>No Record Found</td></tr>";
}
echo $html;