<?php use App\Controller\Wnioski;
use Cake\Core\App;

App::className('Wnioski', 'Model') ?>
<?php if (!empty($wnioski['bWnioski']['bWniosek'])): ?>
    <table class="table hover-rows custom-table" data-start-sort="1" data-start-sort-direction="desc" data-start-height="60vh">
        <thead>
        <tr>
            <th class="text-center"></th>
			<th><span title="Nr wniosku">Nr</span></th>
            <th>Data Od</th>
            <th>Data Do</th>
            <?php if('true' == ($wnioski['bCzyDniNaWidokuListyWnioskow'] ?? 'false')): ?>
            <th>Dni</th>
            <?php endif; ?>
			<th>Godziny</th>
			<th>Godzina Od-Do</th>
            <th>Wniosek</th>
            <th>Status</th>
            <th>Ak.Zast</th>
            <?php if(isset($typ_wniosku) && $typ_wniosku == 'Wplywajace') : ?>
                <th>Wnioskujący</th>
            <?php endif; ?>
            <th>Data utworzenia</th>
            <th class="text-right no-sortable"></th>
        </tr>
        </thead>
        <tbody>
        <?php foreach ($wnioski['bWnioski']['bWniosek'] as $k => $wniosek): ?>
            <tr data-status="">
                <td class="text-center">
                    <?php if (isset($wniosek['bZalaczniki']) && !empty($wniosek['bZalaczniki'])): ?>
                        <i class="fa fa-paperclip" style="margin-top: 3px; font-size: 18px; color: <?php echo $wniosek['bKolorWypelnieniaObrazkaHTML'] ?? '#333'; ?>"></i>
                    <?php endif; ?>
                </td>
				<td><span title="Nr wniosku"><?php echo $wniosek['bNumer']; ?></span></td>
                <td><span class="span-date"><?php echo date('Y-m-d', strtotime($wniosek['bDataOd'])); ?></span></td>
                <td><span class="span-date"><?php echo date('Y-m-d', strtotime($wniosek['bDataDo'])); ?></span></td>
                <?php if('true' == ($wnioski['bCzyDniNaWidokuListyWnioskow'] ?? 'false')): ?>
                <td><?php echo $wniosek['bDni']; ?></td>
                <?php endif; ?>
				<td><?php echo $wniosek['bGodziny']; ?></td>
				<td><?php echo (!empty($wniosek['bGodzinaOd']) && !empty($wniosek['bGodzinaDo']) && $wniosek['bGodzinaOd'] != '00:00' && $wniosek['bGodzinaDo'] != '00:00') ? $wniosek['bGodzinaOd'] . '-' . $wniosek['bGodzinaDo'] : ''; ?></td>
                <td><?php echo $wniosek['bKod']['cOpis']; ?></td>
                <td>
                    <?php
                    $color = 'red';
                    switch ($wniosek['bStatus']) {
                        case 'Zaakceptowany':
                            $color = 'green';
                            break;
                        case 'Odrzucony':
                            $color = 'red';
                            break;
                        case 'Oczekuje':
                            $color = 'blue';
                            break;
						case 'Zaimportowany':
							$color = 'yellow';
							break;
                    }
                    ?>
                    <span rel="tooltip" data-placement="bottom" title="<?= ($wniosek['bAkceptacjeOpis'] ?: ''); ?>" class="badge bg-<?php echo $color; ?>"><?php echo $wniosek['bStatus']; ?></span>
                </td>
                <td>
                    <?php
                        $colorZastepcy = '';
                        $opisZastepcy = '';
                        $existsZastepca = Wnioski::hasZastepca($wniosek['bZastepca']);
                        if (Wnioski::hasStatusZastepcyAccept($wniosek['bStatusZastepcy']) && $existsZastepca) {
                            $colorZastepcy = 'green';
                            $opisZastepcy = 'Tak';
                        } else if (Wnioski::hasStatusZastepcyNotAccept($wniosek['bStatusZastepcy']) && $existsZastepca) {
                            $colorZastepcy = 'red';
                            $opisZastepcy = 'Nie';
                        } else if (Wnioski::hasStatusZastepcyPending($wniosek['bStatusZastepcy']) && $existsZastepca) {
                            $colorZastepcy = 'blue';
                            $opisZastepcy = 'Czeka';
                        }
                    ?>
                    <span class="badge bg-<?php echo $colorZastepcy; ?>"><?php echo $opisZastepcy; ?></span>
                </td>
                <?php if(isset($typ_wniosku) && $typ_wniosku == 'Wplywajace') : ?>
                    <td>
                        <?php echo $wniosek['bWnioskujacy']['cNazwaWyswietlana']; ?>
                    </td>
                <?php endif; ?>
                <td><span class="span-date"><?php echo $wniosek['bDataUtworzenia']; ?></span></td>
                <td class="text-right table-actions">
                    <?php if(isset($typ_wniosku) && $typ_wniosku == 'Wplywajace') : ?>
                        <?php if(empty($wniosek['bAkceptacje']) && empty($wniosek['bOdrzucenia'])) : ?>

                        <?php endif; ?>
                    <?php endif; ?>

					<?php if($wniosek['bCzyMozePrzekazac'] == 'true') : ?>
						<a data-menu-action="przekaz_wniosek/<?php echo $wniosek['bGid']; ?>" rel="tooltip" data-placement="left" title="Przekaż wniosek" class="btn btn-info" href="przekaz_wniosek/<?php echo $wniosek['bGid']; ?>">
							<i class="fa fa-share"></i>
						</a>
					<?php endif; ?>

                    <?php if($wniosek['bCzyAktualnyUserMozeZaakceptowac'] == 'true') : ?>
                        <a rel="tooltip" data-placement="left" title="Akceptuj wniosek" data-method="make-action" class="btn btn-warning" data-url="akceptuj_wniosek/<?php echo $wniosek['bGid']; ?>">
                            <i class="fa fa-thumbs-up green-text"></i>
                        </a>
                    <?php endif; ?>

                    <?php if($wniosek['bCzyAktualnyUserMozeOdrzucic'] == 'true') : ?>
                        <a rel="tooltip" data-placement="left" title="Odrzuć wniosek" data-method="make-action" class="btn btn-warning" data-url="odrzuc_wniosek/<?php echo $wniosek['bGid']; ?>">
                            <i class="fa fa-thumbs-down red-text"></i>
                        </a>
                    <?php endif; ?>

                    <?php if($wniosek['bCzyAktualnyUserMozeDrukowac'] == 'true') : ?>
                        <a target="_blank" rel="tooltip" data-placement="left" title="Drukuj wniosek" class="btn btn-warning" href="drukuj_wniosek/<?php echo $wniosek['bGid']; ?>">
                            <i class="fa fa-print"></i>
                        </a>
                    <?php endif; ?>

                    <?php if($wniosek['bCzyAktualnyUserMozePrzegladac'] == 'true') : ?>
                        <a rel="tooltip" data-placement="left" title="Pokaż wniosek" class="btn btn-success" data-ajax-action-wniosek="pokaz_wniosek/<?php echo $wniosek['bGid']; ?>">
                            <i class="fa fa-eye"></i>
                        </a>
                    <?php endif; ?>

                    <?php if($wniosek['bCzyAktualnyUserMozeEdytowac'] == 'true') : ?>
                        <a data-menu-action="edytuj_wniosek/<?php echo $wniosek['bGid']; ?>" rel="tooltip" data-placement="left" title="Edytuj wniosek" class="btn btn-primary" href="/edytuj_wniosek/<?php echo $wniosek['bGid']; ?>">
                            <i class="fa fa-pencil"></i>
                        </a>
                    <?php endif; ?>

                    <?php if($wniosek['bCzyAktualnyUserMozeUsunac'] == 'true') : ?>
                        <a rel="tooltip" data-placement="left" title="Usuń wniosek" class="btn btn-danger" data-entry="remove" data-gid="<?php echo $wniosek['bGid']; ?>">
                            <i class="fa fa-remove"></i>
                        </a>
                    <?php endif; ?>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
<?php else: ?>
    <div class="no-data"><p>Brak danych</p></div>
<?php endif; ?>

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

<script>
	$(document).ready(function () {
        $('[data-ajax-action-wniosek]').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-wniosek');
            $.ajax({
                url: local_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-wniosek-info').modal('show');
                $('#modal-wniosek-info .modal-body').html(resp);
            });
        });
    });

    $(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>

<div class="modal fade" id="modal-wniosek-info">
    <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">Pokaż wniosek</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 -->
