<div class="row" id="DodajUzytkownikaFormContainer">
    <div class="col-xs-12">
        <div class="box box-danger">
            <?php $inputTextOptions = [
                'type' => 'text',
                'class' => 'form-control',
                'div' => false,
                'label' => false,
                'autocomplete' => 'off',
            ];
            ?>
            <?php echo $this->Form->create(null, ['templates' => ['submitContainer' => '{{content}}',], 'id' => 'tem:pUzytkownik', 'name' => 'tem:pUzytkownik', 'class' => 'form-horizontal', 'autocomplete' => 'off']); ?>
            <div class="box-body">
                <?php echo $this->Form->input('dat:Gid', array('type' => 'hidden')); ?>
                <h2 style="margin-left: 0.5em;">Dane podstawowe</h2>
                <div class="form-group">
                    <label class="col-xs-12 col-md-2 control-label">Nazwa użytkownika</label>
                    <div class="col-xs-12 col-md-10">
                        <?php echo $this->Form->input( 'tem:pUzytkownik.dat:Nazwa', array('required' => 'required', 'class' => 'form-control', 'div' => false, 'label' => false)); ?>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-md-2 control-label">Imie</label>
                    <div class="col-xs-12 col-md-10">
                        <?php echo $this->Form->input( 'tem:pUzytkownik.dat:Imie', array('required' => 'required', 'class' => 'form-control', 'div' => false, 'label' => false)); ?>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-md-2 control-label">Nazwisko</label>
                    <div class="col-xs-12 col-md-10">
                        <?php echo $this->Form->input( 'tem:pUzytkownik.dat:Nazwisko', array('required' => 'required', 'class' => 'form-control', 'div' => false, 'label' => false)); ?>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-md-2 control-label">Opis</label>
                    <div class="col-xs-12 col-md-10">
                        <?php echo $this->Form->input('tem:pUzytkownik.dat:Opis', array('class' => 'form-control', 'div' => false, 'label' => false)); ?>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-md-2 control-label">Email</label>
                    <div class="col-xs-12 col-md-10">
                        <?php echo $this->Form->input('tem:pUzytkownik.dat:Email', array('required' => 'required', 'type' => 'email', 'class' => 'form-control', 'div' => false, 'label' => false)); ?>
                    </div>
                </div>
                <div class="form-group password-1-container">
                    <label class="col-xs-12 col-md-2 control-label">Hasło</label>
                    <div class="col-xs-12 col-md-10">
                        <?php echo $this->Form->input('tem:pUzytkownik.dat:Haslo', array('required' => 'required', 'type' => 'password', 'id' => 'user-password', 'class' => 'form-control', 'div' => false, 'label' => false, 'required' => true)); ?>
                    </div>
                </div>
                <div class="form-group password-2-container">
                    <label class="col-xs-12 col-md-2 control-label">Powtórz hasło</label>
                    <div class="col-xs-12 col-md-10 ">
                        <input type="password" name="data[password_repeat]" class="form-control password-repeat" data-compare-target="#user-password" required="required"/>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-xs-12 col-md-10 col-md-offset-2 col-xs-offset-0">
                        <?php echo $this->Form->checkbox('tem:pUzytkownik.dat:CzyMusiZmienicHaslo', array('id' => 'zmiana-hasla')); ?>
                    <label for="zmiana-hasla">Zmiana hasła przy następnym logowaniu</label>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-xs-12 col-md-10 col-md-offset-2 col-xs-offset-0">
                        <?php echo $this->Form->checkbox('tem:pUzytkownik.dat:CzyWylaczony', array('id' => 'czy-wylaczony')); ?>
                    <label for="czy-wylaczony">Wyłączony</label>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-xs-12 col-md-10 col-md-offset-2 col-xs-offset-0">
                        <?php echo $this->Form->checkbox('tem:pUzytkownik.dat:CzyDostepDoWszystkich', ['id' => 'czy-dostep-do-wszystkich']); ?>
                        <label for="czy-dostep-do-wszystkich">Dostęp do wszystkich</label>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-xs-12 col-md-10 col-md-offset-2 col-xs-offset-0">
                        <?php echo $this->Form->checkbox('tem:pUzytkownik.dat:CzyDostepDoDanychPlacowych', ['id' => 'czy-dostep-do-danych-placowych']); ?>
                        <label for="czy-dostep-do-wszystkich">Dostęp do danych płacowych</label>
                    </div>
                </div>
                <div style="display: none; visibility: hidden;">
                    <div class="form-group">
                        <div class="col-xs-12 col-md-10 col-md-offset-2 col-xs-offset-0">
                            <?php echo $this->Form->checkbox('tem:pUzytkownik.dat:CzyAdministrator', array('id' => 'czy-admin')); ?>
                            <label for="czy-admin">Administrator</label>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-xs-12 col-md-10 col-md-offset-2 col-xs-offset-0">
                            <?php echo $this->Form->checkbox('tem:pUzytkownik.dat:CzyWbudowana', array('id' => 'czy-wbudowana')); ?>
                            <label for="czy-wbudowana">Użytkownik wbudowany</label>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-xs-12 col-md-10 col-md-offset-2 col-xs-offset-0">
                            <?php echo $this->Form->checkbox('tem:pUzytkownik.dat:CzyZaznaczenie', array('id' => 'czy-zaznaczenie')); ?>
                            <label for="czy-zaznaczenie">Zaznaczenie</label>
                        </div>
                    </div>
                </div>
                <h2 style="margin-left: 0.5em;">Użytkownik - pracownik</h2>
                <div class="form-group">
                    <label class="col-xs-12 col-md-2 control-label">Pracownik o numerze</label>
                    <div class="col-xs-12 col-md-10">
                        <div class="col-xs-12 input-group">
                            <?php echo $this->Form->input('tem:pUzytkownik.dat:PowiazanyNumerEwidencyjny', array_merge($inputTextOptions, ['id' => 'dat:PowiazanyNumerEwidencyjny'])); ?>
                            <a href="#" class="input-group-addon workers-modal-trigger" data-count="1" data-getall="yes">
                                <i class="fa fa-search"></i>
                            </a>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-md-2 control-label">Dodatkowo każdy pracownik o numerach</label>
                    <div class="col-xs-12 col-md-10">
                        <div class="col-xs-12 input-group">
                            <?php echo $this->Form->input('tem:pUzytkownik.dat:PowiazaneNumeryEwidencyjne', array_merge($inputTextOptions, ['id' => 'dat:PowiazaneNumeryEwidencyjne'])); ?>
                            <a href="#" class="input-group-addon workers-modal-trigger" data-getall="yes">
                                <i class="fa fa-search"></i>
                            </a>
                        </div>
                    </div>
                </div>
                <h2 style="margin-left: 0.5em;">Uprawnienia do innych pracowników</h2>
                <div class="form-group">
                    <label class="col-xs-12 col-md-2 control-label">Uprawniony</label>
                    <div class="col-xs-12 col-md-10">
                        <div class="col-xs-12 input-group">
                            <?php echo $this->Form->input('tem:pUzytkownik.dat:NumeryEwidencyjne', array_merge($inputTextOptions, ['id' => 'dat:NumeryEwidencyjne'])); ?>
                            <a href="#" class="input-group-addon workers-modal-trigger" data-getall="yes">
                                <i class="fa fa-search"></i>
                            </a>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-md-2 control-label">Wykluczony</label>
                    <div class="col-xs-12 col-md-10">
                        <div class="col-xs-12 input-group">
                            <?php echo $this->Form->input('tem:pUzytkownik.dat:NumeryEwidencyjneWykluczone', array_merge($inputTextOptions, ['id' => 'dat:NumeryEwidencyjneWykluczone'])); ?>
                            <a href="#" class="input-group-addon workers-modal-trigger" data-getall="yes">
                                <i class="fa fa-search"></i>
                            </a>
                        </div>
                    </div>
                </div>
				<h2 style="margin-left: 0.5em;">Grupy portalu pracownika</h2>
				<div class="form-group">
					<label class="col-xs-12 col-md-2 control-label">Grupa portalu pracownika I</label>
					<div class="col-xs-12 col-md-10">
						<div class="col-xs-12 input-group">
							<?php echo $this->Form->input('tem:pUzytkownik.dat:GrupaPracownikowPortalu0', array_merge($inputTextOptions, ['id' => 'dat:GrupaPracownikowPortalu0'])); ?>
							<a href="#" data-code-list-number="192" class="input-group-addon codes-modal-trigger">
								<i class="fa fa-search"></i>
							</a>
						</div>
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-md-2 control-label">Grupa portalu pracownika II</label>
					<div class="col-xs-12 col-md-10">
						<div class="col-xs-12 input-group">
							<?php echo $this->Form->input('tem:pUzytkownik.dat:GrupaPracownikowPortalu1', array_merge($inputTextOptions, ['id' => 'dat:GrupaPracownikowPortalu1'])); ?>
							<a href="#" data-code-list-number="193" class="input-group-addon codes-modal-trigger">
								<i class="fa fa-search"></i>
							</a>
						</div>
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-md-2 control-label">Grupa portalu pracownika III</label>
					<div class="col-xs-12 col-md-10">
						<div class="col-xs-12 input-group">
							<?php echo $this->Form->input('tem:pUzytkownik.dat:GrupaPracownikowPortalu2', array_merge($inputTextOptions, ['id' => 'dat:GrupaPracownikowPortalu2'])); ?>
							<a href="#" data-code-list-number="194" class="input-group-addon codes-modal-trigger">
								<i class="fa fa-search"></i>
							</a>
						</div>
					</div>
				</div>
                <h2 style="margin-left: 0.5em;">Uprawnienia według struktury organizacyjnej do</h2>
                <div class="form-group">
                    <label class="col-xs-12 col-md-2 control-label">Komórek</label>
                    <div class="col-xs-12 col-md-10">
                        <div class="col-xs-12 input-group">
                            <?php echo $this->Form->input('tem:pUzytkownik.dat:Komorki', array_merge($inputTextOptions, ['id' => 'dat:Komorki'])); ?>
                            <a href="#" data-code-list-number="2" class="input-group-addon codes-modal-trigger">
                                <i class="fa fa-search"></i>
                            </a>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-md-2 control-label">Stanowisk</label>
                    <div class="col-xs-12 col-md-10">
                        <div class="col-xs-12 input-group">
                            <?php echo $this->Form->input('tem:pUzytkownik.dat:Stanowiska', array_merge($inputTextOptions, ['id' => 'dat:Stanowiska'])); ?>
                            <a href="#" data-code-list-number="5" class="input-group-addon codes-modal-trigger">
                                <i class="fa fa-search"></i>
                            </a>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-md-2 control-label">Płatników</label>
                    <div class="col-xs-12 col-md-10">
                        <div class="col-xs-12 input-group">
                            <?php echo $this->Form->input('tem:pUzytkownik.dat:Platnicy', array_merge($inputTextOptions, ['id' => 'dat:Platnicy'])); ?>
                            <a href="#" data-code-list-number="52" class="input-group-addon codes-modal-trigger">
                                <i class="fa fa-search"></i>
                            </a>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-md-2 control-label">Pionów</label>
                    <div class="col-xs-12 col-md-10">
                        <div class="col-xs-12 input-group">
                            <?php echo $this->Form->input('tem:pUzytkownik.dat:Piony', array_merge($inputTextOptions, ['id' => 'dat:Piony'])); ?>
                            <a href="#" data-code-list-number="53" class="input-group-addon codes-modal-trigger">
                                <i class="fa fa-search"></i>
                            </a>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-md-2 control-label">Regionów</label>
                    <div class="col-xs-12 col-md-10">
                        <div class="col-xs-12 input-group">
                            <?php echo $this->Form->input('tem:pUzytkownik.dat:Regiony', array_merge($inputTextOptions, ['id' => 'dat:Regiony'])); ?>
                            <a href="#" data-code-list-number="54" class="input-group-addon codes-modal-trigger">
                                <i class="fa fa-search"></i>
                            </a>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-md-2 control-label">Miejsc</label>
                    <div class="col-xs-12 col-md-10">
                        <div class="col-xs-12 input-group">
                            <?php echo $this->Form->input('tem:pUzytkownik.dat:MiejscaWykonywaniaPracy', array_merge($inputTextOptions, ['id' => 'dat:MiejscaWykonywaniaPracy'])); ?>
                            <a href="#" data-code-list-number="125" class="input-group-addon codes-modal-trigger">
                                <i class="fa fa-search"></i>
                            </a>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-md-2 control-label">Charakterów pracy</label>
                    <div class="col-xs-12 col-md-10">
                        <div class="col-xs-12 input-group">
                            <?php echo $this->Form->input('tem:pUzytkownik.dat:CharakteryPracy', array_merge($inputTextOptions, ['id' => 'dat:CharakteryPracy'])); ?>
                            <a href="#" data-code-list-number="24" class="input-group-addon codes-modal-trigger">
                                <i class="fa fa-search"></i>
                            </a>
                        </div>
                    </div>
                </div>
				<div class="form-group">
					<label class="col-xs-12 col-md-2 control-label">Dodatkowy opis</label>
					<div class="col-xs-12 col-md-10">
						<div class="col-xs-12 input-group">
							<?php echo $this->Form->input('tem:pUzytkownik.dat:DodatkoweOpisy', array_merge($inputTextOptions, ['id' => 'dat:DodatkoweOpisy'])); ?>
							<a href="#" data-code-list-number="83" class="input-group-addon codes-modal-trigger">
								<i class="fa fa-search"></i>
							</a>
						</div>
					</div>
				</div>
                <div class="form-group">
                    <div class="col-xs-12 col-md-2 control-label"></div>
                    <div class="col-xs-12 col-md-10">
                        <a href="administracja_uzytkownicy" data-menu-action="administracja_uzytkownicy" class="btn btn-default pull-left mr5">Anuluj</a>
                        <?php echo $this->Form->submit('Zapisz', array('class' => 'btn btn-success')); ?>
                    </div>
                </div>
            </div>
            <?php echo $this->Form->end(); ?>
        </div>
    </div>
