<div class="row">
    <div class="col-xs-12 col-sm-6">
        <a href="#" data-active="1" data-action="dajWnioskiKadrowe" data-single="1" data-value="Moje" data-title="Moje wnioski kadrowe"
           class="btn btn-tile btn-danger">
            <i class="fa fa-upload"></i>
            <span>Moje</span>
        </a>
        <a href="#" data-active="0" data-action="dajWnioskiKadrowe" data-single="1" data-value="Wplywajace" data-title="Wnioski do akceptacji"
           class="btn btn-tile btn-danger">
            <i class="fa fa-download"></i>
            <span>Do akceptacji</span>
        </a>
        <a class="btn btn-tile btn-success" id="dodaj_wniosek_kadrowy">
            <i class="fa fa-plus-circle"></i>
            <span>Dodaj wniosek</span>
        </a>
    </div>
    <div class="col-xs-12 col-sm-6 text-right">
        <a href="#" data-filter-active="0" data-filter-action="dajWnioskiKadrowe" data-single="1" data-filter-value="Wszystkie" data-title=" (Wszystkie)"
           class="btn btn-tile btn-danger">
            <i class="fa fa-folder-open"></i>
            <span>Wszystkie</span>
        </a>
        <a href="#" data-filter-active="0" data-filter-action="dajWnioskiKadrowe" data-single="1" data-filter-value="Odrzucony" data-title=" (Odrzucone)"
           class="btn btn-tile btn-danger">
            <i class="fa fa-folder-open"></i>
            <span>Odrzucone</span>
        </a>
        <a href="#" data-filter-active="0" data-filter-action="dajWnioskiKadrowe" data-single="1" data-filter-value="Zaakceptowany" data-title=" (Zaakceptowane)"
           class="btn btn-tile btn-danger" style="width: 110px !important;">
            <i class="fa fa-folder-open"></i>
            <span>Zaakceptowane</span>
        </a>
        <a href="#" data-filter-active="0" data-filter-action="dajWnioskiKadrowe" data-single="1" data-filter-value="Oczekuje" data-title=" (Oczekujące)"
           class="btn btn-tile btn-danger">
            <i class="fa fa-folder-open"></i>
            <span>Oczekujące</span>
        </a>
    </div>
</div>
<div class="row">
    <div class="col-xs-12">
        <div class="box box-danger with-border">
            <?php echo $this->Element('table_loading'); ?>
            <div class="box-body table-responsive" data-container="table">
                <div class="no-data">
                    <p>Brak danych</p>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.page-name:first-of-type').html('<?php echo $pageName; ?>');
    })
</script>

<script>
    $(document).ready(function () {
        if(localStorage.getItem('typ_wniosku_kadrowy') !== "undefined" && localStorage.getItem('typ_wniosku_kadrowy') !== null) {
            var typ_wniosku = localStorage.getItem('typ_wniosku_kadrowy');
            $('[data-single="1"][data-value="'+typ_wniosku+'"]').trigger('click');
            setTimeout(function () {
                if(localStorage.getItem('typ_wniosku_kadrowy_filter') !== "undefined") {
                    var typ_wniosku_filter = localStorage.getItem('typ_wniosku_kadrowy_filter');
                    $('[data-single="1"][data-filter-value="'+typ_wniosku_filter+'"]').trigger('click');
                }
            });
        } else {
            setTimeout(function () {
                $('[data-filter-active]:first-of-type').attr('data-filter-active', 1).trigger('click');
            });
        }
    });
</script>

<script>
    $(document).ready(function () {
        $('#dodaj_wniosek_kadrowy').click(function () {
            loadAjaxView('wyborRodzajuWnioskuKadrowego');
        });

        function generatePageTitle(el1, el2) {
            var title = '';
            var subtitle = '';

            if(el1 != null) {
                title = $(el1).data('title');
                subtitle = $('[data-filter-active="1"]').data('title');
            } else if(el2 != null) {
                title = $('[data-active="1"]').data('title');
                subtitle = $(el2).data('title');
            } else {
                title = $('[data-active="1"]').data('title');
                subtitle = $('[data-filter-active="1"]').data('title');
            }

            if(subtitle != undefined && subtitle.length > 0) {
                title += subtitle;
            } else {
                $('[data-filter-active]:first-of-type').trigger('click');
            }

            $('.page-name').html(title);
            $('.page-name-second').html("");
        }

        setTimeout(function () {
            generatePageTitle();
        }, 500);

        $('[data-active]').click(function () {
            // Zapamiętaj wybrany typ wniosku w localStorage
            var typWniosku = $(this).data('value');
            localStorage.setItem('typ_wniosku_kadrowy', typWniosku);
            generatePageTitle($(this), null);
        });
        $('[data-filter-active]').click(function () {
            // Zapamiętaj wybrany filtr w localStorage
            var typWnioskuFilter = $(this).data('filter-value');
            localStorage.setItem('typ_wniosku_kadrowy_filter', typWnioskuFilter);
            generatePageTitle(null, $(this));
        });
    });
</script>

<script>
    $(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);
    }
</script>

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