<?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)
 */
/** @var Mage_Checkout_Block_Onepage_Billing $this */
?>
<form id="co-billing-form" action="">
<div class="fieldset">
    <p class="required"><?= $this->__('* Required Fields') ?></p>
    <ul class="form-list">
    <?php if ($this->customerHasAddresses()): ?>
        <li class="wide">
            <label for="billing-address-select"><?= $this->__('Select a billing address from your address book or enter a new address.') ?></label>
            <div class="input-box">
                <?= $this->getAddressesHtmlSelect('billing') ?>
            </div>
        </li>
    <?php endif ?>
    <li id="billing-new-address-form"<?php if ($this->customerHasAddresses()): ?> style="display:none;"<?php endif ?>>
        <div class="fieldset">
            <input type="hidden" name="billing[address_id]" value="<?= $this->getAddress()->getId() ?>" id="billing:address_id" />
            <ul>
                <li class="fields"><?= $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getAddress()->getFirstname() ? $this->getAddress() : $this->getQuote()->getCustomer())->setForceUseCustomerRequiredAttributes(!$this->isCustomerLoggedIn())->setFieldIdFormat('billing:%s')->setFieldNameFormat('billing[%s]')->toHtml() ?></li>
                <li class="fields">
                    <div class="field">
                        <label for="billing:company"><?= $this->__('Company') ?></label>
                        <div class="input-box">
                            <input type="text" id="billing:company" name="billing[company]" value="<?= $this->escapeHtml($this->getAddress()->getCompany()) ?>" title="<?= $this->quoteEscape($this->__('Company')) ?>" class="input-text <?= $this->helper('customer/address')->getAttributeValidationClass('company') ?>" />
                        </div>
                    </div>
        <?php if(!$this->isCustomerLoggedIn()): ?>
                    <div class="field">
                        <label for="billing:email" class="required"><em>*</em><?= $this->__('Email Address') ?></label>
                        <div class="input-box">
                            <input type="email" autocapitalize="off" autocorrect="off" spellcheck="false" name="billing[email]" id="billing:email" value="<?= $this->escapeHtml($this->getAddress()->getEmail()) ?>" title="<?= $this->quoteEscape($this->__('Email Address')) ?>" class="input-text validate-email required-entry" />
                        </div>
                    </div>
        <?php endif ?>
                </li>
        <?php $_streetValidationClass = $this->helper('customer/address')->getAttributeValidationClass('street'); ?>
                <li class="wide">
                    <label for="billing:street1" class="required"><em>*</em><?= $this->__('Address') ?></label>
                    <div class="input-box">
                        <input type="text" title="<?= $this->quoteEscape($this->__('Street Address')) ?>" name="billing[street][]" id="billing:street1" value="<?= $this->escapeHtml($this->getAddress()->getStreet(1)) ?>" class="input-text <?= $_streetValidationClass ?>" />
                    </div>
                </li>
        <?php $_streetValidationClass = trim(str_replace('required-entry', '', $_streetValidationClass)); ?>
        <?php for ($_i = 2, $_n = $this->helper('customer/address')->getStreetLines(); $_i <= $_n; $_i++): ?>
                <li class="wide">
                    <label for="billing:street<?= $_i ?>"><?= $this->__('Street Address %s', $_i) ?></label>
                    <div class="input-box">
                        <input type="text" title="<?= $this->quoteEscape($this->__('Street Address %s', $_i)) ?>" name="billing[street][]" id="billing:street<?= $_i ?>" value="<?= $this->escapeHtml($this->getAddress()->getStreet($_i)) ?>" class="input-text <?= $_streetValidationClass ?>" />
                    </div>
                </li>
        <?php endfor ?>
                <?php if ($this->helper('customer/address')->isVatAttributeVisible()) : ?>
                <li class="wide">
                    <label for="billing:vat_id"><?= $this->__('VAT Number') ?></label>
                    <div class="input-box">
                        <input type="text" id="billing:vat_id" name="billing[vat_id]" value="<?= $this->escapeHtml($this->getAddress()->getVatId()) ?>" title="<?= $this->quoteEscape($this->__('VAT Number')) ?>" class="input-text <?= $this->helper('customer/address')->getAttributeValidationClass('vat_id') ?>" />
                    </div>
                </li>
                <?php endif ?>
                <li class="fields">
                    <div class="field">
                        <label for="billing:city" class="required"><em>*</em><?= $this->__('City') ?></label>
                        <div class="input-box">
                            <input type="text" title="<?= $this->quoteEscape($this->__('City')) ?>" name="billing[city]" value="<?= $this->escapeHtml($this->getAddress()->getCity()) ?>" class="input-text <?= $this->helper('customer/address')->getAttributeValidationClass('city') ?>" id="billing:city" />
                        </div>
                    </div>
                    <div class="field">
                        <label for="billing:region_id" class="required"><em>*</em><?= $this->__('State/Province') ?></label>
                        <div class="input-box">
                            <select id="billing:region_id" name="billing[region_id]" title="<?= $this->quoteEscape($this->__('State/Province')) ?>" class="validate-select" style="display:none;">
                                <option value=""><?= $this->__('Please select region, state or province') ?></option>
                            </select>
                            <script type="text/javascript">
                                document.getElementById('billing:region_id').setAttribute('defaultValue', "<?= $this->getAddress()->getRegionId() ?>");
                            </script>
                            <input type="text" id="billing:region" name="billing[region]" value="<?= $this->escapeHtml($this->getAddress()->getRegion()) ?>"  title="<?= $this->quoteEscape($this->__('State/Province')) ?>" class="input-text <?= $this->helper('customer/address')->getAttributeValidationClass('region') ?>" style="display:none;" />
                        </div>
                    </div>
                </li>
                <li class="fields">
                    <div class="field">
                        <label for="billing:postcode" class="required"><em>*</em><?= $this->__('Zip/Postal Code') ?></label>
                        <div class="input-box">
                            <input type="text" title="<?= $this->quoteEscape($this->__('Zip/Postal Code')) ?>" name="billing[postcode]" id="billing:postcode" value="<?= $this->escapeHtml($this->getAddress()->getPostcode()) ?>" class="input-text validate-zip-international <?= $this->helper('customer/address')->getAttributeValidationClass('postcode') ?>" />
                        </div>
                    </div>
                    <div class="field">
                        <label for="billing:country_id" class="required"><em>*</em><?= $this->__('Country') ?></label>
                        <div class="input-box">
                            <?= $this->getCountryHtmlSelect('billing') ?>
                        </div>
                    </div>
                </li>
                <li class="fields">
                    <div class="field">
                        <label for="billing:telephone" class="required"><em>*</em><?= $this->__('Telephone') ?></label>
                        <div class="input-box">
                            <input type="tel" name="billing[telephone]" value="<?= $this->escapeHtml($this->getAddress()->getTelephone()) ?>" title="<?= $this->quoteEscape($this->__('Telephone')) ?>" class="input-text <?= $this->helper('customer/address')->getAttributeValidationClass('telephone') ?>" id="billing:telephone" />
                        </div>
                    </div>
                    <div class="field">
                        <label for="billing:fax"><?= $this->__('Fax') ?></label>
                        <div class="input-box">
                            <input type="tel" name="billing[fax]" value="<?= $this->escapeHtml($this->getAddress()->getFax()) ?>" title="<?= $this->quoteEscape($this->__('Fax')) ?>" class="input-text <?= $this->helper('customer/address')->getAttributeValidationClass('fax') ?>" id="billing:fax" />
                        </div>
                    </div>
                </li>
                <?php if(!$this->isCustomerLoggedIn()): ?>

        <?php $_dob = $this->getLayout()->createBlock('customer/widget_dob') ?>
        <?php $_gender = $this->getLayout()->createBlock('customer/widget_gender') ?>
            <?php if ($_dob->isEnabled() || $_gender->isEnabled()): ?>
                <li class="fields">
                <?php if ($_dob->isEnabled()): ?>
                    <div class="field">
                        <?= $_dob->setDate($this->getQuote()->getCustomerDob())->setFieldIdFormat('billing:%s')->setFieldNameFormat('billing[%s]')->toHtml() ?>
                    </div>
                <?php endif ?>
                <?php if ($_gender->isEnabled()): ?>
                    <div class="field">
                        <?= $_gender->setGender($this->getQuote()->getCustomerGender())->setFieldIdFormat('billing:%s')->setFieldNameFormat('billing[%s]')->toHtml() ?>
                    </div>
                <?php endif ?>
                </li>
            <?php endif ?>

            <?php if ($this->isTaxvatEnabled()):?>
                <li><?= $this->getTaxvatHtml() ?></li>
            <?php endif ?>

                <li class="fields" id="register-customer-password">
                    <div class="field">
                        <label for="billing:customer_password" class="required"><em>*</em><?= $this->__('Password') ?></label>
                        <div class="input-box">
                            <?php $minPasswordLength = $this->getQuote()->getCustomer()->getMinPasswordLength(); ?>
                            <input type="password"
                                   name="billing[customer_password]"
                                   id="billing:customer_password"
                                   title="<?= $this->quoteEscape($this->__('Password')) ?>"
                                   class="input-text required-entry validate-password min-pass-length-<?= $minPasswordLength ?>"
                                   autocomplete="new-password" />
                            <p class="form-instructions">
                                <?= Mage::helper('customer')->__('The minimum password length is %s', $minPasswordLength) ?>
                            </p>
                        </div>
                    </div>
                    <div class="field">
                        <label for="billing:confirm_password" class="required"><em>*</em><?= $this->__('Confirm Password') ?></label>
                        <div class="input-box">
                            <input type="password" name="billing[confirm_password]" title="<?= $this->quoteEscape($this->__('Confirm Password')) ?>" id="billing:confirm_password" class="input-text required-entry validate-cpassword" autocomplete="new-password" />
                        </div>
                    </div>
                </li>
                <?= $this->getChildHtml('remember.me') ?>
                <?php endif ?>
                <?php if ($this->isCustomerLoggedIn() && $this->customerHasAddresses()):?>
                    <li class="control">
                        <input type="checkbox" name="billing[save_in_address_book]" value="1" title="<?= $this->quoteEscape($this->__('Save in address book')) ?>" id="billing:save_in_address_book" onchange="if(window.shipping) shipping.setSameAsBilling(false);"<?php if ($this->getAddress()->getSaveInAddressBook()):?> checked="checked"<?php endif ?> class="checkbox" /><label for="billing:save_in_address_book"><?= $this->__('Save in address book') ?></label>
                    </li>
                <?php else: ?>
                    <li class="no-display"><input type="hidden" name="billing[save_in_address_book]" value="1" /></li>
                <?php endif ?>
                <?= $this->getChildHtml('form.additional.info') ?>
            </ul>
        </div>
    </li>
    <?php /* Extensions placeholder */ ?>
    <?= $this->getChildHtml('checkout.onepage.billing.extra') ?>
    <?php if ($this->canShip()): ?>
        <li class="control">
            <input type="radio" name="billing[use_for_shipping]" id="billing:use_for_shipping_yes" value="1"<?php if ($this->isUseBillingAddressForShipping()) {?> checked="checked"<?php }?> title="<?= $this->quoteEscape($this->__('Ship to this address')) ?>" onclick="document.getElementById('shipping:same_as_billing').checked = true;" class="radio" /><label for="billing:use_for_shipping_yes"><?=  $this->__('Ship to this address') ?></label></li>
        <li class="control">
            <input type="radio" name="billing[use_for_shipping]" id="billing:use_for_shipping_no" value="0"<?php if (!$this->isUseBillingAddressForShipping()) {?> checked="checked"<?php }?> title="<?= $this->quoteEscape($this->__('Ship to different address')) ?>" onclick="document.getElementById('shipping:same_as_billing').checked = false;" class="radio" /><label for="billing:use_for_shipping_no"><?= $this->__('Ship to different address') ?></label>
        </li>
    <?php endif ?>
    </ul>
    <?php if (!$this->canShip()): ?>
        <input type="hidden" name="billing[use_for_shipping]" value="1" />
    <?php endif ?>
    <div class="buttons-set" id="billing-buttons-container">
        <button type="button" title="<?= $this->quoteEscape($this->__('Continue')) ?>" class="button" onclick="billing.save()"><?= $this->__('Continue') ?></button>
        <span class="please-wait" id="billing-please-wait" style="display:none;">
            <img src="<?= $this->getSkinUrl('images/loading.svg') ?>" alt="<?= $this->quoteEscape($this->__('Loading next step...')) ?>" title="<?= $this->quoteEscape($this->__('Loading next step...')) ?>" class="v-middle" /> <?= $this->__('Loading next step...') ?>
        </span>
    </div>
    <?= $this->getBlockHtml('formkey') ?>
