<?php
/**
 * Authorize.net Credit Card Template
 *
 * This template handles the credit card 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-credit-card-css', plugin_dir_url(__FILE__) . '../../assets/css/anet-credit-card-normal-checkout.css', array(), ANET_PLUGIN_VERSION);
wp_enqueue_script('anet-credit-card-js', plugin_dir_url(__FILE__) . '../../assets/js/frontend/anet-credit-card-normal-checkout.js', array('jquery'), ANET_PLUGIN_VERSION, true);
// Localize script to pass PHP variables to JavaScript.
wp_localize_script('anet-credit-card-js', 'anetCreditCardVars', array(
    'pluginUrl' => plugin_dir_url(__FILE__)
));
// Add inline script to clear eCheck fields when credit card payment method is selected.
wp_add_inline_script('anet-credit-card-js', "
jQuery(document).ready(function($) {
    // Function to clear eCheck fields
    function clearEcheckFields() {
        $('#anet-echeck-routing-number').val('');
        $('#anet-echeck-account-number').val('');
        $('#anet-echeck-account-name').val('');
        $('#anet-echeck-bank-account-type').val('');
    }
    // Clear eCheck fields when credit card payment method is selected
    $(document).on('change', 'input[name=\"payment_method\"]', function() {
        if ($(this).val() === 'anet' && $(this).is(':checked')) {
            clearEcheckFields();
        }
    });
    // Also clear when credit card payment method container becomes visible
    $(document).on('click', 'label[for=\"payment_method_anet\"]', function() {
        clearEcheckFields();
    });
});
");
$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;";
    }
    // Add focus styles for Astra theme.
    $anet_astra_focus_css = '
    .anet-astra-focus input[type="tel"]:focus,
    .anet-astra-focus select:focus {
        outline: var(--ast-global-color-3) !important;
        outline-offset: 1px !important;
        border-color: var(--ast-global-color-3) !important;
        box-shadow: 0 0 0 var(--ast-global-color-3) !important;
    }
    .anet-astra-focus input[type="tel"]:focus-visible,
    .anet-astra-focus select:focus-visible {
        outline: var(--ast-global-color-3) !important;
        outline-offset: 1px !important;
    }
';
    wp_register_style('anet-astra-focus-style', false, [], ANET_PLUGIN_VERSION);
    wp_enqueue_style('anet-astra-focus-style');
    wp_add_inline_style('anet-astra-focus-style', $anet_astra_focus_css);
} 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;";
    }
}
$anet_plugin_base_url = '/authorize-net-payments-for-woocommerce/assets/img/';
// Get allowed card types from gateway settings.
$anet_allowed_card_types = $this->allowed_card_types;
// Define card type configuration.
$anet_card_types = array(
    'visa' => array(
        'image' => 'card-visa.png',
        'alt' => 'Visa',
        'class' => 'wc-block-components-payment-method-icon--visa'
    ),
    'mastercard' => array(
        'image' => 'card-mastercard.png',
        'alt' => 'Mastercard',
        'class' => 'wc-block-components-payment-method-icon--mastercard'
    ),
    'amex' => array(
        'image' => 'card-amex.png',
        'alt' => 'American Express',
        'class' => 'wc-block-components-payment-method-icon--amex'
    ),
    'discover' => array(
        'image' => 'card-discover.png',
        'alt' => 'Discover',
        'class' => 'wc-block-components-payment-method-icon--discover'
    ),
    'jcb' => array(
        'image' => 'card-jcb.png',
        'alt' => 'JCB',
        'class' => 'wc-block-components-payment-method-icon--jcb'
    ),
    'diners-club' => array(
        'image' => 'card-dinersclub.png',
        'alt' => 'Diners Club',
        'class' => 'wc-block-components-payment-method-icon--dinersclub'
    )
);
?>
<div class="woocommerce-billing-fields__field-wrapper_credit_card" style="<?php echo esc_attr($anet_margin); ?>">
    <div class="form-row-pair">
        <p class="form-row form-row-wide validate-required" id="anet-card-number-div">
            <label class="required_field " for="anet-card-number"><?php esc_html_e('Card Number', 'authorize-net-payments-for-woocommerce'); ?>
                <span class="required" aria-hidden="true">*</span>
            </label>
            <span class="woocommerce-input-wrapper">
                <input id="anet-card-number" class="text-input-credit-card" data-anet="card-number" autocomplete="cc-number" placeholder="<?php echo esc_attr__('Enter Card Number', 'authorize-net-payments-for-woocommerce'); ?>" type="tel" 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;' : ''; ?>"
                    pattern="\d{13,19}">
            </span>
        </p>
        <p class="form-row form-row-wide validate-required" id="anet-card-cvc-div">
            <label class="required_field" for="anet-card-cvc"><?php esc_html_e('CVV', 'authorize-net-payments-for-woocommerce'); ?>
                <span class="required" aria-hidden="true">*</span>
            </label>
            <span class="woocommerce-input-wrapper">
                <input id="anet-card-cvc" class="text-input-credit-card" data-anet="card-cvc" autocomplete="cc-csc" placeholder="<?php echo esc_attr__('Enter CVV', 'authorize-net-payments-for-woocommerce'); ?>"
                    inputmode="numeric" minlength="3" maxlength="4" type="password" 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;' : ''; ?>"
                    pattern="\d{3,4}">
            </span>
        </p>
    </div>
    <div class="form-row-pair">
        <p class="form-row form-row-wide" id="anet-card-expiry-month-div">
            <label class="required_field" for="anet-card-expiry-month"><?php esc_html_e('Expiry Month', 'authorize-net-payments-for-woocommerce'); ?>
                <span class="required" aria-hidden="true">*</span>
            </label>
            <span class="woocommerce-input-wrapper">
                <select id="anet-card-expiry-month" class="text-input-credit-card" data-anet="expiry-month" autocomplete="cc-exp-month"
                    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;' : ''; ?>">
                    <option value="" disabled selected><?php esc_html_e('Select Month', 'authorize-net-payments-for-woocommerce'); ?></option>
                    <option value="01"><?php esc_html_e('January', 'authorize-net-payments-for-woocommerce'); ?></option>
                    <option value="02"><?php esc_html_e('February', 'authorize-net-payments-for-woocommerce'); ?></option>
                    <option value="03"><?php esc_html_e('March', 'authorize-net-payments-for-woocommerce'); ?></option>
                    <option value="04"><?php esc_html_e('April', 'authorize-net-payments-for-woocommerce'); ?></option>
                    <option value="05"><?php esc_html_e('May', 'authorize-net-payments-for-woocommerce'); ?></option>
                    <option value="06"><?php esc_html_e('June', 'authorize-net-payments-for-woocommerce'); ?></option>
                    <option value="07"><?php esc_html_e('July', 'authorize-net-payments-for-woocommerce'); ?></option>
                    <option value="08"><?php esc_html_e('August', 'authorize-net-payments-for-woocommerce'); ?></option>
                    <option value="09"><?php esc_html_e('September', 'authorize-net-payments-for-woocommerce'); ?></option>
                    <option value="10"><?php esc_html_e('October', 'authorize-net-payments-for-woocommerce'); ?></option>
                    <option value="11"><?php esc_html_e('November', 'authorize-net-payments-for-woocommerce'); ?></option>
                    <option value="12"><?php esc_html_e('December', 'authorize-net-payments-for-woocommerce'); ?></option>
                </select>
            </span>
        </p>
        <p class="form-row form-row-wide" id="anet-card-expiry-year-div">
            <label class="required_field" for="anet-card-expiry-year"><?php esc_html_e('Expiry Year', 'authorize-net-payments-for-woocommerce'); ?>
                <span class="required" aria-hidden="true">*</span>
            </label>
            <span class="woocommerce-input-wrapper">
                <select id="anet-card-expiry-year" class="text-input-credit-card" data-anet="expiry-year" autocomplete="cc-exp-year"
                    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;' : ''; ?>">
                    <option value="" disabled selected><?php esc_html_e('Select Year', 'authorize-net-payments-for-woocommerce'); ?></option>
                    <?php
                    $anet_current_year = gmdate('Y');
                    for ($anet_i = 0; $anet_i < 10; $anet_i++) {
                        $anet_expiry_year = $anet_current_year + $anet_i;
                        echo '<option value="' . esc_attr($anet_expiry_year) . '">' . esc_html($anet_expiry_year) . '</option>';
                    }
                    ?>
                </select>
            </span>
        </p>
    </div>
    <!-- Normal checkout save card option. -->
    <?php
    $anet_subscription = new Anet_Subscriptions();
    if ((is_user_logged_in() && is_checkout() && !empty($this->tokenization)) && !($anet_subscription->is_subscription_active && (WC_Subscriptions_Cart::cart_contains_subscription() || wcs_cart_contains_renewal() || WC_Subscriptions_Change_Payment_Gateway::$is_request_to_change_payment))) : ?>
        <div class="anet_form-group anet-save-card-group">
            <label for="anet_save_payment_method" style="display:inline;">
                <input id="anet_save_payment_method" name="anet_save_payment_method" class="wc-anet-save-card-credit-card" 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>