<?php
$formatter = function ($str) {

    if (is_array($str)) {
        return '';
    }

    return $str;
};
?>
<div class="row">
    <div class="col-xs-12">
        <div class="box box-danger mb0">
            <div class="row">
                <div class="col-xs-4">
                    <div class="pull-left" style="margin: 7px;">
                        <label>
                            <input type="text" name="Search" id="search">
                            <i class="fa fa-search"></i>
                        </label>
                    </div>
                </div>
                <div class="col-xs-8">
                    <div class="pull-right">
                        <div class="btn">
                            <span style="margin-right: 10px;">Rok</span>
                            <label>
                                <select name="RokAkceptacja" id="RokAkceptacja"></select>
                            </label>
                        </div>
                        <a class="btn btn-danger btn-flat hidden-messages-toggle">
                            <span style="margin-right: 10px;">Tylko do akceptacji</span>
                            <label class="switch switch-current-year">
                                <input type="checkbox" name="OnlyAccept"
                                       id="OnlyAccept" <?php if (isset($_GET['tylko-akceptacja'])) echo 'checked="checked"'; ?> />
                            </label>
                        </a>
                    </div>
                </div>
            </div>
            <div class="box-body table-responsive table-fixed-height">
                <table id="PlanUrlopuTable" class="table hover-rows custom-table FullTable" data-start-sort="0"
                       data-start-sort-direction="desc" data-start-height="50vh">
                    <thead>
                    <tr class="text-uppercase">
                        <th>Nr pracownika</th>
                        <th>Nazwisko i imię</th>
                        <th>Data od</th>
                        <th>Data do</th>
                        <th>Kod urlopu</th>
                        <th>Nazwa urlopu</th>
                        <th>Dni urlopu</th>
                        <th>Dni zaplanowanych</th>
                        <th>Dni do zaplanowania</th>
                        <th>Status</th>
                        <th title="Akceptacja kierownika">Akcept. kierownika</th>
                        <th title="Akceptacja administrator">Akcept. administratora</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php $acceptAllManagers = false;
                    $declineAllManagers = false;
                    $acceptAllAdmins = false;
                    $declineAllAdmins = false; ?>
                    <?php if (isset($free_days['bPlanyUrlopu']['bPlanUrlopu'])): ?>
                        <?php foreach ($free_days['bPlanyUrlopu']['bPlanUrlopu'] as $k => $fd): ?>
                        <?php $akceptacja = $_GET['tylko-akceptacja'] ?? 0; ?>
                            <?php if (!($akceptacja == 1) || $fd['bCzyMozeZaakceptowacJakoPrzelozony'] == 'true' || $fd['bCzyMozeZaakceptowacJakoAdministrator'] == 'true'): ?>

                                <tr>
                                    <td class="info-addon search-nr"><?php echo $fd['bNumerEwidencyjny']; ?></td>
                                    <td class="info-addon search-val"><?php echo $fd['bNazwiskoIImie']; ?></td>
                                    <td><span class="span-date"><?php echo $fd['bDataOd']; ?></span></td>
                                    <td><span class="span-date"><?php echo $fd['bDataDo']; ?></span></td>
                                    <td class="info-addon"><?php echo $fd['bKod']['cNumer']; ?></td>
                                    <td class="info-addon"><?php echo $fd['bKod']['cOpis']; ?></td>
                                    <td><?php echo intval($fd['bDni']); ?></td>
                                    <td><?php echo intval($fd['bUrlopZaplanowany']); ?></td>
                                    <td><?php echo intval($fd['bUrlopDoZaplanowania']); ?></td>
                                    <td><?php echo $formatter($fd['bOpisAktualnegoStatusu']); ?></td>
                                    <td class="text-right">
                                        <?php


                                        $info = array(
                                            'gidDokumentu' => $fd['bGid'],
                                            'czyAdmin' => 'false',
                                            'czyOdrzucenie' => 'false');
                                        ?>


                                        <?php if ($fd['bCzyMozeZaakceptowacJakoPrzelozony'] == 'true') :
                                            $info['czyAdmin'] = 'false';
                                            $info['czyOdrzucenie'] = 'false';
                                            $acceptAllManagers = true;
                                            ?>

                                            <a rel="tooltip" data-placement="left"
                                               title="Akceptuj wniosek"
                                               data-method="make-action" class="btn btn-warning buttonAcceptManager"
                                               data-url="akceptuj_odrzuc_wniosek/<?php echo $info['gidDokumentu'] ?>/<?php echo $info['czyAdmin'] ?>/<?php echo $info['czyOdrzucenie'] ?>"
                                               data-gid="<?php echo $info['gidDokumentu'] ?>">
                                                <i class="fa fa-thumbs-up green-text"></i>
                                            </a>

                                        <?php endif; ?>

                                        <?php if ($fd['bCzyMozeOdrzucicJakoPrzelozony'] == 'true') :
                                            $info['czyAdmin'] = 'false';
                                            $info['czyOdrzucenie'] = 'true';
                                            $declineAllManagers = true;
                                            ?>

                                            <a rel="tooltip" data-placement="left" title="Odrzuć wniosek"
                                               data-method="make-action" class="btn btn-warning buttonDeclineManager"
                                               data-url="akceptuj_odrzuc_wniosek/<?php echo $info['gidDokumentu'] ?>/<?php echo $info['czyAdmin'] ?>/<?php echo $info['czyOdrzucenie'] ?>"
                                               data-gid="<?php echo $info['gidDokumentu'] ?>">
                                                <i class="fa fa-thumbs-down red-text"></i>
                                            </a>
                                        <?php endif; ?>
                                    </td>


                                    <td class="text-right">
                                        <?php if ($fd['bCzyMozeZaakceptowacJakoAdministrator'] == 'true') :
                                            $info['czyAdmin'] = 'true';
                                            $info['czyOdrzucenie'] = 'false';
                                            $acceptAllAdmins = true;
                                            ?>

                                            <a rel="tooltip" data-placement="left"
                                               title="Akceptuj wniosek"
                                               data-method="make-action" class="btn btn-warning buttonAcceptAdmin"
                                               data-url="akceptuj_odrzuc_wniosek/<?php echo $info['gidDokumentu'] ?>/<?php echo $info['czyAdmin'] ?>/<?php echo $info['czyOdrzucenie'] ?>"
                                               data-gid="<?php echo $info['gidDokumentu'] ?>">
                                                <i class="fa fa-thumbs-up green-text"></i>
                                            </a>
                                        <?php endif; ?>

                                        <?php if ($fd['bCzyMozeOdrzucicJakoAdministrator'] == 'true') :
                                            $info['czyAdmin'] = 'true';
                                            $info['czyOdrzucenie'] = 'true';
                                            $declineAllAdmins = true;
                                            ?>

                                            <a rel="tooltip" data-placement="left" title="Odrzuć wniosek"
                                               data-method="make-action" class="btn btn-warning buttonDeclineAdmin"
                                               data-url="akceptuj_odrzuc_wniosek/<?php echo $info['gidDokumentu'] ?>/<?php echo $info['czyAdmin'] ?>/<?php echo $info['czyOdrzucenie'] ?>"
                                               data-gid="<?php echo $info['gidDokumentu'] ?>">
                                                <i class="fa fa-thumbs-down red-text"></i>
                                            </a>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    <?php endif; ?>
                    </tbody>
                    <tfoot>
                    <tr>
                        <th>AKCEPTUJ/ODRZUĆ WNIOSKI:</th>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th class="text-right">


                            <?php if ($acceptAllManagers == true) : ?>

                                <a id="buttonAcceptAllManager" rel="tooltip" data-placement="left"
                                   title="Akceptuj wszystkie wnioski" class="btn btn-warning">
                                    <i class="fa fa-thumbs-up green-text"></i>
                                </a>

                            <?php endif; ?>
                            <?php if ($declineAllManagers == true) : ?>
                                <a id="buttonDeclineAllManager" rel="tooltip" data-placement="left"
                                   title="Odrzuć wszystkie wnioski" class="btn btn-warning">
                                    <i class="fa fa-thumbs-down red-text"></i>
                                </a>
                            <?php endif; ?>
                        </th>
                        <th class="text-right">
                            <?php if ($acceptAllAdmins == true) : ?>
                                <a id="buttonAcceptAllAdmin" rel="tooltip" data-placement="left"
                                   title="Akceptuj wszystkie wnioski" class="btn btn-warning">
                                    <i class="fa fa-thumbs-up green-text"></i>
                                </a>
                            <?php endif; ?>
                            <?php if ($declineAllAdmins == true) : ?>
                                <a id="buttonDeclineAllAdmin" rel="tooltip" data-placement="left"
                                   title="Odrzuć wszystkie wnioski" class="btn btn-warning">
                                    <i class="fa fa-thumbs-down red-text"></i>
                                </a>
                            <?php endif; ?>
                        </th>
                    </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
