<?php
declare(strict_types=1);

use Magento\Customer\Block\Account\Forgotpassword;
use Magento\Framework\Escaper;
use Hyva\Theme\ViewModel\ReCaptcha;

/** @var Forgotpassword $block */
/** @var Escaper $escaper */

$formId = 'user_forgotpassword';

/** @var ReCaptcha $recaptchaViewModel */
//Do not replace this with $viewModels->require(ReCaptcha::class); that might break the dependency on
//Magento's Recaptcha module
$recaptchaViewModel = $block->getData('viewModelRecaptcha');
$recaptchaInputFieldBlock = '';
$recaptchaLegalNoticeBlock = '';
if ($recaptchaViewModel && $recaptchaData = $recaptchaViewModel->getRecaptchaData('customer_forgot_password')) {
    $recaptchaInputFieldBlock  = $recaptchaData[ReCaptcha::RECAPTCHA_INPUT_FIELD];
    $recaptchaLegalNoticeBlock = $recaptchaData[ReCaptcha::RECAPTCHA_LEGAL_NOTICE_BLOCK];
}
?>
<div class="container pl-0 mb-12">
    <div class="w-full my-8 md:w-1/2">
        <form action="<?= $escaper->escapeUrl($block->getUrl('*/*/forgotpasswordpost')) ?>"
              method="post"
              id="<?= $escaper->escapeHtmlAttr($formId) ?>"
              x-data="initPasswordForm()"
              @submit.prevent="submitForm();">
            <?= $block->getBlockHtml('formkey') ?>
            <input type="hidden" name="formId" value="<?= $escaper->escapeHtmlAttr($formId) ?>"/>
            <?= $block->getChildHtml('form_fields_before') ?>
            <?= $block->getBlockHtml($recaptchaInputFieldBlock) ?>
            <fieldset class="fieldset">
                <div class="mb-8 font-medium text-black"><?= $escaper->escapeHtml(
                    __('Please enter your email address below to receive a password reset link.')
                ) ?></div>
                <div class="field email required">
                    <label for="email_address" class="label"><span><?= $escaper->escapeHtml(
                        __('Email')
                    ) ?></span></label>
                    <div class="control">
                        <input type="email" name="email" alt="email" id="email_address" class="form-input" required
                               value="<?= $escaper->escapeHtmlAttr($block->getEmailValue()) ?>">
                    </div>
                </div>
                <?= $block->getChildHtml('form_additional_info') ?>
                <?= $block->getBlockHtml($recaptchaLegalNoticeBlock) ?>
            </fieldset>
            <div class="actions-toolbar">
                <div class="primary">
                    <button type="submit" class="action submit primary"><span><?= $escaper->escapeHtml(
                        __('Reset My Password')
                    ) ?></span></button>
                </div>
                <div class="secondary">
                    <a class="action back"
                       href="<?= $escaper->escapeUrl($block->getLoginUrl()) ?>"><span><?= $escaper->escapeHtml(
                           __('Go back')
                       ) ?></span></a>
                </div>
            </div>
        </form>
        <script>
            function initPasswordForm() {
                return {
                    errors: 0,
                    hasCaptchaToken: 0,
                    submitForm() {
                        // Do not rename $form, it is expected to be declared in the child block output
                        const $form = document.querySelector('#<?= $escaper->escapeJs($formId) ?>');
                        <?= $block->getChildHtml('recaptcha_validation'); ?>

                        if (this.errors === 0) {
                            $form.submit();
                        }
                    }
                }
            }
        </script>
    </div>
</div>
