<?php

use Cake\Routing\Router;

if (!empty($passwordInfo)) {
    $passwordInfoData = explode("&#xD;\n", $passwordInfo);//str_replace("&#xD;\n","<br/>", $passwordInfo);
} else {
    $passwordInfoData = array();
}
echo $this->Form->create(null, ['templates' => ['submitContainer' => '{{content}}',], 'id' => 'UserZmianaHaslaForm', 'name' => 'User', 'novalidate' => 'novalidate']);
?>
<div class="form-group has-feedback custom-error-holder" xmlns="http://www.w3.org/1999/html">
    <?php echo $this->Form->input('username', array('id' => 'UserUsername', 'autocomplete' => 'new-password',  'required' => 'required', 'type' => 'text', 'class' => 'form-control', 'div' => false, 'label' => false, 'placeholder' => 'Nazwa użytkownika', 'value' => $this->request->getData('User')['username'])); ?>
    <span class="glyphicon glyphicon-user form-control-feedback"></span>
</div>
<div class="form-group has-feedback custom-error-holder">
    <?php echo $this->Form->input('password', array('id' => 'UserPassword', 'autocomplete' => 'new-password', 'required' => 'required', 'type' => 'password', 'class' => 'form-control', 'div' => false, 'label' => false, 'placeholder' => 'Hasło aktualne', 'value' => $this->request->getData('User')['password'])); ?>
    <span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="form-group has-feedback custom-error-holder">
    <?php echo $this->Form->input('new_password', array('id' => 'UserNewPassword', 'autocomplete' => 'new-password', 'required' => 'required', 'type' => 'password', 'class' => 'form-control', 'div' => false, 'label' => false, 'placeholder' => 'Nowe hasło')); ?>
    <span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="form-group has-feedback custom-error-holder">
    <?php echo $this->Form->input('re_new_password', array('id' => 'UserReNewPassword', 'autocomplete' => 'new-password', 'required' => 'required', 'type' => 'password', 'class' => 'form-control', 'div' => false, 'label' => false, 'placeholder' => 'Nowe hasło ponownie')); ?>
    <span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="row">
    <div class="col-xs-6">
        <?php
        $protocol = stripos($_SERVER['SERVER_PROTOCOL'], 'https') === true ? 'https://' : 'http://';
        //        $backLinkPath = $protocol . $_SERVER['SERVER_NAME'] .'/login';
        $backLinkPath = $local_url . 'login';
        if ($isSystemEvent) {
            $backLink = $local_url . 'login';
        } else {
            $backLink = $local_url;
        }
//        $backLink = $local_url;
        ?>
        <!--        --><?php //if(isset($backLink) && !empty($backLink) && $backLinkPath != $backLink) : ?>
        <!--        --><?php //if(isset($backLink) && !empty($backLink)) : ?>
        <a href="<?php echo Router::url('/'); ?>" class="btn btn-default pull-left btn-flat">Anuluj</a>
        <!--        --><?php //endif; ?>
    </div>
    <div class="col-xs-6">
        <?php echo $this->Form->submit('Zmień hasło', array('class' => 'btn btn-success btn-block btn-flat')); ?>
        <?php echo $this->Form->end(); ?>
    </div>
</div>
<div class="row" style="margin-top: 8%">
    <div class="col-xs-12">
        <?php foreach ($passwordInfoData as $rowInfo) : ?>
            <small class="help-block" style="margin-top: -4%"><em><?= $rowInfo ?></em></small>
        <?php endforeach; ?>
    </div>
</div>

<script>
    $(document).ready(function () {
        jQuery.validator.addMethod("passwords_equal", function (value, element) {
            var s_pass1 = $('#UserNewPassword').val();
            var s_pass2 = $('#UserReNewPassword').val();

            if (s_pass1 == s_pass2) {
                return true;
            } else {
                return false;
            }
        }, "Nowe hasła muszą być takie same.");

        $('#UserZmianaHaslaForm').validate({
            rules: {
                'data[User][username]': {
                    required: true
                },
                'data[User][password]': {
                    required: true
                },
                'data[User][new_password]': {
                    required: true
                },
                'data[User][re_new_password]': {
                    required: true,
                    passwords_equal: true
                },
            },
            messages: {
                'data[User][username]': 'Pole wymagane',
                'data[User][password]': 'Pole wymagane',
                'data[User][new_password]': 'Pole wymagane',
                'data[User][re_new_password]': {
                    required: 'Pole wymagane',
                    passwords_equal: 'Nowe hasła muszą być takie same'
                },
            }
        });
    });
</script>

<style>
    .custom-error-holder span {
        top: 0 !important;
    }
</style>

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