<?php
include "../../classes/db.class.php";

$db = new DBMain();

$where_data = [];

$user_id = $_GET['user_id'];
if ($user_id >= 1) {
    $where_data[] = "user_id = " . $user_id . "";
}

$stock_name = $_GET['stock_name'];
if ($stock_name >= 1) {
    $where_data[] = "stock_name = " . $stock_name . "";
}

$qr_code = $_GET['qr_code'];
if (strlen($qr_code) >= 1) {
    $where_data[] = "qr_code LIKE '%" . $qr_code . "%'";
}

$shelf = $_GET['shelf'];
if ($shelf >= 1) {
    $where_data[] = "shelf = " . $shelf . "";
}

$row = $_GET['row'];
if ($row >= 1) {
    $where_data[] = "record_id = " . $row . "";
}

$date = $_GET['date_time_added'];
if (strlen($date) >= 1) {
    $where_data[] = "date_time_added LIKE '%" . $date . "%'";
}

$category = $_GET['category_id'];
if ($category >= 1) {
    $where_data[] = "category_id = " . $category . "";
}

if (count($where_data) > 0) {
    $where = " WHERE " . implode(" AND ", $where_data);
} else {
    $where = " WHERE 1";
}
// echo "SELECT * FROM `stock_items` $where ORDER BY record_id DESC";
$result = $db->conn->query("SELECT * FROM `stock_items` $where ORDER BY record_id DESC");
if ($result->num_rows > 0) {
    while ($data = $result->fetch_assoc()) {
        echo "

        <div class='flex_container_content' style='box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19) ;background-color: #145593; width:80%;padding: 10px;border-radius: 10px; margin-bottom: 10px; margin-left:auto; margin-right:auto; margin-top:2vw;'>
            <div class='flex_container'>
                <div class='flex_container_content'>
                    <input type='text' readonly value='$data[user_id_added]' />
                    <input type='text' readonly value='$data[name]' />
                    <input type='text' readonly value='$data[qr_code]' />
                </div>
                <div class='flex_container_content'>
                    <input type='text' readonly value='$data[shelf]' />
                    <input type='text' readonly value='$data[row]' />
                    <input type='text' readonly value='$data[date_time_added]' />
                </div>
                <div class='flex_container_content'>
                    <input type='text' readonly value='$data[category_id]' />
                </div>

            </div>
            <button onclick='edit_stock($data[record_id])' style='width:100%; background-color: white;' class='font_size_2vw'>SELECT</button>
        </div>
        
        ";
    }
} else {

    echo "        <div class='flex_container_content' style='box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19) ;background-color: #145593; width:80%;padding: 10px;border-radius: 10px; margin-bottom: 10px; margin-left:auto; margin-right:auto; margin-top:2vw;'>
        <h1> NO RESULTS </h1>
        </div>";
}
