<?php
/**
 * Maho
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
 * @copyright   Copyright (c) 2021-2023 The OpenMage Contributors (https://openmage.org)
 * @copyright   Copyright (c) 2024 Maho (https://mahocommerce.com)
 * @license     https://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php
/**
 * Customer onepage checkout login form template
 *
 * @var $this Mage_Checkout_Block_Onepage_Login
 */
?>
<?php
$isRegistrationAllowed = $this->helper('customer')->isRegistrationAllowed();
$isRedirectRegisterStep = $this->helper('checkout')->isRedirectRegisterStep();
$isAllowedGuestCheckout = $this->helper('checkout')->isAllowedGuestCheckout($this->getQuote());
?>
<?php /* Extensions placeholder */ ?>
<?= $this->getChildHtml('login_before') ?>
<?= $this->getChildHtml('checkout.onepage.login.extra') ?>
<div class="col2-set<?= !($isRegistrationAllowed || $isAllowedGuestCheckout) ? ' login-only' : '' ?>">
    <?php if ($isRegistrationAllowed || $isAllowedGuestCheckout): ?>
    <div class="col-1">
        <?php if ($isRegistrationAllowed): ?>
            <?php if ($isAllowedGuestCheckout): ?>
                <h3><?= $this->__('Checkout as a Guest or Register') ?></h3>
            <?php else: ?>
                <h3><?= $this->__('Register to Create an Account') ?></h3>
            <?php endif ?>
            <p><?= $this->__('By creating an account with our store, you will be able to move through the checkout process faster, store multiple shipping addresses, view and track your orders in your account and more.') ?></p>
        <?php else: ?>
            <h3><?= $this->__('Checkout as a Guest') ?></h3>
        <?php endif ?>

        <?php if ($isRegistrationAllowed && $isAllowedGuestCheckout): ?>
            <ul class="form-list">
                <li class="control">
                    <input type="radio" name="checkout_method" id="login:guest" value="guest"<?php if($this->getQuote()->getCheckoutMethod() != Mage_Checkout_Model_Type_Onepage::METHOD_REGISTER): ?> checked="checked"<?php endif ?> class="radio" />
                    <label for="login:guest"><?= $this->__('Checkout as Guest') ?></label>
                </li>
                <li class="control">
                    <input type="radio" name="checkout_method" id="login:register" value="register"<?php if($this->getQuote()->getCheckoutMethod() == Mage_Checkout_Model_Type_Onepage::METHOD_REGISTER): ?> checked="checked"<?php endif ?> class="radio" />
                    <label for="login:register"><?= $this->__('Register and Checkout') ?></label>
                </li>
            </ul>
        <?php elseif ($isRegistrationAllowed): ?>
            <input type="hidden" name="checkout_method" id="login:register" value="register" checked="checked" />
        <?php else: ?>
            <input type="hidden" name="checkout_method" id="login:guest" value="guest" checked="checked" />
        <?php endif ?>

        <div class="buttons-set">
            <p class="required">&nbsp;</p>
            <button id="onepage-guest-register-button" type="button" class="button">
                <?= $this->__($isAllowedGuestCheckout ? 'Continue' : 'Register') ?>
            </button>
        </div>
    </div>
    <?php endif ?>
    <?php // This column should be col-1 if the registration column is not displayed ?>
    <div class="<?= ($isRegistrationAllowed || $isAllowedGuestCheckout) ? 'col-2' : 'col-1' ?> registered-users">
        <h3><?= $this->__('Login') ?></h3>
        <?= $this->getMessagesBlock()->toHtml() ?>
        <form id="login-form" action="<?= $this->getPostAction() ?>" method="post">
        <fieldset>
            <?= $this->getBlockHtml('formkey') ?>
            <h4><?= $this->__('Already registered?') ?></h4>
            <p><?= $this->__('Please log in below:') ?></p>
            <ul class="form-list">
                <li>
                    <label for="login-email" class="required"><em>*</em><?= $this->__('Email Address') ?></label>
                    <div class="input-box">
                        <input type="email" autocapitalize="off" autocorrect="off" spellcheck="false" class="input-text required-entry validate-email" id="login-email" name="login[username]" value="<?= $this->escapeHtml($this->getUsername()) ?>" />
                    </div>
                </li>
                <li>
                    <label for="login-password" class="required"><em>*</em><?= $this->__('Password') ?></label>
                    <div class="input-box">
                        <input type="password" class="input-text required-entry" id="login-password" name="login[password]" autocomplete="off" />
                    </div>
                </li>
                <li>
                    <a href="<?= $this->getUrl('customer/account/forgotpassword') ?>" class="f-left"><?= $this->__('Forgot your password?') ?></a>
                </li>
                <?= $this->getChildHtml('form.additional.info') ?>
                <?= $this->getChildHtml('remember.me') ?>
            </ul>
            <input name="context" type="hidden" value="checkout" />
        </fieldset>
        </form>
        <div class="buttons-set">
            <button id="onepage-login-button" type="submit" class="button"><?= $this->__('Login') ?></button>
        </div>
    </div>
</div>
<?php $registerParam = $this->getRequest()->getParam('register') ?>
<script type="text/javascript">
    const loginForm = new VarienForm('login-form', true);

    document.getElementById('login-email').addEventListener('keypress', bindLoginPost);
    document.getElementById('login-password').addEventListener('keypress', bindLoginPost);
    function bindLoginPost(evt) {
        if (evt.keyCode == Event.KEY_RETURN) {
            loginForm.submit();
        }
    }

    document.getElementById('onepage-login-button').addEventListener('click', onepageLogin);
    function onepageLogin() {
        if(loginForm.validator && loginForm.validator.validate()){
            this.disabled = true;
            loginForm.submit();
        }
    }

    document.getElementById('onepage-guest-register-button')?.addEventListener('click', onepageRegisterOrGuest);
    function onepageRegisterOrGuest() {
        const method = document.querySelector('input[name=checkout_method]:is(:checked,[type=hidden])')?.value;
        if (method === 'register') {
            <?php if ($isRedirectRegisterStep): ?>
                window.location = '<?= $this->quoteEscape($this->helper('checkout/url')->getRegistrationUrl()) ?>';
            <?php else: ?>
                checkout.setMethod();
            <?php endif; ?>
        } else if(method === 'guest') {
            checkout.setMethod();
        }
    }

    <?php if ($registerParam || $registerParam === ''): ?>
        document.addEventListener("DOMContentLoaded", function() {
            const registerCheckbox = document.getElementById('login:register');
            if (registerCheckbox) {
                registerCheckbox.checked = true;
                checkout.setMethod();
            }
        });
    <?php endif ?>
</script>