</div>
<div class="row more-info-container">
    <div class="col-xs-12">
        <div class="box">
            <div class="box-header">
                <h2 class="box-title">Informacje dodatkowe</h2>
            </div>
            <div class="box-body" id="more-info-container">

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

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

        // initiateDataTable();

        var select = $("#RokAkceptacja");
        var year = parseInt(moment().format('Y'));
        var selected = localStorage.getItem('kadry_rok_akceptacja');

        for (var i = 10; i > 0; i--) {
            select.append('<option value="' + (year - i) + '" ' + ((year - i) == selected ? 'selected' : '') + '>' + (year - i) + '</option>');
        }

        select.append('<option value="' + year + '" ' + ((year) == selected ? 'selected' : '') + '>' + year + '</option>');

        for (var i = 1; i < 3; i++) {
            select.append('<option value="' + (year + i) + '" ' + ((year + i) == selected ? 'selected' : '') + '>' + (year + i) + '</option>');
        }

        var buttonsAM = document.getElementsByClassName('buttonAcceptManager');
        var buttonsDM = document.getElementsByClassName('buttonDeclineManager');
        var buttonsAA = document.getElementsByClassName('buttonAcceptAdmin');
        var buttonsDA = document.getElementsByClassName('buttonDeclineAdmin');

        function all_akceptuj_wniosek(ids, czyAdmin, czyOdrzucony) {
            $.ajax({
                url: local_url + 'akceptuj_odrzuc_wniosek/post/' + czyAdmin + '/' + czyOdrzucony,
                type: 'POST',
                data: {
                    gid: ids
                }
            }).done(function (response) {
                var r = response;
                response = JSON.parse(response);
                if (trim(r) == 'login') {
                    window.location = local_url + 'login';
                } else {
                    hideMessages();
                    if (response['error'] != undefined) {
                        showError(response['error']);
                    } else {
                        showSuccess(response['message']);
                    }
                    loadAjaxView(localStorage.getItem('menu_action'));
                }
            });
        }

        $("#buttonAcceptAllManager").click(function () {
            var ids = [];
            for (var i = 0; i <= buttonsAM.length; i++) {
                ids.push($(buttonsAM[i]).data('gid'));
            }
            if (confirm('Czy na pewno chcesz wszystkie zaakceptować?')) {
                all_akceptuj_wniosek(ids, 'false', 'false');
            }
        });

        $("#buttonDeclineAllManager").click(function () {
            var ids = [];
            for (var i = 0; i <= buttonsDM.length; i++) {
                ids.push($(buttonsDM[i]).data('gid'));
            }
            if (confirm('Czy na pewno chcesz wszystkie odrzucić?')) {
                all_akceptuj_wniosek(ids, 'false', 'true');
            }
        });

        $("#buttonAcceptAllAdmin").click(function () {
            var ids = [];
            for (var i = 0; i <= buttonsAA.length; i++) {
                ids.push($(buttonsAA[i]).data('gid'));
            }
            if (confirm('Czy na pewno chcesz wszystkie zaakceptować?')) {
                all_akceptuj_wniosek(ids, 'true', 'false');
            }
        });

        $("#buttonDeclineAllAdmin").click(function () {
            var ids = [];
            for (var i = 0; i <= buttonsDA.length; i++) {
                ids.push($(buttonsDA[i]).data('gid'));
            }
            if (confirm('Czy na pewno chcesz wszystkie odrzucić?')) {
                all_akceptuj_wniosek(ids, 'true', 'false');
            }
        });

        $('#search').on('change', function () {

            var tr = $('#PlanUrlopuTable > tbody > tr'), td = null, txt = null;
            var search = $(this).val(), res = false;
            var not_number = isNaN(search);

            for (var i = 0; i < tr.length; i++) {
                if (not_number) {
                    td = $(tr[i]).find('.search-val');
                } else {
                    td = $(tr[i]).find('.search-nr');
                }
                res = false;
                for (var j = 0; j < td.length; j++) {
                    txt = $(td[j]).text().trim();
                    if (not_number) {
                        if (txt.toLowerCase().indexOf(search.toLowerCase()) >= 0) {
                            $(tr[i]).show();
                            res = true;
                        }
                    } else {
                        if (txt == search) {
                            $(tr[i]).show();
                            res = true;
                        }
                    }
                }
                if (search === '') {
                    $(tr[i]).show();
                } else if (!res) {
                    $(tr[i]).hide();
                }
            }
        });
    })
</script>

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