<div class="row">
    <div class="col-xs-12">
        <div class="box box-danger">
            <div class="text-right">
                <form class="form-horizontal">
                    <div style="margin: 10px 10px 0px 0px;">
                        <label>Szukaj:</label>
                        <input type="search" class="form-control input-sm" placeholder="" id="RoleModuleFilter">
                    </div>
                </form>
            </div>
            <?php echo $this->Form->create(null, ['templates' => ['submitContainer' => '{{content}}',], 'id' => 'tem:pZadanie', 'name' => 'tem:pZadanie', 'class' => 'form-horizontal']); ?>
            <?php echo $this->Form->input('dat:GidRoli', array('type' => 'hidden', 'value' => $pZadanie['dat:GidRoli'])); ?>
            <div class="box-body table-responsive">
                <table class="table table-hover" id="UprawnieniaRoliTable">
                    <thead>
                    <th>Nazwa uprawnienia</th>
                    <th class="text-right">Moje dane</th>
                    <th class="text-right">Dane podległych</th>
                    </thead>
                    <tbody>
                    <tr>
                        <td></td>
                        <td style="text-align: right;" class="blue-text">
                            <?php echo $this->Form->checkbox('col1CheckAll', array('id' => 'col1CheckAll')); ?>
                            <label for="col1CheckAll"><i class="fa fa-check"></i></label>
                        </td>
                        <td style="text-align: right;" class="blue-text">
                            <?php echo $this->Form->checkbox('col2CheckAll', array('id' => 'col2CheckAll')); ?>
                            <label for="col2CheckAll"><i class="fa fa-check"></i></label>
                        </td>
                    </tr>
                    <?php foreach ($pZadanie['dat:Moduly']['dat:Modul'] as $key => $module): ?>
                        <tr data-czy-niedostepny="<?php echo $module['dat:CzyNiedostepny']; ?>" <?php if ($module['dat:CzyNiedostepny'] == "true"): ?> style="display: none;" <?php endif; ?>>
                            <td>
                                <?php echo $module['dat:SciezkaNazw']; ?>
                                <br/>
                                <b><?php echo $module['dat:Opis']; ?></b>
                            </td>
                            <td style="text-align: right;">
                                <div class="make-relative-8">
                                    <?php //Uprawnienia; ?>
                                    <?php //URUCHAMIANIE; ?>
                                    <?php if ($module['dat:CzyUruchamianieDotyczy'] !== "false"): ?>
                                        <?php $checked = false; ?>
                                        <?php if ((string)$module['dat:CzyUruchamianie'] == 'true'): ?>
                                            <?php $checked = true; ?>
                                        <?php endif; ?>
                                        <?php echo $this->Form->checkbox('dat:Moduly.dat:Modul.' . $key . '.dat:CzyUruchamianie', array('id' => 'uruchamianie-' . $key, 'checked' => $checked)); ?>
                                        <label for="uruchamianie-<?php echo $key; ?>"><i
                                                class="fa fa-power-off"></i></label>
                                    <?php else: ?>
                                        <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyUruchamianie', array('type' => 'hidden', 'value' => $module['dat:CzyUruchamianie'])); ?>
                                    <?php endif; ?>
                                    <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyUruchamianieDotyczy', array('type' => 'hidden', 'value' => $module['dat:CzyUruchamianieDotyczy'])); ?>
                                    <?php //DODAWANIE; ?>
                                    <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyDodawanieDotyczy', array('type' => 'hidden', 'value' => $module['dat:CzyDodawanieDotyczy'])); ?>
                                    <?php if ($module['dat:CzyDodawanieDotyczy'] !== "false"): ?>
                                        <?php $checked = false; ?>
                                        <?php if ((string)$module['dat:CzyDodawanie'] == 'true'): ?>
                                            <?php $checked = true; ?>
                                        <?php endif; ?>
                                        <?php echo $this->Form->checkbox('dat:Moduly.dat:Modul.' . $key . '.dat:CzyDodawanie', array('id' => 'dodawanie-' . $key, 'checked' => $checked)); ?>
                                        <label for="dodawanie-<?php echo $key; ?>"><i class="fa fa-plus"></i></label>
                                    <?php else: ?>
                                        <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyDodawanie', array('type' => 'hidden', 'value' => $module['dat:CzyDodawanie'])); ?>
                                    <?php endif; ?>
                                    <?php //EDYCJA; ?>
                                    <?php if ($module['dat:CzyEdycjaDotyczy'] !== "false"): ?>
                                        <?php $checked = false; ?>
                                        <?php if ((string)$module['dat:CzyEdycja'] == 'true'): ?>
                                            <?php $checked = true; ?>
                                        <?php endif; ?>
                                        <?php echo $this->Form->checkbox('dat:Moduly.dat:Modul.' . $key . '.dat:CzyEdycja', array('id' => 'edycja-' . $key, 'checked' => $checked)); ?>
                                        <label for="edycja-<?php echo $key; ?>"><i class="fa fa-pencil"></i></label>
                                    <?php else: ?>
                                        <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyEdycja', array('type' => 'hidden', 'value' => $module['dat:CzyEdycja'])); ?>
                                    <?php endif; ?>
                                    <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyEdycjaDotyczy', array('type' => 'hidden', 'value' => $module['dat:CzyEdycjaDotyczy'])); ?>
                                    <?php //USUWANIE; ?>
                                    <?php if ($module['dat:CzyUsuwanieDotyczy'] !== "false"): ?>
                                        <?php $checked = false; ?>
                                        <?php if ((string)$module['dat:CzyUsuwanie'] == 'true'): ?>
                                            <?php $checked = true; ?>
                                        <?php endif; ?>
                                        <?php echo $this->Form->checkbox('dat:Moduly.dat:Modul.' . $key . '.dat:CzyUsuwanie', array('id' => 'usuwanie-' . $key, 'checked' => $checked)); ?>
                                        <label for="usuwanie-<?php echo $key; ?>"><i class="fa fa-minus"></i></label>
                                    <?php else: ?>
                                        <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyUsuwanie', array('type' => 'hidden', 'value' => $module['dat:CzyUsuwanie'])); ?>
                                    <?php endif; ?>
                                    <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyUsuwanieDotyczy', array('type' => 'hidden', 'value' => $module['dat:CzyUsuwanieDotyczy'])); ?>
                                    <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyNiedostepny', array('type' => 'hidden', 'value' => $module['dat:CzyNiedostepny'])); ?>
                                </div>
                            </td>
                            <td style="text-align: right;">
                                <div class="make-relative-8">
                                    <?php //GRUPA; ?>
                                    <?php //URUCHAMIANIE; ?>
                                    <?php if ($module['dat:CzyUruchamianieGrupaDotyczy'] !== "false"): ?>
                                        <?php $checked = false; ?>
                                        <?php if ((string)$module['dat:CzyUruchamianieGrupa'] == 'true'): ?>
                                            <?php $checked = true; ?>
                                        <?php endif; ?>
                                        <?php echo $this->Form->checkbox('dat:Moduly.dat:Modul.' . $key . '.dat:CzyUruchamianieGrupa', array('id' => 'uruchamianie-grupa-' . $key, 'checked' => $checked)); ?>
                                        <label for="uruchamianie-grupa-<?php echo $key; ?>"><i
                                                class="fa fa-power-off"></i></label>
                                    <?php else: ?>
                                        <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyUruchamianieGrupa', array('type' => 'hidden', 'value' => $module['dat:CzyUruchamianieGrupa'])); ?>
                                    <?php endif; ?>
                                    <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyUruchamianieGrupaDotyczy', array('type' => 'hidden', 'value' => $module['dat:CzyUruchamianieGrupaDotyczy'])); ?>
                                    <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyUruchamianieZastepstwo', array('type' => 'hidden', 'value' => $module['dat:CzyUruchamianieZastepstwo'])); ?>

                                    <?php //DODAWANIE; ?>
                                    <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyDodawanieGrupaDotyczy', array('type' => 'hidden', 'value' => $module['dat:CzyDodawanieGrupaDotyczy'])); ?>
                                    <?php if ($module['dat:CzyDodawanieGrupaDotyczy'] !== "false"): ?>
                                        <?php $checked = false; ?>
                                        <?php if ((string)$module['dat:CzyDodawanieGrupa'] == 'true'): ?>
                                            <?php $checked = true; ?>
                                        <?php endif; ?>
                                        <?php echo $this->Form->checkbox('dat:Moduly.dat:Modul.' . $key . '.dat:CzyDodawanieGrupa', array('id' => 'dodawanie-grupa-' . $key, 'checked' => $checked)); ?>
                                        <label for="dodawanie-grupa-<?php echo $key; ?>"><i
                                                class="fa fa-plus"></i></label>
                                    <?php else: ?>
                                        <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyDodawanieGrupa', array('type' => 'hidden', 'value' => $module['dat:CzyDodawanieGrupa'])); ?>
                                    <?php endif; ?>
                                    <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyDodawanieZastepstwo', array('type' => 'hidden', 'value' => $module['dat:CzyDodawanieZastepstwo'])); ?>

                                    <?php //EDYCJA; ?>
                                    <?php if ($module['dat:CzyEdycjaGrupaDotyczy'] !== "false"): ?>
                                        <?php $checked = false; ?>
                                        <?php if ((string)$module['dat:CzyEdycjaGrupa'] == 'true'): ?>
                                            <?php $checked = true; ?>
                                        <?php endif; ?>
                                        <?php echo $this->Form->checkbox('dat:Moduly.dat:Modul.' . $key . '.dat:CzyEdycjaGrupa', array('id' => 'edycja-grupa-' . $key, 'checked' => $checked)); ?>
                                        <label for="edycja-grupa-<?php echo $key; ?>"><i
                                                class="fa fa-pencil"></i></label>
                                    <?php else: ?>
                                        <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyEdycjaGrupa', array('type' => 'hidden', 'value' => $module['dat:CzyEdycjaGrupa'])); ?>
                                    <?php endif; ?>
                                    <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyEdycjaGrupaDotyczy', array('type' => 'hidden', 'value' => $module['dat:CzyEdycjaGrupaDotyczy'])); ?>
                                    <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyEdycjaZastepstwo', array('type' => 'hidden', 'value' => $module['dat:CzyEdycjaZastepstwo'])); ?>

                                    <?php //USUWANIE; ?>
                                    <?php if ($module['dat:CzyUsuwanieGrupaDotyczy'] !== "false"): ?>
                                        <?php $checked = false; ?>
                                        <?php if ((string)$module['dat:CzyUsuwanieGrupa'] == 'true'): ?>
                                            <?php $checked = true; ?>
                                        <?php endif; ?>
                                        <?php echo $this->Form->checkbox('dat:Moduly.dat:Modul.' . $key . '.dat:CzyUsuwanieGrupa', array('id' => 'usuwanie-grupa-' . $key, 'checked' => $checked)); ?>
                                        <label for="usuwanie-grupa-<?php echo $key; ?>"><i
                                                class="fa fa-minus"></i></label>
                                    <?php else: ?>
                                        <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyUsuwanieGrupa', array('type' => 'hidden', 'value' => $module['dat:CzyUsuwanieGrupa'])); ?>
                                    <?php endif; ?>
                                    <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyUsuwanieGrupaDotyczy', array('type' => 'hidden', 'value' => $module['dat:CzyUsuwanieGrupaDotyczy'])); ?>
                                    <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyUsuwanieZastepstwo', array('type' => 'hidden', 'value' => $module['dat:CzyUsuwanieZastepstwo'])); ?>

                                    <?php //CzyModulAdministracyjny; ?>
                                    <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:CzyModulAdministracyjny', array('type' => 'hidden', 'value' => $module['dat:CzyModulAdministracyjny'])); ?>

                                    <?php //POZOSTAŁE; ?>
                                    <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:Gid', array('type' => 'hidden', 'value' => $module['dat:Gid'])); ?>
                                    <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:MozliweUprawnienia', array('type' => 'hidden', 'value' => $module['dat:MozliweUprawnienia'])); ?>
                                    <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:Nazwa', array('type' => 'hidden', 'value' => $module['dat:Nazwa'])); ?>
                                    <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:Opis', array('type' => 'hidden', 'value' => $module['dat:Opis'])); ?>
                                    <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:SciezkaGid', array('type' => 'hidden', 'class' => 'input-sciezka-gid', 'value' => $module['dat:SciezkaGid'])); ?>
                                    <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:SciezkaNazw', array('type' => 'hidden', 'value' => $module['dat:SciezkaNazw'])); ?>
                                    <?php echo $this->Form->input('dat:Moduly.dat:Modul.' . $key . '.dat:Wciecie', array('type' => 'hidden', 'value' => $module['dat:Wciecie'])); ?>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>

                <div class="form-group mt20">
                    <label class="col-xs-12 col-md-2 control-label"></label>
                    <div class="col-xs-12 col-md-10">
                        <div class="col-xs-12 input-group">
                            <a data-menu-action="administracja_role" class="btn btn-default pull-left margin-r-5"
                               href="administracja_role">Anuluj</a>
                            <?php echo $this->Form->submit('Zapisz', array('id' => 'submitForm-UprawnieniaRoli', 'class' => 'btn btn-success pull-left')); ?>
                        </div>
                    </div>
                </div>
            </div>
            <?php echo $this->Form->end(); ?>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('#col1CheckAll, #col2CheckAll').click(function () {
            var checkboxChecked = $(this).is(':checked');
            var cellIndex = $(this).parents('td').index();
            var row = $(this).parents('tr');
            var rowIndex = row.index();
            var sciezkaGid = row.find('.input-sciezka-gid').val();

            console.log(cellIndex, checkboxChecked);

            $(this).parents('table').find('tr td:nth-of-type(' + (cellIndex + 1) + ')').each(function () {
                if (checkboxChecked) {
                    $(this).find('[type="checkbox"]:visible').prop('checked', true);
                } else {
                    $(this).find('[type="checkbox"]:visible').prop('checked', false);
                }
            });
        });

        jQuery.expr[":"].Contains = jQuery.expr.createPseudo(function (arg) {
            return function (elem) {
                return jQuery(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
            };
        });

        $('#RoleModuleFilter').keyup(function () {
            var f = $(this).val();
            $('#UprawnieniaRoliTable tr').hide();
            $('#UprawnieniaRoliTable tr:first-of-type').show();
            $('#UprawnieniaRoliTable tr:Contains(' + f + ')').not('[data-czy-niedostepny="true"]').show();
        });
//    $(document).ready(function () {
//        $('td .iCheck-helper').on('click', function () {
//            var parentCheckbox = $(this).parents('.icheck');
//            var checkboxChecked = parentCheckbox.find('[type="checkbox"]').is(':checked');
//            var cellIndex = $(this).parents('td').index();
//            var row = $(this).parents('tr');
//            var rowIndex = row.index();
//            var sciezkaGid = row.find('.input-sciezka-gid').val();
//
//            $(this).parents('table').find('tr').each(function () {
//                var loopRow = $(this);
//                if(loopRow.index() > rowIndex) {
//                    var loopSciezkaGid = loopRow.find('.input-sciezka-gid').val();
//                    console.log(loopRow.index(), loopSciezkaGid);
//                }
//            });
//        });
    });
</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'); ?>
