<?php
/**
 * Authorize.net eCheck Template
 *
 * This template handles the eCheck form display for Authorize.net payment gateway.
 * It includes proper styling and JavaScript integration for the payment form.
 *
 * @package Authorize_Net_Payments_For_Woocommerce
 */

if (! defined('ABSPATH')) {
    exit;
}

// Enqueue the external CSS and JS files.
wp_enqueue_style('anet-echeck-css', plugin_dir_url(__FILE__) . '../../assets/css/anet-echeck-normal-checkout.css', array(), ANET_PLUGIN_VERSION);
wp_enqueue_script('anet-echeck-js', plugin_dir_url(__FILE__) . '../../assets/js/frontend/anet-echeck-normal-checkout.js', array('jquery'), ANET_PLUGIN_VERSION, true);
// Localize script to pass PHP variables to JavaScript.
wp_localize_script('anet-echeck-js', 'anetEcheckVars', array(
    'pluginUrl' => plugin_dir_url(__FILE__)
));
// Add inline script to clear credit card fields when eCheck payment method is selected.
wp_add_inline_script('anet-echeck-js', "
jQuery(document).ready(function($) {
    // Function to clear credit card fields
    function clearCreditCardFields() {
        $('#anet-card-number').val('');
        $('#anet-card-cvc').val('');
        $('#anet-card-expiry-month').val('');
        $('#anet-card-expiry-year').val('');
        // Also clear any card type background images
        $('#anet-card-number').css({
            'background-image': 'none',
            'padding-right': ''
        });
    }
    // Clear credit card fields when eCheck payment method is selected
    $(document).on('change', 'input[name=\"payment_method\"]', function() {
        if ($(this).val() === 'anet_echeck' && $(this).is(':checked')) {
            clearCreditCardFields();
        }
    });
    // Also clear when eCheck payment method container becomes visible
    $(document).on('click', 'label[for=\"payment_method_anet_echeck\"]', function() {
        clearCreditCardFields();
    });
});
");
$anet_current_url = (isset($_SERVER['HTTPS']) && 'on' === $_SERVER['HTTPS'] ? "https" : "http");
$anet_host = isset($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : '';
$anet_request_uri = isset($_SERVER['REQUEST_URI']) ? esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])) : '';
$anet_current_url .= ANET_COLON_SLASH . esc_url_raw($anet_host . $anet_request_uri);
$anet_last_segment = basename(wp_parse_url($anet_current_url, PHP_URL_PATH));
// Retrieve the active theme information for styling adjustments.
$anet_current_theme = wp_get_theme();
$anet_theme_name = $anet_current_theme->get('Name');
$anet_expiry_month_style = '';
$anet_card_number_style = '';
$anet_focus_styles = '';
if (ANET_ASTRA_THEME === $anet_theme_name) {
    $anet_expiry_month_width = '100%';
    $anet_expiry_month_height = '40px';
    $anet_card_number_width = '100%';
    $anet_card_number_height = '40px';
    $anet_margin = "margin-left: 20px;";
    if (ANET_ADD_PAYMENT_METHODS === $anet_last_segment) {
        $anet_margin = "margin-left: 1px;";
    }
} else {
    $anet_expiry_month_width = '90%';
    $anet_expiry_month_height = 'auto';
    $anet_card_number_width = '90%';
    $anet_card_number_height = 'auto';
    $anet_margin = "margin-left: 40px;";
    if (ANET_ADD_PAYMENT_METHODS === $anet_last_segment) {
        $anet_margin = "margin-left: 1px;";
    }
}
?>
<?php
// Output focus styles for Astra theme.
if (ANET_ASTRA_THEME === $anet_theme_name) {
    echo wp_kses($anet_focus_styles, array(
        'style' => array(),
    ));
}
?>
<!-- eCheck Form HTML -->
<div class="woocommerce-billing-fields__field-wrapper_echeck" style="<?php echo esc_attr($anet_margin); ?>">
    <div class="form-row-pair">
        <p class="form-row form-row-wide validate-required" id="anet-bank-routing-number-div">
            <label class="anet-bank-routing-number-label" for="anet-echeck-routing-number"><?php esc_html_e('Routing Number', 'authorize-net-payments-for-woocommerce'); ?>
                <span class="required" aria-hidden="true">*</span>
            </label>
            <span class="woocommerce-input-wrapper">
                <input id="anet-echeck-routing-number" class="text-input-echeck" data-anet="routing-number" type="tel" placeholder="<?php echo esc_attr__('Enter Routing Number', 'authorize-net-payments-for-woocommerce'); ?>" inputmode="numeric" style="width: <?php echo esc_attr($anet_card_number_width); ?> !important;<?php echo 'auto' !== $anet_card_number_height ? ' height: ' . esc_attr($anet_card_number_height) . ' !important;' : ''; ?>" aria-required="true" autocomplete="routing-number" minlength="9" maxlength="9" pattern="[0-9]{9}">
            </span>
        </p>
        <p class="form-row form-row-wide validate-required" id="anet-bank-account-number-div">
            <label class="anet-bank-account-number-label" for="anet-echeck-account-number"><?php esc_html_e('Account Number', 'authorize-net-payments-for-woocommerce'); ?>
                <span class="required" aria-hidden="true">*</span>
            </label>
            <span class="woocommerce-input-wrapper">
                <input id="anet-echeck-account-number" class="text-input-echeck" data-anet="account-number" type="tel" placeholder="<?php echo esc_attr__('Enter Account Number', 'authorize-net-payments-for-woocommerce'); ?>" inputmode="numeric" style="width: <?php echo esc_attr($anet_card_number_width); ?> !important;<?php echo 'auto' !== $anet_card_number_height ? ' height: ' . esc_attr($anet_card_number_height) . ' !important;' : ''; ?>" aria-required="true" autocomplete="account-number" minlength="6" maxlength="17" pattern="[0-9]{6,17}">
            </span>
        </p>
    </div>
    <div class="form-row-pair">
        <p class="form-row form-row-wide validate-required" id="anet-bank-account-name-div">
            <label class="anet-bank-account-name-label" for="anet-echeck-account-name"><?php esc_html_e('Account Name', 'authorize-net-payments-for-woocommerce'); ?>
                <span class="required" aria-hidden="true">*</span>
            </label>
            <span class="woocommerce-input-wrapper">
                <input id="anet-echeck-account-name" class="text-input-echeck" data-anet="account-name" type="text" placeholder="<?php echo esc_attr__('Enter Account Name', 'authorize-net-payments-for-woocommerce'); ?>" inputmode="string" style="width: <?php echo esc_attr($anet_card_number_width); ?> !important;<?php echo 'auto' !== $anet_card_number_height ? ' height: ' . esc_attr($anet_card_number_height) . ' !important;' : ''; ?>" aria-required="true" autocomplete="account-name" maxlength="22">
            </span>
        </p>
        <p class="form-row form-row-wide" id="anet-bank-account-type-div">
            <label class="anet-bank-account-type-label" for="anet-echeck-bank-account-type"><?php esc_html_e('Account Type', 'authorize-net-payments-for-woocommerce'); ?>
                <span class="required" aria-hidden="true">*</span>
            </label>
            <span class="woocommerce-input-wrapper">
                <select id="anet-echeck-bank-account-type" class="text-input-echeck" data-anet="account-type" style="width: <?php echo esc_attr($anet_expiry_month_width); ?> !important;<?php echo 'auto' !== $anet_expiry_month_height ? ' height: ' . esc_attr($anet_expiry_month_height) . ' !important;' : ''; ?>" placeholder="" aria-required="true" autocomplete="account-type">
                    <option value="" disabled selected><?php esc_html_e('Select Account Type', 'authorize-net-payments-for-woocommerce'); ?></option>
                    <option value="checking"><?php esc_html_e('Checking', 'authorize-net-payments-for-woocommerce'); ?></option>
                    <option value="savings"><?php esc_html_e('Savings', 'authorize-net-payments-for-woocommerce'); ?></option>
                </select>
            </span>
        </p>
    </div>
    <!-- normal checkout save card option -->
    <?php if (is_user_logged_in() && is_checkout() && !empty($this->tokenization)) : ?>
        <div class="anet_echeck-form-group anet-echeck-save-card-group">
            <label for="anet-echeck-save-card" style="display:inline;">
                <input id="anet_echeck_save_payment_method" name="anet_echeck_save_payment_method" class="wc-anet-save-card-echeck" type="checkbox" value="1" style="width:auto;" />
                    <?php esc_html_e('Save payment information to my account for future purchases.', 'authorize-net-payments-for-woocommerce'); ?>            
            </label>
        </div>
    <?php endif; ?>
</div>