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

echo $_SESSION['user_id'];

// check user type
$user_type_res = $db->query("users", "SELECT user_type FROM users WHERE record_id = '{$_SESSION['user_id']}'");

$user_type = $user_type_res->fetch_assoc()['user_type'];

if ($user_type == 'SUPPLIER') {

    $client_requests = $db->query("delivery_request", "SELECT * FROM delivery_request WHERE status = 'PENDING' AND user_id = '{$_SESSION['user_id']}'");

    if ($request_res->num_rows > 0) {
        while ($request_data = $request_res->fetch_assoc()) {
            $data_table = $data_table . "
            <tr>
                <td>{$request_data['supplier_batch_no']}</td>
                <td>{$request_data['date_created']}</td>
                <td>{$request_data['contact_number']}</td>
                <td>{$request_data['global_gap_no']}</td>
                <td>{$request_data['status']}</td>
                <td>
                <input type='submit' value='PDF' class='submit_btn' onclick='window.location.href = \"delivery_request.pdf.php?supplier_batch_no={$request_data['supplier_batch_no']}\"'>
                </td>
            </tr>
        ";
        }
    } else {
        $data_table = "
        <tr>
                <h2>NO JOBCARDS FOUND</h2>
            </div>
        </div>
        ";
    }
} else if ($user_type == 'ADMIN') {
    $request_res = $db->query("delivery_request", "SELECT * FROM delivery_request WHERE status = 'PENDING'");

    if ($request_res->num_rows > 0) {
        while ($request_data = $request_res->fetch_assoc()) {
            $data_table = $data_table . "
            <tr>
                <td>{$request_data['supplier_batch_no']}</td>
                <td>{$request_data['date_created']}</td>
                <td>{$request_data['contact_number']}</td>
                <td>{$request_data['global_gap_no']}</td>
                <td>{$request_data['status']}</td>
                <td>
                <input type='submit' value='CHECK' class='submit_btn' onclick='window.location.href = \"delivery_request_approval.php?supplier_batch_no={$request_data['supplier_batch_no']}\"'>
                </td>
            </tr>
        ";
        }
    } else {
        $data_table = "
        <tr>
                <h2>NO JOBCARDS FOUND</h2>
            </div>
        </div>
        ";
    }
} elseif ($user_type == 'QA') {
    $request_res = $db->query("delivery_request", "SELECT * FROM delivery_request WHERE status = 'SEND SAMPLE TO QA'");

    if ($request_res->num_rows > 0) {
        while ($request_data = $request_res->fetch_assoc()) {
            $data_table = $data_table . "
            <tr>
                <td>{$request_data['supplier_batch_no']}</td>
                <td>{$request_data['date_created']}</td>
                <td>{$request_data['contact_number']}</td>
                <td>{$request_data['global_gap_no']}</td>
                <td>{$request_data['status']}</td>
                <td>
                <input type='submit' value='PDF' class='submit_btn' onclick='window.location.href = \"delivery_request.pdf.php?supplier_batch_no={$request_data['supplier_batch_no']}\"'>
                </td>
            </tr>
        ";
        }
    } else {
        $data_table = "
        <tr>
                <h2>NO JOBCARDS FOUND</h2>
            </div>
        </div>
        ";
    }
}


?>

<style>
    body {
        background-image: url('');
        /* background-color: white; */
        background-repeat: repeat-y;
    }

    table {
        width: 95%;
        border-collapse: collapse;
    }

    th,
    td {
        padding-left: 10px;
        border: 3px solid black;
        text-align: left;
        background-color: white;
    }
</style>
<div class="form_down">

    <h1>REQUEST STATUS</h1>
    <table style='width:90%; text-align:center;'>
        <tr>
            <td style='font-size:1.5vw;'>
                SUPPLIER BATCH NO:
            </td>
            <td style='font-size:1.5vw;'>
                DATE REQUESTED:
            </td>
            <td style='font-size:1.5vw;'>
                CONTACT NUMBER:
            </td>
            <td style='font-size:1.5vw;'>
                GLOBAL GAP NO:
            </td>
            <td>
                STATUS:
            </td>
            <td>
                REQUEST PDF:
            </td>
        </tr>

        <?php echo $data_table; ?>
    </table>
    <br>
</div>