<?php
$wnioski_typ = 'moje';
/*
if(isset($liczbaPracownikow) && $liczbaPracownikow > 0) {
    $wnioski_typ = 'wplywajace';
}
*/
if(isset($view_enter_after_add) && $view_enter_after_add) {
    $wnioski_typ = 'moje';
}
?>

<div class="row">
    <div class="col-xs-12 col-sm-6">
        <a href="#" data-active="<?php echo ($wnioski_typ == 'moje') ? 1 : 0; ?>" data-action="daj_wnioski" data-single="1" data-value="Moje" data-title="Moje wnioski"
           class="btn btn-tile btn-danger">
            <i class="fa fa-upload"></i>
            <span>Moje</span>
        </a>
        <a href="#" data-active="<?php echo ($wnioski_typ == 'wplywajace') ? 1 : 0; ?>" data-action="daj_wnioski" data-single="1" data-value="Wplywajace" data-title="Wpływające wnioski"
           class="btn btn-tile btn-danger">
            <i class="fa fa-download"></i>
            <span>Wpływające</span>
        </a>
        <?php if (!empty($eployees)) : ?>
            <a class="btn btn-tile btn-success" id="dodaj_wniosek">
                <i class="fa fa-plus-circle"></i>
                <span>Dodaj wniosek</span>
            </a>
        <?php else: ?>
            <a class="btn btn-tile btn-success" data-menu-action="dodaj_wniosek" href="/dodaj_wniosek">
                <i class="fa fa-plus-circle"></i>
                <span>Dodaj wniosek</span>
            </a>
        <?php endif; ?>
    </div>
    <div class="col-xs-12 col-sm-6 text-right">
        <a href="#" data-filter-active="0" data-filter-action="daj_wnioski" 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="daj_wnioski" 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="daj_wnioski" 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="daj_wnioski" 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>

<?php if(isset($view_enter) && $view_enter) : ?>
    <script>
        $(function () {
            setTimeout(function () {
                $('[data-filter-active]:first-of-type').attr('data-filter-active', 1).trigger('click');
            });
        });
    </script>
<?php else : ?>
    <script>
        $(document).ready(function () {
            if(localStorage.getItem('typ_wniosku') !== "undefined" && localStorage.getItem('typ_wniosku') !== null) {
                var typ_wniosku = localStorage.getItem('typ_wniosku');
                $('[data-single="1"][data-value="'+typ_wniosku+'"]').trigger('click');
                setTimeout(function () {
                    if(localStorage.getItem('typ_wniosku_filter') !== "undefined") {
                        var typ_wniosku_filter = localStorage.getItem('typ_wniosku_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>
<?php endif; ?>

<script>
    $(document).ready(function () {
        $('#dodaj_wniosek').click(function () {
            $('#modal-wniosek-add').modal('show');
        });

        $('#employees_self_btn').click(function () {
            $('#employees_choose').attr('style', "display: none");
            $('#modal-wniosek-add-ok').attr('href', '/dodaj_wniosek');
            $('#modal-wniosek-add-ok').attr('data-menu-action', 'dodaj_wniosek');
        });

        $('#employees_choose_btn').click(function () {
            $('#employees_choose').attr('style', "display: block");
            $('#modal-wniosek-add-ok').attr('href', '/dodaj_wniosek/wniosek/' + $('#employees option:selected').val());
            $('#modal-wniosek-add-ok').attr('data-menu-action', 'dodaj_wniosek/wniosek/' + $('#employees option:selected').val());
        });

        $('#employees').change(function () {
            $('#modal-wniosek-add-ok').attr('href', '/dodaj_wniosek/wniosek/' + $('#employees option:selected').val());
            $('#modal-wniosek-add-ok').attr('data-menu-action', 'dodaj_wniosek/wniosek/' + $('#employees option:selected').val());
        });

        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 () {
            generatePageTitle($(this), null);
        });
        $('[data-filter-active]').click(function () {
            generatePageTitle(null, $(this));
        });

        if (localStorage.getItem('typ_wniosku_return')) {
            var s = localStorage.getItem('typ_wniosku_return');
            var ss = localStorage.getItem('typ_wniosku_filter_return');

            $('[data-value="'+s+'"]').click();
            setTimeout(function () {
                $('[data-filter-value="'+ss+'"]').click();
                localStorage.removeItem('typ_wniosku_return')
                localStorage.removeItem('typ_wniosku_filter_return')
            });
        }
    });
</script>

<script>
    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'); ?>

<div class="modal fade" id="modal-wniosek-add">
    <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">Dla kogo chcesz złożyć wniosek ?</h4>
            </div>
            <div class="modal-body">
                <div class="form-group">
                    <div class="col-xs-12 col-md-10">
                        <label><input id="employees_self_btn" name="employee" value="" type="radio" checked> SAMEGO SIEBIE</label>
                    </div>
                    <div class="col-xs-12 col-md-10">
                        <label><input id="employees_choose_btn" name="employee" value="choose" type="radio"> PODWŁADNEGO</label>
                    </div>
                </div>
                <div class="form-group" id="employees_choose" style="display: none">
                    <div class="col-xs-12 col-md-10">
                        <label for="employee">Wybierz pracownika:</label>
                        <select name="employee" id="employees">
                            <?php foreach ($eployees as $eployee) { ?>
                                <?php foreach ($eployee as $eploye) { ?>
                                    <?php foreach ($eploye as $eploy) { ?>
                                        <option value="<?php echo $eploy['bNrewid']; ?>"><?php echo $eploy['bNazwaWyswietlana']; ?></option>
                                    <?php } ?>
                                <?php } ?>
                            <?php } ?>
                        </select>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <a data-menu-action="dodaj_wniosek" href="/dodaj_wniosek" class="btn btn-success"
                   id="modal-wniosek-add-ok" data-dismiss="modal">Wypełnij</a>
            </div>
        </div>
    </div>
</div>
