<?php
/**
 * Copyright 2014 Adobe
 * All Rights Reserved.
 */

use Magento\Customer\Block\Widget\Name;

/** @var \Magento\Customer\Block\Form\Edit $block */
if (!$block->getButtonLockManager()) {
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $block->setButtonLockManager(
        $objectManager->get(\Magento\Framework\View\Element\ButtonLockManager::class)
    );
}
/** @var $escaper \Magento\Framework\Escaper */
/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
?>
<form class="form form-edit-account"
      action="<?= $escaper->escapeUrl($block->getUrl('customer/account/editPost')) ?>"
      method="post" id="form-validate"
      enctype="multipart/form-data"
      data-hasrequired="<?= $escaper->escapeHtmlAttr(__('* Required Fields')) ?>"
      autocomplete="off">
    <fieldset class="fieldset info">
        <?= $block->getBlockHtml('formkey') ?>
        <legend class="legend"><span><?= $escaper->escapeHtml(__('Account Information')) ?></span></legend><br>
        <?= $block->getLayout()->createBlock(Name::class)->setObject($block->getCustomer())->toHtml() ?>

        <?php $_dob = $block->getLayout()->createBlock(\Magento\Customer\Block\Widget\Dob::class) ?>
        <?php $_taxvat = $block->getLayout()->createBlock(\Magento\Customer\Block\Widget\Taxvat::class) ?>
        <?php $_gender = $block->getLayout()->createBlock(\Magento\Customer\Block\Widget\Gender::class) ?>
        <?php if ($_dob->isEnabled()): ?>
            <?= $_dob->setDate($block->getCustomer()->getDob())->toHtml() ?>
        <?php endif ?>
        <?php if ($_taxvat->isEnabled()): ?>
            <?= $_taxvat->setTaxvat($block->getCustomer()->getTaxvat())->toHtml() ?>
        <?php endif ?>
        <?php if ($_gender->isEnabled()): ?>
            <?= $_gender->setGender($block->getCustomer()->getGender())->toHtml() ?>
        <?php endif ?>
        <div class="field choice">
            <input type="checkbox" name="change_email" id="change-email" data-role="change-email" value="1"
                   title="<?= $escaper->escapeHtmlAttr(__('Change Email')) ?>" class="checkbox" />
            <label class="label" for="change-email">
                <span><?= $escaper->escapeHtml(__('Change Email')) ?></span>
            </label>
        </div>
        <div class="field choice">
            <input type="checkbox" name="change_password" id="change-password" data-role="change-password" value="1"
                   title="<?= $escaper->escapeHtmlAttr(__('Change Password')) ?>"
                <?php if ($block->getChangePassword()): ?> checked="checked"<?php endif; ?> class="checkbox" />
            <label class="label" for="change-password">
                <span><?= $escaper->escapeHtml(__('Change Password')) ?></span>
            </label>
        </div>
        <?= $block->getChildHtml('fieldset_edit_info_additional') ?>
    </fieldset>

    <fieldset class="fieldset password" data-container="change-email-password">
        <legend class="legend">
            <span data-title="change-email-password"><?= $escaper->escapeHtml(__('Change Email and Password')) ?></span>
        </legend><br>
        <div class="field email required" data-container="change-email">
            <label class="label" for="email"><span><?= $escaper->escapeHtml(__('Email')) ?></span></label>
            <div class="control">
                <input type="email" name="email" id="email" autocomplete="email" data-input="change-email"
                       value="<?= $escaper->escapeHtmlAttr($block->getCustomer()->getEmail()) ?>"
                       title="<?= $escaper->escapeHtmlAttr(__('Email')) ?>"
                       class="input-text"
                       data-validate="{required:true, 'validate-email':true}" />
            </div>
        </div>
        <div class="field password current required">
            <label class="label" for="current-password">
                <span><?= $escaper->escapeHtml(__('Current Password')) ?></span>
            </label>
            <div class="control">
                <input type="password" class="input-text" name="current_password" id="current-password"
                       data-input="current-password"
                       autocomplete="off" />
            </div>
        </div>
        <div class="field new password required" data-container="new-password">
            <label class="label" for="password"><span><?= $escaper->escapeHtml(__('New Password')) ?></span></label>
            <div class="control">
                <?php $minCharacterSets = $block->getRequiredCharacterClassesNumber() ?>
                <input type="password" class="input-text" name="password" id="password"
                    data-password-min-length="<?= $escaper->escapeHtml($block->getMinimumPasswordLength()) ?>"
                    data-password-min-character-sets="<?= $escaper->escapeHtml($minCharacterSets) ?>"
                    data-input="new-password"
                    data-validate="{required:true, 'validate-customer-password':true}"
                    autocomplete="off" />
                <div id="password-strength-meter-container" data-role="password-strength-meter" aria-live="polite">
                    <div id="password-strength-meter" class="password-strength-meter">
                        <?= $escaper->escapeHtml(__('Password Strength')) ?>:
                        <span id="password-strength-meter-label" data-role="password-strength-meter-label">
                            <?= $escaper->escapeHtml(__('No Password')) ?>
                        </span>
                    </div>
                </div>
            </div>
        </div>
        <div class="field confirmation password required" data-container="confirm-password">
            <label class="label" for="password-confirmation">
                <span><?= $escaper->escapeHtml(__('Confirm New Password')) ?></span>
            </label>
            <div class="control">
                <input type="password" class="input-text" name="password_confirmation" id="password-confirmation"
                    data-input="confirm-password"
                    autocomplete="off" />
            </div>
        </div>
        <div class="field choice" data-bind="scope: 'showPassword'">
            <!-- ko template: getTemplate() --><!-- /ko -->
        </div>
    </fieldset>

    <fieldset class="fieldset additional_info">
        <?= $block->getChildHtml('form_additional_info') ?>
    </fieldset>

    <div class="actions-toolbar">
        <div class="primary">
            <button type="submit" class="action save primary" title="<?= $escaper->escapeHtmlAttr(__('Save')) ?>"
                <?php if ($block->getButtonLockManager()->isDisabled('customer_edit_form_submit')): ?>
                    disabled="disabled"
                <?php endif; ?>>
            <span><?= $escaper->escapeHtml(__('Save')) ?></span>
            </button>
        </div>
        <div class="secondary">
            <a class="action back" href="<?= $escaper->escapeUrl($block->getBackUrl()) ?>">
                <span><?= $escaper->escapeHtml(__('Go back')) ?></span>
            </a>
        </div>
    </div>
