<div class="row" id="DodajZastepstwo">
    <div class="col-xs-12">
        <div class="box box-danger" style="max-height: 88vh; overflow-y: auto;">
            <?php echo $this->Form->create(null, ['templates' => ['submitContainer' => '{{content}}',], 'id' => 'tem:pZastepstwo', 'name' => 'tem:pZastepstwo', 'class' => 'form-horizontal']); ?>
            <div class="box-body relative-body">
                <div class="col-xs-12">
                    <div class="box-header-container">
                        <h2 style="margin-left: 0.5em;">Dane podstawowe</h2>
                        <div class="row mb20">
                            <div class="col-xs-6">
                                <label class="col-xs-12 col-md-2 control-label text-left">Data od</label>
                                <div class="col-xs-12 col-md-3">
                                    <?php echo $this->Form->input('tem:pZastepstwo.dat:DataOd', array('required' => 'required', 'class' => 'form-control datepicker-from', 'div' => false, 'label' => false, 'autocomplete' => 'off')); ?>
                                </div>
                            </div>
                        </div>
                        <div class="row mb20">
                            <div class="col-xs-6">
                                <label class="col-xs-12 col-md-2 control-label text-left">Data do</label>
                                <div class="col-xs-12 col-md-3">
                                    <?php echo $this->Form->input('tem:pZastepstwo.dat:DataDo', array('required' => 'required', 'class' => 'form-control datepicker-to', 'div' => false, 'label' => false, 'autocomplete' => 'off')); ?>
                                </div>
                            </div>
                        </div>
                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Gid', array('type' => 'hidden', 'id' => 'gid-zastepstwa')); ?>
                        <?php echo $this->Form->input('tem:pZastepstwo.dat:GidZastepowanego', array('type' => 'hidden', 'class' => 'gid-zastepowanego')); ?>
                        <?php echo $this->Form->input('tem:pZastepstwo.dat:GidZastepujacego', array('type' => 'hidden', 'class' => 'gid-zastepujacego')); ?>

						<?php echo $this->Form->input('tem:pZastepstwo.dat:Zastepowany.dat2:CzyMozeZaakceptowacZaPozostalych', array('type' => 'hidden', 'id' => 'akceptuje-za-pozostalych-zastepowanego')); ?>
						<?php echo $this->Form->input('tem:pZastepstwo.dat:Zastepowany.dat2:CzyMusiZaakceptowac', array('type' => 'hidden', 'id' => 'musi-zaakceptowac-zastepowanego')); ?>
						<?php echo $this->Form->input('tem:pZastepstwo.dat:Zastepowany.dat2:Data', array('type' => 'hidden', 'id' => 'data-zastepowanego')); ?>
						<?php echo $this->Form->input('tem:pZastepstwo.dat:Zastepowany.dat2:DataAkceptacjiOdrzucenia', array('type' => 'hidden', 'id' => 'data-akceptacji-odrzucenia-zastepowanego')); ?>
						<?php echo $this->Form->input('tem:pZastepstwo.dat:Zastepowany.dat2:DataPowiadomienia', array('type' => 'hidden', 'id' => 'data-powiadomienia-zastepowanego')); ?>
						<?php echo $this->Form->input('tem:pZastepstwo.dat:Zastepowany.dat2:Email', array('type' => 'hidden', 'id' => 'email-zastepowanego')); ?>
						<?php echo $this->Form->input('tem:pZastepstwo.dat:Zastepowany.dat2:Gid', array('type' => 'hidden', 'class' => 'gid-zastepowanego')); ?>
						<?php if(isset($czyMozeWybracZastepujacego) && $czyMozeWybracZastepujacego == 'true'): ?>
						<div class="row mb20">
							<div class="col-xs-6">
								<label class="col-xs-12 col-md-2 control-label text-left">Zastępowany</label>
								<div class="col-xs-12 col-md-10">
									<div id="receiver-search-holder">
										<div class="input-group custom-error-holder">
											<div class="input-group-addon receivers-list-trigger" id="receivers-list-trigger" data-holder-gid=".gid-zastepowanego" data-holder-name="#replacement-search1" data-zmetody="zastepowanego">
												<i class="fa fa-list"></i>
											</div>
											<?php echo $this->Form->input('tem:pZastepstwo.dat:Zastepowany.dat2:Gid', array('type' => 'hidden', 'class' => 'gid-zastepowanego')); ?>
											<?php echo $this->Form->input('tem:pZastepstwo.dat:Zastepowany.dat2:Nazwa', array('value' => $this->request->getData()['tem:pZastepstwo']['dat:Zastepowany']['dat2:NazwaWyswietlana'], 'data-loader' => '#replacement-search-loader', 'id' => 'replacement-search1', 'placeholder' => 'Szukaj...', 'class' => 'form-control', 'div' => false, 'label' => false)); ?>
										</div>
									</div>
								</div>
								<div class="loader-holder" id="replacement-search-loader" style="display: none; visibility: hidden;">
									<div class="loader"></div>
								</div>
							</div>
						</div>
						<?php endif; ?>
						<?php echo $this->Form->input('tem:pZastepstwo.dat:Zastepujacy.dat2:CzyMozeZaakceptowacZaPozostalych', array('type' => 'hidden', 'id' => 'akceptuje-za-pozostalych-zastepujacego')); ?>
						<?php echo $this->Form->input('tem:pZastepstwo.dat:Zastepujacy.dat2:CzyMusiZaakceptowac', array('type' => 'hidden', 'id' => 'musi-zaakceptowac-zastepujacego')); ?>
						<?php echo $this->Form->input('tem:pZastepstwo.dat:Zastepujacy.dat2:Data', array('type' => 'hidden', 'id' => 'data-zastepujacego')); ?>
						<?php echo $this->Form->input('tem:pZastepstwo.dat:Zastepujacy.dat2:DataAkceptacjiOdrzucenia', array('type' => 'hidden', 'id' => 'data-akceptacji-odrzucenia-zastepujacego')); ?>
						<?php echo $this->Form->input('tem:pZastepstwo.dat:Zastepujacy.dat2:DataPowiadomienia', array('type' => 'hidden', 'id' => 'data-powiadomienia-zastepujacego')); ?>
						<?php echo $this->Form->input('tem:pZastepstwo.dat:Zastepujacy.dat2:Email', array('type' => 'hidden', 'id' => 'email-zastepujacego')); ?>
						<?php echo $this->Form->input('tem:pZastepstwo.dat:Zastepujacy.dat2:Gid', array('type' => 'hidden', 'class' => 'gid-zastepujacego')); ?>
                        <div class="row mb20">
                            <div class="col-xs-6">
                                <label class="col-xs-12 col-md-2 control-label text-left">Zastępujący</label>
                                <div class="col-xs-12 col-md-10">
                                    <div id="receiver-search-holder">
                                        <div class="input-group">
                                            <div class="input-group-addon receivers-list-trigger" id="receivers-list-trigger" data-holder-gid=".gid-zastepujacego" data-holder-name="#replacement-search2" data-zmetody="zastepujacego">
                                                <i class="fa fa-list"></i>
                                            </div>
                                            <?php echo $this->Form->input('tem:pZastepstwo.dat:Zastepujacy.dat2:Gid', array('type' => 'hidden', 'class' => 'gid-zastepujacego')); ?>
                                            <?php echo $this->Form->input('tem:pZastepstwo.dat:Zastepujacy.dat2:Nazwa', array('value' => $this->request->getData('tem:pZastepstwo')['dat:Zastepujacy']['dat2:NazwaWyswietlana'], 'data-loader' => '#replacement-search-loader', 'id' => 'replacement-search2', 'placeholder' => 'Szukaj...', 'class' => 'form-control', 'div' => false, 'label' => false)); ?>
                                        </div>
                                    </div>
                                </div>
                                <div class="loader-holder" id="replacement-search-loader" style="display: none; visibility: hidden;">
                                    <div class="loader"></div>
                                </div>
                            </div>
                        </div>

                        <hr />

                        <div class="row">
                            <div class="col-xs-12">
                                <label class="col-xs-12 col-md-1 control-label text-left">Filtruj</label>
                                <div class="col-xs-12 col-md-11">
                                    <input type="text" id="modules-filter" placeholder="Filtruj..." class="form-control" style=""/>
                                </div>
                                <!--                            <div class="col-xs-3 col-md-3 text-right">-->
                                <!--                                -->
                                <!--                            </div>-->
                            </div>
                        </div>

                        <hr />
                    </div>

                    <div class="replacement-modules label-no-bold">

                        <div class="col-xs-12 form-group">

                            <div class="col-md-1" style="text-align: right;">

                            </div>
                            <label class="col-md-11">
                                <div class="inline-block width-60px make-relative">
                                    <input type="checkbox" id="check-all" data-all-checked="false"/>
                                </div>
                                <label for="check-all">
                                    <div class="inline-block blue-text" style="padding-top: 20px; position: relative; top: -10px;">
                                        Wszystkie
                                    </div>
                                </label>
                            </label>
                        </div>


                        <?php foreach ($this->request->getData('tem:pZastepstwo')['dat:Uprawnienia']['dat1:Modul'] as $key => $module): ?>
                            <div class="col-xs-12 form-group">

                                <div class="col-md-1" style="text-align: right;">

                                </div>
                                <label class="col-md-11">
                                    <div class="inline-block width-60px make-relative">
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyDodawanie', array('type' => 'hidden')); ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyDodawanieDotyczy', array('type' => 'hidden')); ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyDodawanieGrupa', array('type' => 'hidden')); ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyDodawanieGrupaDotyczy', array('type' => 'hidden')); ?>
                                        <?php if ($module['dat1:CzyDodawanieGrupaDotyczy'] !== "false"): ?>
                                            <?php $checked = false; ?>
                                            <?php if ((string)$module['dat1:CzyDodawanieZastepstwo'] == 'true'): ?>
                                                <?php $checked = true; ?>
                                                <script>
                                                    $(function () {
                                                        setTimeout(function() {
                                                            $('#dodawanie-<?php echo $key; ?>').prop('checked', <?php echo $checked; ?>);
                                                        });
                                                    });
                                                </script>
                                            <?php endif; ?>
                                            <?php echo $this->Form->checkbox('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyDodawanieZastepstwo', 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('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyDodawanieZastepstwo', array('type' => 'hidden')); ?>
                                        <?php endif; ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyEdycja', array('type' => 'hidden')); ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyEdycjaDotyczy', array('type' => 'hidden')); ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyEdycjaGrupa', array('type' => 'hidden')); ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyEdycjaGrupaDotyczy', array('type' => 'hidden')); ?>
                                        <?php if ($module['dat1:CzyEdycjaGrupaDotyczy'] !== "false"): ?>
                                            <?php $checked = false; ?>
                                            <?php if ((string)$module['dat1:CzyEdycjaZastepstwo'] == 'true'): ?>
                                                <?php $checked = true; ?>
                                                <script>
                                                    $(function () {
                                                        setTimeout(function() {
                                                            $('#edycja-<?php echo $key; ?>').prop('checked', <?php echo $checked; ?>);
                                                        });
                                                    });
                                                </script>
                                            <?php endif; ?>
                                            <?php echo $this->Form->checkbox('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyEdycjaZastepstwo', 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('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyEdycjaZastepstwo', array('type' => 'hidden')); ?>
                                        <?php endif; ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyModulAdministracyjny', array('type' => 'hidden')); ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyUruchamianie', array('type' => 'hidden')); ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyUruchamianieDotyczy', array('type' => 'hidden')); ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyUruchamianieGrupa', array('type' => 'hidden')); ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyUruchamianieGrupaDotyczy', array('type' => 'hidden')); ?>
                                        <?php if ($module['dat1:CzyUruchamianieGrupaDotyczy'] !== "false"): ?>
                                            <?php $checked = false; ?>
                                            <?php if ((string)$module['dat1:CzyUruchamianieZastepstwo'] == 'true'): ?>
                                                <?php $checked = true; ?>
                                                <script>
                                                    $(function () {
                                                        setTimeout(function() {
                                                            $('#uruchamianie-<?php echo $key; ?>').prop('checked', <?php echo $checked; ?>);
                                                        });
                                                    });
                                                </script>
                                            <?php endif; ?>
                                            <?php echo $this->Form->checkbox('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyUruchamianieZastepstwo', 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('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyUruchamianieZastepstwo', array('type' => 'hidden')); ?>
                                        <?php endif; ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyUsuwanie', array('type' => 'hidden')); ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyUsuwanieDotyczy', array('type' => 'hidden')); ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyUsuwanieGrupa', array('type' => 'hidden')); ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyUsuwanieGrupaDotyczy', array('type' => 'hidden')); ?>
                                        <?php if ($module['dat1:CzyUsuwanieGrupaDotyczy'] !== "false"): ?>
                                            <?php $checked = false; ?>
                                            <?php if ((string)$module['dat1:CzyUsuwanieZastepstwo'] == 'true'): ?>
                                                <?php $checked = true; ?>
                                                <script>
                                                    $(function () {
                                                        setTimeout(function() {
                                                            $('#usuwanie-<?php echo $key; ?>').prop('checked', <?php echo $checked; ?>);
                                                        });
                                                    });
                                                </script>
                                            <?php endif; ?>
                                            <?php echo $this->Form->checkbox('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyUsuwanieZastepstwo', 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('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:CzyUsuwanieZastepstwo', array('type' => 'hidden')); ?>
                                        <?php endif; ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:Gid', array('type' => 'hidden')); ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:MozliweUprawnienia', array('type' => 'hidden')); ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:Nazwa', array('type' => 'hidden')); ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:Opis', array('type' => 'hidden')); ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:SciezkaGid', array('type' => 'hidden')); ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:SciezkaNazw', array('type' => 'hidden')); ?>
                                        <?php echo $this->Form->input('tem:pZastepstwo.dat:Uprawnienia.dat1:Modul.' . $key . '.dat1:Wciecie', array('type' => 'hidden')); ?>
                                    </div>
                                    <div class="inline-block">
                                        <?php echo $module['dat1:SciezkaNazw']; ?>
                                        <br/>
                                        <b><?php echo $module['dat1:Opis']; ?></b>
                                    </div>
                                </label>
                            </div>
                        <?php endforeach; ?>
                    </div>

                </div>

                <div class="mt20 absolute-footer-zastepstwo">
                    <div class="col-xs-6">
                        <label class="col-xs-12 col-md-2 control-label text-left"></label>
                        <div class="col-xs-12 col-md-10">
                            <a href="/zastepstwa" data-menu-action="zastepstwa" class="pull-left btn btn-default btn-flat margin-r-5">Anuluj</a>
                            <?php echo $this->Form->submit('Zapisz', array('class' => 'btn btn-success btn-flat pull-left')); ?>
                        </div>
                    </div>
                </div>
            </div>
            <?php echo $this->Form->end(); ?>
        </div>
    </div>
