<?php
// Default value for $boxType if not defined
if (!isset($boxType)) {
    $boxType = 'Wszystkie';
}

function echo_message_receivers($message, $count = null)
{
    if ($count != null) {
        for ($i = 0; $i <= $count; $i++):
            if (isset($message['bOdbiorcy']['bAdres'][$i])) {
                echo $message['bOdbiorcy']['bAdres'][$i]['bNazwaWyswietlana'];
                if (isset($message['bOdbiorcy']['bAdres'][$i + 1])) {
                    echo ', ';
                }
            }
        endfor;
    } else {
        foreach ($message['bOdbiorcy']['bAdres'] as $k => $receiver):
            echo $receiver['bNazwaWyswietlana'];
            if (isset($message['bOdbiorcy']['bAdres'][$k + 1])) {
                echo ',';
            }
        endforeach;
    }
}

?>

<?php
$startSort = 0;
switch ($boxType) {
    case 'Robocze':
        $startSort = 2;
        break;
    case 'Odbiorcza':
        $startSort = 3;
        break;
    case 'Nadawcza':
        $startSort = 2;
        break;
    case 'Wszystkie':
        $startSort = 3;
        break;
}
?>
<?php
$protocol = stripos($_SERVER['SERVER_PROTOCOL'],'https') === true ? 'https://' : 'http://';
$homePath = $protocol . $_SERVER['SERVER_NAME'] .'/index';
$isHomePage = ($_SERVER['HTTP_REFERER'] == $homePath) ? true : false;
?>