</form>
<?php $ignore = /* @noEscape */ $_dob->isEnabled() ? '\'input[id$="full"]\'' : 'null';
$scriptString = <<<script
    require([
        "jquery",
        "mage/mage"
    ], function($){
        var dataForm = $('#form-validate');
        var ignore = {$ignore};

        dataForm.mage('validation', {
script;
if ($_dob->isEnabled()):
    $scriptString .= <<<script
            errorPlacement: function(error, element) {
                if (element.prop('id').search('full') !== -1) {
                    var dobElement = $(element).parents('.customer-dob'),
                        errorClass = error.prop('class');
                    error.insertAfter(element.parent());
                    dobElement.find('.validate-custom').addClass(errorClass)
                        .after('<div class="' + errorClass + '"></div>');
                }
                else {
                    error.insertAfter(element);
                }
            },
            ignore: ':hidden:not(' + ignore + ')'
script;
else:
    $scriptString .= <<<script
            ignore: ignore ? ':hidden:not(' + ignore + ')' : ':hidden'
script;
endif;
$scriptString .= <<<script
        });

    });
script;
?>
<?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false) ?>
<?php $changeEmailAndPasswordTitle = $escaper->escapeHtml(__('Change Email and Password')) ?>
<script type="text/x-magento-init">
    {
        "[data-role=change-email], [data-role=change-password]": {
            "changeEmailPassword": {
                "titleChangeEmail": "<?= $escaper->escapeJs($escaper->escapeHtml(__('Change Email'))) ?>",
                "titleChangePassword": "<?= $escaper->escapeJs($escaper->escapeHtml(__('Change Password'))) ?>",
                "titleChangeEmailAndPassword": "<?= $escaper->escapeJs($changeEmailAndPasswordTitle) ?>"
            }
        },
        "[data-container=new-password]": {
            "passwordStrengthIndicator": {
                "formSelector": "form.form-edit-account"
            }
        },
        "*": {
            "Magento_Ui/js/core/app": {
                "components": {
                    "showPassword": {
                        "component": "Magento_Customer/js/show-password",
                        "passwordSelector": "#current-password,#password,#password-confirmation"
                    }
                }
            }
        }
    }
</script>
