
<?php if (in_array(mb_strtolower($rodzaj), ['dokument', 'inne'])) { ?>
<div class="row">
    <div class="col-xs-12">
        <div class="form-group">
            <label for="kategoriaFilter">Filtruj kategorię:</label>
            <select id="kategoriaFilter" class="form-control">
                <option value="">WSZYSTKIE</option>
                <option value="TEST I">TEST I</option>
                <option value="TEST 2">TEST 2</option>
                <option value="INNE">INNE</option>
            </select>
        </div>
    </div>
</div>
<?php } ?>

<table id="DokumentyTable"
       class="table hover-rows custom-table <?php if (in_array(mb_strtolower($rodzaj), ['dokument', 'inne'])) { ?> dokument-class-table <?php } ?> "
        <?php if (in_array(mb_strtolower($rodzaj), ['dokument', 'inne'])) { ?> data-start-sort="2" <?php } ?>
        data-start-sort-direction="asc"
        data-start-height="50vh">
    <thead>
    <tr class="text-uppercase">
        <th>Nazwa dokumentu</th>
        <th>Data publikacji</th>
        <?php if (in_array(mb_strtolower($rodzaj), ['dokument', 'inne'])) { ?>
            <th>Kategoria dokumentu</th>
        <?php } ?>
        <th class="text-right no-sortable"></th>
    </tr>
    </thead>
    <tbody>
    <?php if (!empty($documents['bDokumenty']['bDokument'])): ?>
        <?php foreach ($documents['bDokumenty']['bDokument'] as $k => $document): ?>
            <tr
                <?php if (!empty($document['bInformacjeDodatkowe'])): ?>
                    class="more-info-append" data-append-to="#more-info-container"
                    data-more-info="<?php echo $document['bInformacjeDodatkowe']; ?>"
                <?php endif; ?>
            >
                <td class="info-addon"><?php echo $document['bNazwa']; ?></td>
                <td><span class="span-date"><?php echo $document['bDataPublikacji']; ?></span></td>
                <?php if (in_array(mb_strtolower($rodzaj), ['dokument', 'inne'])) { ?>
                    <td><?php echo($document['bKategoriaKodOpis'] ?: 'INNE'); ?></td>
                <?php } ?>
                <td class="text-right">
                    <div class="btn-group mr5">
                        <a data-ajax-action="<?php echo $local_url; ?>dokument_historia/<?php echo $document['bGid']; ?>"
                           class="btn btn-success">
                            <i class="fa fa-history"></i>
                        </a>
                        <?php if ($document['bCzyMozePrzegladac'] == 'true'): ?>
                            <a data-open-tab="true"
                               data-href="<?php echo $local_url; ?>dokument/<?php echo $document['bGid']; ?>?rodzaj=<?php echo $document['bRodzaj']; ?>"
                               class="btn btn-primary">
                                <i class="fa fa-eye"></i>
                            </a>
                        <?php endif; ?>
                        <?php if ($document['bCzyMozePobrac'] == 'true'): ?>
                            <a data-open-tab="true"
                               data-href="<?php echo $local_url; ?>dokument/<?php echo $document['bGid']; ?>/download?rodzaj=<?php echo $document['bRodzaj']; ?>"
                               class="btn btn-warning">
                                <i class="fa fa-download"></i>
                                <span><?php echo strtoupper($document['bRozszerzenie']); ?></span>
                            </a>
                        <?php endif; ?>
                        <?php if (($document['bCzyMozeUsunacTresc'] == 'true' or $document['bCzyMozeUsunac'] == 'true')) : ?>
                            <?php if ($document['bCzyMozeUsunacTresc'] == 'true' and $document['bCzyMozeUsunac'] == 'true'): ?>
                                <a class="btn btn-danger dropdown-toggle dropdown-hover" data-toggle="dropdown">
                                    <i class="fa fa-remove"></i>
                                    <i class="fa fa-angle-down"></i>
                                </a>
                                <ul class="dropdown-menu" style="z-index: 1000; position: absolute">
                                    <li>
                                        <a href="#"
                                           onclick="removeDokument('<?php echo $local_url; ?>dokument_remove_doc/<?php echo $document['bGid']; ?>')">Usuń</a>
                                    </li>
                                    <li class="separator"></li>
                                    <li>
                                        <a href="#"
                                           onclick="removeDokument('<?php echo $local_url; ?>dokument_remove/<?php echo $document['bGid']; ?>')">Usuń
                                            treść</a>
                                    </li>
                                </ul>
                            <?php else: ?>
                                <a class="btn btn-danger" href="#"
                                   onclick="removeDokument(<?php echo $local_url; ?><?php if ($document['bCzyMozeUsunacTresc']): ?>dokument_remove<?php else: ?>dokument_remove_doc<?php endif; ?>/<?php echo $document['bGid']; ?>)">
                                    <i class="fa fa-remove"></i>
                                </a>
                            <?php endif; ?>
                        <?php endif; ?>
                        <?php if ($document['bCzyMozeWyslacEmail'] == 'true'): ?>
                            <a data-email-modal="<?php echo $local_url; ?>dokument_email/<?php echo $document['bGid']; ?>/<?php echo $document['bRodzaj']; ?>"
                               class="btn btn-default">
                                <i class="fa fa-envelope"></i>
                            </a>
                        <?php endif; ?>
                    </div>
                    <div class="btn-group">
                        <?php if ($document['bCzyTrescUPO'] == 'true') : ?>
                            <a data-open-tab="true"
                               data-href="<?php echo $local_url; ?>dokument/<?php echo $document['bGid']; ?>?format=upo&rodzaj=<?php echo $document['bRodzaj']; ?>"
                               class="btn btn-primary">
                                <i class="fa fa-eye"></i>
                            </a>
                            <a data-open-tab="true"
                               data-href="<?php echo $local_url; ?>dokument/<?php echo $document['bGid']; ?>/download?format=upo&rodzaj=<?php echo $document['bRodzaj']; ?>"
                               class="btn btn-warning">
                                <i class="fa fa-download"></i> <span>UPO</span>
                            </a>
                        <?php endif; ?>

                        <?php if ($document['bCzyTrescXADES'] == 'true') : ?>
                            <a data-open-tab="true"
                               data-href="<?php echo $local_url; ?>dokument/<?php echo $document['bGid']; ?>/download?format=xades&rodzaj=<?php echo $document['bRodzaj']; ?>"
                               class="btn btn-warning">
                                <i class="fa fa-download"></i> <span>XADES</span>
                            </a>
                        <?php endif; ?>

                        <?php if ($document['bCzyTrescXML'] == 'true') : ?>
                            <a data-open-tab="true"
                               data-href="<?php echo $local_url; ?>dokument/<?php echo $document['bGid']; ?>/download?format=xml&rodzaj=<?php echo $document['bRodzaj']; ?>"
                               class="btn btn-warning">
                                <i class="fa fa-download"></i> <span>XML</span>
                            </a>
                        <?php endif; ?>
                    </div>
                </td>
            </tr>
        <?php endforeach; ?>
    <?php endif; ?>
    </tbody>