<table id="MessagesTable" data-messages-homepage="<?php echo $isHomePage; ?>" class="table hover-rows custom-table" data-start-sort="<?php echo $startSort; ?>" data-start-sort-direction="desc" <?php if($isHomePage) : ?>data-start-height="50vh"<?php else : ?>data-start-height="60vh"<?php endif; ?>>
    <thead>
    <tr class="text-uppercase">
        <th></th>
        <th>Wiadomość</th>
        <?php if($boxType != 'Odbiorcza' && $boxType != 'Nadawcza' && $boxType != 'Robocze' && $boxType != 'Wszystkie') : ?>
            <th>Odbiorca</th>
        <?php endif; ?>
        <?php if ($boxType != 'Robocze'): ?>
            <th>Data publikacji</th>
        <?php endif; ?>
        <?php if ($boxType == "Odbiorcza" || $boxType == 'Wszystkie' || $boxType == 'Nadawcza'): ?>
            <th>Data odebrania</th>
        <?php endif; ?>
        <?php if ($boxType == "Nadawcza" || $boxType == 'Wszystkie' || $boxType == 'Robocze'): ?>
            <th>Odbiorcy</th>
            <?php if($boxType == 'Robocze') : ?>
                <th>Data utworzenia</th>
            <?php endif; ?>
        <?php else: ?>
            <?php if ($boxType != 'Odbiorcza'): ?>
                <th>Data utworzenia</th>
            <?php endif; ?>
        <?php endif; ?>
        <th data-orderable="false" class="no-sortable"></th>
    </tr>
    </thead>
    <tbody>
    <?php if (!empty($messages['bWiadomosci']['bWiadomosc'])): ?>
        <?php foreach ($messages['bWiadomosci']['bWiadomosc'] as $message): ?>
            <?php $isHidden = ($message['bCzyUkryta'] == false || $message['bCzyUkryta'] == 'false') ? false : true; ?>
            <?php $isRead = ((is_bool($message['bCzyOdebrana']) && $message['bCzyOdebrana'] == true) || ($message['bCzyOdebrana'] == 'true')) ? (bool)true : (bool)false; ?>
            <tr class="message<?php if (!$isRead) echo '-not'; ?>-read">
                <td class="text-center" width="20px" style="vertical-align: middle;">
                    <span class="hidden"><?php echo $message['bPriorytet']; ?></span>
                    <?php if (isset($message['bZalaczniki']['bZalacznik']) && !empty($message['bZalaczniki']['bZalacznik'])): ?>
                        <i class="fa fa-paperclip" style="font-size: 18px; color: <?php echo $message['bKolorWypelnieniaObrazkaHTML'] ?? '#333'; ?>"></i>
                    <?php endif; ?>
                    <?php if ($message['bPriorytet'] == 'Wysoki'): ?>
                        <i class="fa fa-exclamation" style="font-size: 18px; color: <?php echo $message['bKolorWypelnieniaObrazkaHTML'] ?? '#333'; ?>"></i>
                    <?php endif; ?>
                    <?php if ($message['bPriorytet'] == 'Najwyzszy'): ?>
                        <i class="fa fa-exclamation" style="font-size: 18px; color: <?php echo $message['bKolorWypelnieniaObrazkaHTML'] ?? '#333'; ?>"></i>
                    <?php endif; ?>
                </td>
                <td<?php if(!$isRead): ?> style="font-weight: bold" <?php endif; ?>>
                    <div style="display: inline-block;">
                        <div><?php echo $message['bNadawca']['bNazwaWyswietlana']; ?></div>
                        <div><?php echo $message['bTytul']; ?></div>
                    </div>
                </td>
                <?php if($boxType != 'Odbiorcza' && $boxType != 'Nadawcza' && $boxType != 'Robocze' && $boxType != 'Wszystkie') : ?>
                    <td>
                        <?php if(isset($message['bOdbiorcy']) && !empty($message['bOdbiorcy'])) : ?>
                            <?php foreach ($message['bOdbiorcy']['bAdres'] as $odbiorcaAdres) : ?>
                                <div><?php echo $odbiorcaAdres['bNazwaWyswietlana']; ?></div>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </td>
                <?php endif; ?>
                <?php if ($boxType != 'Robocze'): ?>
                    <td<?php if(!$isRead): ?> style="font-weight: bold" <?php endif; ?>><?php echo $message['bDataPublikacji']; ?></td>
                <?php endif; ?>
                <?php if ($boxType == 'Odbiorcza' || $boxType == 'Wszystkie' || $boxType == 'Nadawcza'): ?>
                    <td<?php if(!$isRead): ?> style="font-weight: bold" <?php endif; ?>><?php echo $message['bDataOdebrania']; ?></td>
                <?php endif; ?>
                <?php if ($boxType == "Nadawcza" || $boxType == 'Wszystkie' || $boxType == 'Robocze'): ?>
                    <td>
                        <?php
                        echo_message_receivers($message, 3);
                        if (isset($message['bOdbiorcy']['bAdres'][4])): ?>
                            <a class="show-sent-receivers" href="#"
                               data-receivers="<?php echo_message_receivers($message); ?>">
                                Więcej
                            </a>
                        <?php endif; ?>
                    </td>
                    <?php if ($boxType == 'Robocze'): ?>
                        <td><?php echo $message['bDataUtworzenia']; ?></td>
                    <?php endif; ?>
                <?php else: ?>
                    <?php if ($boxType != 'Odbiorcza'): ?>
                        <td><?php echo $message['bDataUtworzenia']; ?></td>
                    <?php endif; ?>
                <?php endif; ?>
                <td class="text-right table-actions">
                    <a class="btn btn-success open-message" rel="tooltip" data-placement="left" title="Zobacz"
                       data-gid="<?php echo $message['bGid']; ?>">
                        <span><i class="fa fa-eye"></i></span>
                    </a>
                    <?php if ($boxType == 'Robocze'): ?>
                        <a class="btn btn-primary" data-menu-action="edytuj_wiadomosc/<?php echo $message['bGid']; ?>" href="/edytuj_wiadomosc/<?php echo $message['bGid']; ?>" rel="tooltip" data-placement="left" title="Edytuj">
                            <span><i class="fa fa-pencil"></i></span>
                        </a>
                    <?php endif; ?>
                    <?php if (!$isHidden): ?>
                        <a rel="tooltip" data-placement="left" title="Ukryj" class="btn btn-warning" data-hide="<?php echo $message['bGid']; ?>">
                            <span><i class="fa fa-toggle-off"></i></span>
                        </a>
                    <?php endif; ?>
                </td>
            </tr>
        <?php endforeach; ?>
    <?php endif; ?>
    </tbody>
</table>
<div class="modal fade" id="sent-receivers-modal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Odbiorcy</h4>
            </div>
            <div class="modal-body">

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Zamknij</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<?php echo $this->element('alert_close'); ?>

<script>
    $(document).ready(function () {
        $('.page-name-second').html($('[data-active="1"] span').html());
    });
    $('[data-active]').click(function () {
        $('.page-name-second').html($('[data-active="1"] span').html());
    });

    $(function(){
        const elemsData = document.querySelectorAll('[data-menu-action]');
        elemsData.forEach(elem => elem.onclick = testFun);
    });

    function testFun(e) {
        e.preventDefault();

        $(this).parents('.sidebar').find('li.active:not(.treeview)').removeClass('active');
        $(this).parents('li').addClass('active');

        var url = $(this).data('menu-action');
        localStorage.setItem('menu_action', url);

        loadAjaxView(url);
    }

    // Remove font-weight: bold style from all td elements in the parent tr when the "Zobacz" button is clicked
    $('.open-message').on('click', function() {
        $(this).closest('tr').find('td').css('font-weight', '');
    });
</script>