</div>

<div class="modal fade" id="code-list-modal">
    <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">Wybierz kody</h4>
            </div>
            <div class="modal-body" id="code-list-body">
                <div class="row">
                    <div class="loader-holder" id="code-list-loader">
                        <div class="loader"></div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Zamknij</button>
                <button type="button" class="btn btn-success" id="code-list-submit">Zapisz</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<div class="modal fade" id="workers-list-modal">
    <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">Wybierz pracowników</h4>
            </div>
            <div class="modal-body" id="workers-list-body">
                <div class="row">
                    <div class="loader-holder" id="workers-list-loader">
                        <div class="loader"></div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Zamknij</button>
                <button type="button" class="btn btn-success" id="workers-list-submit">Zapisz</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script type="text/javascript" src="<?php echo $local_url; ?>js/paths.js"></script>

<script>
    jQuery.validator.addMethod("passwordequal", function (value, element) {
        var pass1 = $('.password-1-container input[type="password"]').val();
        var pass2 = $('.password-2-container input[type="password"]').val();
        if(pass1.length > 0 && pass2.length > 0) {
            if(pass1 != pass2) {
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }, "Hasła nie są identyczne.");

    $(document).ready(function () {
        $('#DodajUzytkownikaFormContainer form').validate({
            'data[password_repeat]' : {
                passwordequal: true
            },
            submitHandler: function (form) {
                var formData = $(form).serializeArray();
                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('/administracja_uzytkownicy');
                    }
                });
            }
        });
    });
</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'); ?>