</table>
<?php
$subtitle = '';
switch ($rodzaj) {
    case 'PaskiWyplat':
        $subtitle = 'Paski wypłat';
        break;
    case 'DeklaracjePIT':
        $subtitle = 'Deklaracje PIT';
        break;
    case 'RMUA':
        $subtitle = 'RMUA';
        break;
    case 'Dokument':
    case 'Inne':
        $subtitle = 'Inne';
        break;
}
?>
<script>
    $(function () {
        $('.page-name').html("Dokumenty płacowe (<?php echo $subtitle; ?>)");

        // Filter for the 3rd column (Kategoria dokumentu)
        $('#kategoriaFilter').on('change', function() {
            var table = $('#DokumentyTable').DataTable();
            var value = $(this).val();

            // If "WSZYSTKIE" is selected (empty value), clear the filter
            if (value === '') {
                table.column(2).search('').draw();
            } else {
                // Filter for exact match
                table.column(2).search('^' + value + '$', true, false).draw();
            }
        });

        $('[data-ajax-action]').click(function () {
            $('#table-loading2[data-main-loader]').css('visibility', 'visible');
            $('#table-loading2[data-main-loader]').css('opacity', '1');
            $('#table-loading2[data-main-loader]').css('display', 'block');
            var u = $(this).data('ajax-action');
            $.ajax({
                url: u
            }).done(function (resp) {
                $('#table-loading2[data-main-loader]').css('visibility', 'hidden');
                $('#table-loading2[data-main-loader]').css('opacity', '0');
                $('#table-loading2[data-main-loader]').css('display', 'none');
                $('#modal-history-show .modal-body').html(resp);
                $('#modal-history-show').modal('show');
            });
        });

        $('[data-email-modal]').click(function () {
            var u = $(this).data('email-modal');
            $('#modal-email-show input[name="url"]').val(u);
            $('#modal-email-show').modal('show');
        });

        $('.email-send').click(function () {
            var re = /\S+@\S+\.\S+/;
            var email = $('#modal-email-show input[name="email"]').val();
            var url = $('#modal-email-show input[name="url"]').val();

            if (!re.test(email)) {
                showError('adres email niepoprawny');
                return;
            }

            $('#table-loading2[data-main-loader]').css('visibility', 'visible');
            $('#table-loading2[data-main-loader]').css('opacity', '1');
            $('#table-loading2[data-main-loader]').css('display', 'block');


            console.log(email);

            $.ajax({
                url: url,
                data: {
                    email: email
                }
            }).done(function (data) {

                var response = JSON.parse(data);

                $('#table-loading2[data-main-loader]').css('visibility', 'hidden');
                $('#table-loading2[data-main-loader]').css('opacity', '0');
                $('#table-loading2[data-main-loader]').css('display', 'none');

                hideMessages();

                if (response['error'] != undefined) {
                    showError(response['error']);
                } else {
                    showSuccess(response['message']);
                }
            });

            console.log(email + ' - ' + url);
        });

    });
</script>

<div class="modal fade" id="modal-history-show">
    <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">Historia przetwarzania</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 -->

<div class="modal fade" id="modal-email-show">
    <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">Wyślij email</h4>
            </div>
            <div class="modal-body">
                <?php if ($emails['bCzyUmozliwicWpisanieWlasnegoAdresuEmail'] == 'true'): ?>
                    <input type="email" list="email-list" name="email" style="width: 100%"
                           autocomplete="off"
                           placeholder="wpisz email">
                    <datalist id="email-list">
                        <?php
                        foreach ($emails['emails'] as $email) {
                            echo '<option value="' . $email . '">';
                        }
                        ?>
                    </datalist>
                <?php else: ?>
                    <?php if ($emails['bCzyUmozliwicZmianeAdresuEmailWRamachHierarchii'] == 'false'): ?>
                        <input type="email" list="email-list" name="email" style="width: 100%"
                               value="<?php echo $emails['emails'][0]; ?>"
                               readonly>
                    <?php else: ?>
                        <select name="email" style="width: 100%">
                            <?php
                            foreach ($emails['emails'] as $email) {
                                echo '<option value="' . $email . '">' . $email . '</option>';
                            }
                            ?>
                        </select>
                    <?php endif ?>
                <?php endif ?>
                <input type="hidden" name="url">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default email-send" data-dismiss="modal">Wyślij</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

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