</div>
<script type="text/javascript" src="<?php echo $local_url; ?>js/replacements.js"></script>

<script>
    $(function() {
        var checkAllCheckbox = $('#check-all');
        checkAllCheckbox.on('change', function () {
            if (checkAllCheckbox.is(":checked")) {
                $('.replacement-modules input[type="checkbox"]:visible').each(function () {
                    if ($(this).attr('id') != 'check-all') {
                        $(this).prop('checked', true).change();
                    }
                });
            } else {
                $('.replacement-modules input[type="checkbox"]:visible').each(function () {
                    if ($(this).attr('id') != 'check-all') {
                        $(this).prop('checked', false).change();
                    }
                });
            }
        });
    });
</script>

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

<script>
    jQuery.validator.addMethod("datefrom", function(value, element) {
        if($('[name="data[tem:pZastepstwo][dat:DataDo]"]').val() == undefined || $('[name="data[tem:pZastepstwo][dat:DataDo]"]').val().length == 0) {
            return true;
        } else {
            return  ($('[name="data[tem:pZastepstwo][dat:DataOd]"]').val() > $('[name="data[tem:pZastepstwo][dat:DataDo]"]').val()) ? false : true;
        }
    }, "'Data od' nie może być większa od 'Data do'");

	jQuery.validator.addMethod("selectpeople1", function(value, element) {
		var s_text = $('#replacement-search1').val();
		var s_gid = $('.gid-zastepowanego').val();
		if(s_text.length > 0) {
			if(s_gid.length > 0) {
				return true;
			} else {
				return false;
			}
		} else {
			return true;
		}
	}, "Proszę wybrać poprawną wartość w polu 'Zastępowany'");

	jQuery.validator.addMethod("selectpeople2", function(value, element) {
		var s_text = $('#replacement-search2').val();
		var s_gid = $('.gid-zastepujacego').val();
		if(s_text.length > 0) {
			if(s_gid.length > 0) {
				return true;
			} else {
				return false;
			}
		} else {
			return true;
		}
	}, "Proszę wybrać poprawną wartość w polu 'Zastępca'");

    $(document).ready(function () {
        datepickerFromToInit();

        $('#DodajZastepstwo form').validate({
			rules: {
				'data[tem:pZastepstwo][dat:DataOd]': {
					datefrom: true
				},
				'data[tem:pZastepstwo][dat:Zastepowany][dat2:Nazwa]': {
					selectpeople1: true
				},
				'data[tem:pZastepstwo][dat:Zastepujacy][dat2:Nazwa]': {
					selectpeople2: true
				}
			},
            submitHandler: function (form) {
                var formData = $(form).serializeArray();

                console.log(formData);

                var formAction = $(form).attr('action');

                $.ajax({
                    type: 'POST',
                    url: formAction,
                    data: formData
                }).done(function (response) {
                    response = JSON.parse(response);
                    console.log(response);
                    if(response['error'] != undefined) {
                        showTop();
                        showError(response['error']);
                    } else {
                        hideMessages();
                        showSuccess(response['message']);
                        loadAjaxView('/zastepstwa');
                    }
                });
            }
        });

        function resizeModules() {
            var mh = $(window).innerHeight() - $('.box-header-container').innerHeight() - 190;
            $('.replacement-modules').css('max-height', mh);
        }

        resizeModules();

        $(window).resize(function () {
            resizeModules();
        });
    });
</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'); ?>
