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

<div class="row">
    <div class="col-xs-12">
        <div class="loader-overlapping loader-holder" id="table-loading" style="min-height: 140px;">
            <div class="loader"></div>
        </div>
        <div data-container="table">

        </div>
    </div>
</div>

<script>
    $(function () {
        var counter = 0;
        var currentYear = false;
        var onlyAccept = false;
        var rokAkceptacja = parseInt(moment().format('Y'));
        $('.kadry-tiles .btn-tile').click(function () {
            if(counter > 0) $('#table-loading').css('min-height', '0');
            counter++;

            $('.kadry-tiles .btn-tile').attr('data-active', 0);
            $('#table-loading').css('visibility', 'visible');
            $('#table-loading').css('opacity', '1');
            $('#table-loading').css('display', 'block');

            $(this).attr('data-active', 1);

            var url = $(this).data('action');
            localStorage.setItem('kadry_url', url);
            localStorage.setItem('kadry_current_year', currentYear);
            localStorage.setItem('kadry_only_accept', onlyAccept);
            localStorage.setItem('kadry_rok_akceptacja', rokAkceptacja);

            var timestamp = (new Date()).getTime();

            url += '?t=' + timestamp + '&rok-akceptacja=' + rokAkceptacja;

            if(currentYear) {
                url += '&tylko-ten-rok=1';
            }
            if(onlyAccept) {
                url += '&tylko-akceptacja=1';
            }

            $.ajax({
                method: 'GET',
                url: local_url + url
            }).done(function (response) {
                if(trim(response) == 'login') window.location = local_url + 'login';

                $('#table-loading').css('visibility', 'hidden');
                $('#table-loading').css('opacity', '0');
                $('#table-loading').css('display', 'none');
                $('[data-container="table"]').html(response);
            });
        });

        if(localStorage.getItem('kadry_url') !== null) {
            var url = localStorage.getItem('kadry_url');
            if(localStorage.getItem('kadry_current_year') !== null) {
                currentYear = localStorage.getItem('kadry_current_year');
            }
            $('.kadry-tiles .btn-tile[data-action="'+url+'"]').trigger('click');
        } else {
            $('.kadry-tiles .btn-tile:first-of-type').trigger('click');
        }

        $(document).on('click', '#CurrentYear', function () {
            currentYear = $(this).is(':checked');
            $('.kadry-tiles .btn-tile[data-active="1"]').trigger('click');
        });

		$(document).on('click', '#CurrentYear1', function () {
			currentYear = $(this).is(':checked');
			console.log(currentYear);
		});

         $(document).on('click', '#OnlyAccept', function () {
			 onlyAccept = $(this).is(':checked');
            $('.kadry-tiles .btn-tile[data-active="1"]').trigger('click');
        });

        $(document).on('change', '#RokAkceptacja', function () {
            rokAkceptacja = $(this).val();
            $('.kadry-tiles .btn-tile[data-active="1"]').trigger('click');
        });
    });
</script>

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

<style>
    #table-loading {
        opacity: 1;
        display: block;
        visibility: visible;
        width: calc(100% - 30px);
        left: 15px;
    }
    .hidden-messages-toggle {
        position: relative;
    }
    label.switch-current-year {
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        text-align: right;
    }
    label.switch-current-year input {
        margin: 10px 5px 0 0;
    }
</style>

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