<?php
include "../../classes/db.class.php";

$db = new DBMain();

$where_data = [];

$jobcard_number = $_GET['jobcard_number'];
if ($jobcard_number >= 1) {
    $where_data[] = "jobcard_number = " . $jobcard_number . "";
}

$status = $_GET['status'];
if ($status >= 1 && $status != "STATUS") {
    $where_data[] = "status = " . $status;
}


$client_id = $_GET['client_id'];
if ($client_id >= 1) {
    $where_data[] = "client_id = " . $client_id . "";
}

$description = $_GET['description'];
if (strlen($description) >= 1) {
    $where_data[] = "description LIKE '%" . $description . "%'";
}

$date_from = $_GET['date_from'];
if (strlen($date_from) >= 1) {
    $where_data[] = "date_from >= '" . $date_from . "'";
}


$date_to = $_GET['date_to'];
if (strlen($date_to) >= 1) {
    $where_data[] = "date_to >= '" . $date_to . "'";
}


if (count($where_data) > 0) {
    $where = " WHERE " . implode(" AND ", $where_data);
} else {
    $where = " WHERE 1";
}
echo $where;
$result = $db->conn->query("SELECT * FROM `jobcards` $where ORDER BY record_id DESC");
if ($result->num_rows > 0) {
    while ($data = $result->fetch_assoc()) {

        $client_name = $db->get_client_info($data['client_id']);
        $status = $db->get_status($data['status']);
        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[jobcard_number]' />
                <input type='text' readonly value='$status' />
                <input type='text' readonly value='$client_name[name]' />
            </div>
            <div class='flex_container_content'>
                <input type='text' readonly value='$data[datetime_created]' />
                <input type='text' readonly value='$data[date]' />

            </div>
            </div>
            <button onclick='edit_jobcard($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>";
}