</div>
</form>
<script type="text/javascript">
    var billing = new Billing('co-billing-form', '<?= $this->getUrl('checkout/onepage/getAddress') ?>address/', '<?= $this->getUrl('checkout/onepage/saveBilling') ?>');
    var billingForm = new VarienForm('co-billing-form');

    const billingAddressSelect = document.getElementById('billing-address-select');
    if (billingAddressSelect) {
        billing.newAddress(!billingAddressSelect.value);
    }

    var billingRegionUpdater = new RegionUpdater(
        'billing:country_id', 'billing:region', 'billing:region_id',
        <?= Mage::helper('directory')->getRegionJsonByStore() ?>, undefined, 'billing:postcode'
    );

    const guestRegisterButton = document.getElementById('onepage-guest-register-button');
    if (guestRegisterButton) {
        guestRegisterButton.addEventListener('click', function(event) {
            const billingForm = document.getElementById('co-billing-form');
            const billingRememberMe = billingForm.querySelectorAll('.remember-me-box');

            if (billingRememberMe.length > 0) {
                const method = document.querySelector('input[name=checkout_method]:is(:checked,[type=hidden])')?.value;
                if (method === 'guest') {
                    billingRememberMe[0].style.display = 'none';
                } else if (method === 'register') {
                    billingRememberMe[0].style.display = 'block';
                }
            }
        });
    }
</script>
