<?php
include "../../classes/db.class.php";

$db = new DBMain();

$where_data = [];

$name = $_GET['name'];
if (strlen($name) >= 1) {
    $where_data[] = "name LIKE '%" . $name . "%'";
}

$contact_person = $_GET['contact_person'];
if (strlen($contact_person) >= 1) {
    $where_data[] = "contact_person LIKE '%" . $contact_person . "%'";
}

$number = $_GET['number'];
if (strlen($number) >= 1) {
    $where_data[] = "number LIKE '%" . $number . "%'";
}

$email = $_GET['email'];
if (strlen($email) >= 1) {
    $where_data[] = "email LIKE '%" . $email . "%'";
}

$address = $_GET['address'];
if (strlen($address) >= 1) {
    $where_data[] = "address LIKE '%" . $address . "%'";
}

$status = $_GET['status'];
if (strlen($status) >= 1 && $status != "STATUS") {
    $where_data[] = "status = " . $status;
}

if (count($where_data) > 0) {
    $where = " WHERE " . implode(" AND ", $where_data);
} else {
    $where = " WHERE 1";
}

$result = $db->conn->query("SELECT * FROM `clients` $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[name]' />
                <input type='text' readonly value='$data[contact_person]' />
                <input type='text' readonly value='$data[number]' />
            </div>
            <div class='flex_container_content'>
                <input type='text' readonly value='$data[email]' />
                <input type='text' readonly value='$data[address]' />
                <input type='text' readonly value='$data[status]' />    
            </div>
            </div>
            <button onclick='edit_client($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>";
}
