<?php

$count = 0;

$pietra = get_field('pietra', 'options');

foreach ($pietra as $nr => $pietro) {
    if (!$pietro['zablokuj']) {
        if ($pietro['mieszkania']) {
            foreach ($pietro['mieszkania'] as $mieszkanie) {

                $building_name = strtoupper($mieszkanie['nr'][0]);
                $apartment = substr($mieszkanie['nr'], 1);

                $array[] = [
                    'level_id' => $pietro['id_pietra'],
                    'level_name' => $pietro['nazwa_pietra'],
                    'building' => $building_name,
                    'apartment' => $apartment,
                    'number' => $mieszkanie['nr'],
                    'surface' => $mieszkanie['metraz'],
                    'room' => $mieszkanie['liczba_pokoi'],
                    'terrace' => $mieszkanie['balkon'],
                    'status' => $mieszkanie['status']
                ];
            }
        }
    }
}

foreach($array as $key => $row) {
    if ($building && $row['building'] != $building) {
        unset($array[$key]);
    }

    if ($level && strtolower($row['level_name']) != $level) {
        unset($array[$key]);
    }

    if ($room && $row['room'] != $room) {
        unset($array[$key]);
    }

    if ($status && $row['status'] != $status) {
        unset($array[$key]);
    }

    $surface = (float)(str_replace(",", ".", $row['surface']));

    if ($from <= $surface && $to >= $surface) {

    } else {
        unset($array[$key]);
    }
}
?>

<table class="table-sort">
    <tr>
        <th class="sort">Budynek</th>
        <th class="sort">Piętro</th>
        <th class="sort">Mieszkanie</th>
        <th class="sort">Metraż [m<sup>2</sup>]</th>
        <th class="sort">Pokoje</th>
        <th>Balkon /<br/>Loggia /<br>Ogródek [m<sup>2</sup>]</th>
        <th class="sort">Status</th>
        <th>Karta</th>
        <th>Cena</th>
    </tr>

    <?php foreach ($array as $row) : ?>
        <tr>
            <td><?php echo $row['building'] ?></td>
            <td><?php echo $row['level_name'] ?></td>
            <td><?php echo $row['apartment'] ?></td>
            <td><?php echo $row['surface'] ?></td>
            <td><?php echo $row['room'] ?></td>
            <td><?php echo $row['terrace'] ?></td>
            <td class="<?php echo $row['status'] ?>"><?php echo $row['status'] ?></td>
            <td class="strong"><span class="offer-show-projection" data-id="<?php echo $row['number'] ?>" data-pietro="<?php echo $row['level_id'] ?>"><i class="fa-solid fa-clipboard-list"></i></span></td>
            <td class="strong"><span class="send-message" data-id="<?php echo $row['number'] ?>" data-pietro="<?php echo $row['level_name'] ?>"><span class="button">zapytaj</span></span></td>
        </tr>
        <?php $count++ ?>
    <?php endforeach; ?>

    <?php if (!$array): ?>
        <tr>
            <td colspan="9" class="lack">Brak wyników wyszukiwania</td>
        </tr>
    <?php endif; ?>
</table>

<p>Wyfiltrowano pozycji: <?php echo $count ?> </p>