<?php

/**
 * My Account - View Single Subscription Template
 *
 * @package ArraySubs
 * @var array $arraysubs_subscription_data Subscription data
 * @var int $arraysubs_subscription_id Subscription ID
 */

defined('ABSPATH') || exit;

$arraysubs_effective_product_id = function_exists('arraysubs_get_subscription_effective_product_id')
    ? arraysubs_get_subscription_effective_product_id($arraysubs_subscription_id)
    : absint($arraysubs_subscription_data['variation_id'] ?? $arraysubs_subscription_data['product_id'] ?? 0);
$arraysubs_product = $arraysubs_effective_product_id > 0 ? wc_get_product($arraysubs_effective_product_id) : null;
$arraysubs_product_permalink = $arraysubs_product ? $arraysubs_product->get_permalink() : '';
$arraysubs_customer = get_user_by('id', $arraysubs_subscription_data['customer_id']);
$arraysubs_status = get_post_status($arraysubs_subscription_id);
$arraysubs_status_name = arraysubs_get_subscription_status_name($arraysubs_status);
$arraysubs_order_ids = arraysubs_get_subscription_orders($arraysubs_subscription_id);

// Get product name with variation suffix
$arraysubs_product_name = arraysubs_get_subscription_product_name_by_subscription($arraysubs_subscription_id);
if (empty($arraysubs_product_name) && $arraysubs_product) {
    $arraysubs_product_name = $arraysubs_product->get_name();
}

$arraysubs_effective_recurring_amount = (float) ($arraysubs_subscription_data['effective_recurring_amount'] ?? $arraysubs_subscription_data['recurring_amount'] ?? 0);
$arraysubs_base_recurring_amount = (float) ($arraysubs_subscription_data['base_recurring_amount'] ?? $arraysubs_effective_recurring_amount);
$arraysubs_active_retention_discount = $arraysubs_subscription_data['active_retention_discount'] ?? null;

$arraysubs_is_waiting_cancellation = !empty($arraysubs_subscription_data['is_waiting_cancellation']);
$arraysubs_scheduled_cancel_date = $arraysubs_subscription_data['scheduled_cancel_date'] ?? '';
$arraysubs_scheduled_cancel_date_display = !empty($arraysubs_scheduled_cancel_date)
    ? arraysubs_format_date_local($arraysubs_scheduled_cancel_date)
    : '';
$arraysubs_can_undo_cancellation = !empty($arraysubs_subscription_data['can_undo_cancellation']);

?>

<div class="arraysubs-view-subscription">
    <div class="subscription-details">

        <?php if ($arraysubs_is_waiting_cancellation) : ?>
            <div class="arraysubs-pending-cancel-banner" role="status">
                <strong>
                    <?php
                    if (!empty($arraysubs_scheduled_cancel_date_display)) {
                        printf(
                            /* translators: %s: scheduled cancellation date */
                            esc_html__('This subscription is scheduled to cancel on %s.', 'arraysubs'),
                            esc_html($arraysubs_scheduled_cancel_date_display)
                        );
                    } else {
                        esc_html_e('This subscription is scheduled to cancel at the end of the current billing period.', 'arraysubs');
                    }
                    ?>
                </strong>
                <span><?php esc_html_e('You will retain access until that date and no further payments will be charged.', 'arraysubs'); ?></span>
            </div>
        <?php endif; ?>

        <?php
        // Pending plan-switch banner (deferred APPLY_AT_RENEWAL). Shows
        // the customer that their next renewal will charge the new plan
        // price plus any switch fee, and offers an Undo button that hits
        // the cancel-pending-switch REST endpoint.
        $arraysubs_pending_switch = function_exists('arraysubs_get_pending_switch_details')
            ? arraysubs_get_pending_switch_details($arraysubs_subscription_id)
            : null;
        if ($arraysubs_pending_switch) :
            $arraysubs_pending_target_name = $arraysubs_pending_switch['target_product_name'] ?? __('the new plan', 'arraysubs');
            $arraysubs_pending_effective_display = $arraysubs_pending_switch['effective_renewal_date_display'] ?? __('the next renewal', 'arraysubs');
        ?>
            <div class="arraysubs-pending-switch-banner" role="status">
                <strong>
                    <?php
                    printf(
                        /* translators: 1: target plan name, 2: effective date */
                        esc_html__('Plan switch to %1$s scheduled for %2$s.', 'arraysubs'),
                        esc_html($arraysubs_pending_target_name),
                        esc_html($arraysubs_pending_effective_display)
                    );
                    ?>
                </strong>
                <span>
                    <?php
                    echo wp_kses_post(
                        sprintf(
                            /* translators: 1: quantity, 2: line total, 3: switch fee */
                            __('Quantity: %1$d. Future renewal line total: %2$s. Switch fee: %3$s. Until then your current plan continues unchanged.', 'arraysubs'),
                            absint($arraysubs_pending_switch['quantity'] ?? 1),
                            wp_kses_post($arraysubs_pending_switch['formatted_line_total'] ?? wc_price(0)),
                            wp_kses_post($arraysubs_pending_switch['formatted_switch_fee'] ?? wc_price(0))
                        )
                    );
                    ?>
                </span>
                <button type="button"
                    class="button arraysubs-cancel-pending-switch"
                    data-subscription-id="<?php echo esc_attr($arraysubs_subscription_id); ?>"
                    data-cancel-url="<?php echo esc_url(rest_url('arraysubs/v1/subscriptions/' . $arraysubs_subscription_id . '/pending-switch/cancel')); ?>">
                    <?php esc_html_e('Undo Pending Switch', 'arraysubs'); ?>
                </button>
            </div>
        <?php endif; ?>

        <!-- Overview Section -->
        <div class="subscription-overview">
            <h2>
                <?php
                printf(
                    /* translators: %d: subscription detail ID */
                    esc_html__('Subscription #%d', 'arraysubs'),
                    intval($arraysubs_subscription_id)
                );
                ?>
            </h2>

            <table class="woocommerce-table woocommerce-table--order-details shop_table order_details">
                <tbody>
                    <tr>
                        <th><?php esc_html_e('Status:', 'arraysubs'); ?></th>
                        <td>
                            <span class="subscription-status-badge subscription-status-<?php echo esc_attr(str_replace('arraysubs-', '', $arraysubs_status)); ?>">
                                <?php echo esc_html($arraysubs_status_name); ?>
                            </span>
                            <?php if ($arraysubs_is_waiting_cancellation) : ?>
                                <span class="subscription-status-badge subscription-status-pending-cancel">
                                    <?php esc_html_e('Pending cancellation', 'arraysubs'); ?>
                                </span>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <tr>
                        <th><?php esc_html_e('Product:', 'arraysubs'); ?></th>
                        <td>
                            <?php if ($arraysubs_product_name && !empty($arraysubs_product_permalink)) : ?>
                                <a href="<?php echo esc_url($arraysubs_product_permalink); ?>">
                                    <?php echo esc_html($arraysubs_product_name); ?>
                                </a>
                            <?php else : ?>
                                <?php echo $arraysubs_product_name ? esc_html($arraysubs_product_name) : '—'; ?>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <tr>
                        <th><?php esc_html_e('Start Date:', 'arraysubs'); ?></th>
                        <td><?php echo !empty($arraysubs_subscription_data['start_date']) ? esc_html(arraysubs_format_date_local($arraysubs_subscription_data['start_date'])) : '—'; ?></td>
                    </tr>
                    <?php
                    $arraysubs_billing_period_value = $arraysubs_subscription_data['billing_period'] ?? '';
                    if ($arraysubs_is_waiting_cancellation) :
                    ?>
                        <tr>
                            <th><?php esc_html_e('Cancels On:', 'arraysubs'); ?></th>
                            <td>
                                <?php
                                if (!empty($arraysubs_scheduled_cancel_date_display)) {
                                    echo esc_html($arraysubs_scheduled_cancel_date_display);
                                } else {
                                    esc_html_e('End of current billing period', 'arraysubs');
                                }
                                ?>
                            </td>
                        </tr>
                    <?php elseif ($arraysubs_billing_period_value === 'lifetime') : ?>
                        <tr>
                            <th><?php esc_html_e('Next Payment:', 'arraysubs'); ?></th>
                            <td><?php esc_html_e('Lifetime Deal — No recurring payment', 'arraysubs'); ?></td>
                        </tr>
                    <?php elseif (!empty($arraysubs_subscription_data['next_payment_date']) && in_array($arraysubs_status, ['arraysubs-active', 'arraysubs-on-hold', 'arraysubs-trial'], true)) : ?>
                        <tr>
                            <th><?php esc_html_e('Next Payment:', 'arraysubs'); ?></th>
                            <td><?php echo esc_html(arraysubs_format_date_local($arraysubs_subscription_data['next_payment_date'])); ?></td>
                        </tr>
                    <?php endif; ?>
                    <?php if (!empty($arraysubs_subscription_data['end_date']) && $arraysubs_subscription_data['end_date'] !== '0000-00-00 00:00:00') : ?>
                        <tr>
                            <th><?php esc_html_e('End Date:', 'arraysubs'); ?></th>
                            <td><?php echo esc_html(arraysubs_format_date_local($arraysubs_subscription_data['end_date'])); ?></td>
                        </tr>
                    <?php endif; ?>
                    <tr>
                        <th><?php esc_html_e('Recurring Amount:', 'arraysubs'); ?></th>
                        <td>
                            <?php echo wp_kses_post(wc_price($arraysubs_effective_recurring_amount)); ?>
                            <i><?php echo esc_html(arraysubs_get_billing_schedule_string((int) ($arraysubs_subscription_data['billing_interval'] ?? 1), $arraysubs_subscription_data['billing_period'] ?? 'month')); ?></i>
                            <?php if ($arraysubs_effective_recurring_amount < $arraysubs_base_recurring_amount && is_array($arraysubs_active_retention_discount)) : ?>
                                <br>
                                <small>
                                    <?php
                                    printf(
                                        /* translators: 1: original amount, 2: number of renewals remaining */
                                        esc_html__('Discounted from %1$s for the next %2$d renewal(s).', 'arraysubs'),
                                        esc_html(wp_strip_all_tags(wc_price($arraysubs_base_recurring_amount))),
                                        absint($arraysubs_active_retention_discount['remaining'] ?? 0)
                                    );
                                    ?>
                                </small>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php
                    // Coupon discount row - show active recurring coupon info.
                    $arraysubs_coupon_info = class_exists('\ArraySubs\Features\CouponTracking\Services\Hooks')
                        ? \ArraySubs\Features\CouponTracking\Services\Hooks::getCouponInfo($arraysubs_subscription_id)
                        : null;
                    ?>
                    <?php if ($arraysubs_coupon_info && 'recurring' === $arraysubs_coupon_info['discount_type']) : ?>
                        <tr>
                            <th><?php esc_html_e('Coupon Discount:', 'arraysubs'); ?></th>
                            <td>
                                <?php
                                $arraysubs_coupon_is_percent = $arraysubs_coupon_info['discount_percent'] > 0;
                                $arraysubs_coupon_is_unlimited = 0 === $arraysubs_coupon_info['original_cycles'];
                                $arraysubs_coupon_is_active = $arraysubs_coupon_is_unlimited || $arraysubs_coupon_info['remaining_cycles'] > 0;
                                ?>
                                <?php if ($arraysubs_coupon_is_active) : ?>
                                    <strong>
                                        <?php
                                        if ($arraysubs_coupon_is_percent) {
                                            printf(
                                                /* translators: 1: percentage amount, 2: coupon code */
                                                esc_html__('%1$s%% off (%2$s)', 'arraysubs'),
                                                esc_html($arraysubs_coupon_info['discount_percent']),
                                                esc_html($arraysubs_coupon_info['code'])
                                            );
                                        } else {
                                            printf(
                                                /* translators: 1: formatted currency amount, 2: coupon code */
                                                esc_html__('%1$s off (%2$s)', 'arraysubs'),
                                                wp_kses_post(wc_price($arraysubs_coupon_info['discount_amount'])),
                                                esc_html($arraysubs_coupon_info['code'])
                                            );
                                        }
                                        ?>
                                    </strong>
                                    <?php if (! $arraysubs_coupon_is_unlimited) : ?>
                                        <br>
                                        <small>
                                            <?php
                                            printf(
                                                /* translators: %d: number of renewal cycles remaining */
                                                esc_html__('%d renewal cycle(s) remaining', 'arraysubs'),
                                                absint($arraysubs_coupon_info['remaining_cycles'])
                                            );
                                            ?>
                                        </small>
                                    <?php else : ?>
                                        <br>
                                        <small><?php esc_html_e('Applied to all future renewals', 'arraysubs'); ?></small>
                                    <?php endif; ?>
                                <?php else : ?>
                                    <span class="arraysubs-coupon-exhausted">
                                        <?php
                                        printf(
                                            /* translators: %s: coupon code */
                                            esc_html__('Coupon "%s" — all discount cycles used', 'arraysubs'),
                                            esc_html($arraysubs_coupon_info['code'])
                                        );
                                        ?>
                                    </span>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endif; ?>
                    <?php
                    // Payment Method row - show current payment method with link to update
                    $arraysubs_payment_method_title = $arraysubs_subscription_data['payment_method_title'] ?? '';
                    $arraysubs_show_payment_update = in_array($arraysubs_status, ['arraysubs-active', 'arraysubs-on-hold', 'arraysubs-trial'], true);
                    ?>
                    <?php if (!empty($arraysubs_payment_method_title) || $arraysubs_show_payment_update) : ?>
                        <tr>
                            <th><?php esc_html_e('Payment Method:', 'arraysubs'); ?></th>
                            <td>
                                <?php if (!empty($arraysubs_payment_method_title)) : ?>
                                    <?php echo esc_html($arraysubs_payment_method_title); ?>
                                <?php else : ?>
                                    <em><?php esc_html_e('Not set', 'arraysubs'); ?></em>
                                <?php endif; ?>
                                <?php if ($arraysubs_show_payment_update && function_exists('wc_get_endpoint_url')) : ?>
                                    <a href="<?php echo esc_url(wc_get_endpoint_url('payment-methods', '', wc_get_page_permalink('myaccount'))); ?>" class="subscription-payment-update-link">
                                        <?php esc_html_e('Manage payment methods', 'arraysubs'); ?>
                                    </a>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endif; ?>
                    <?php
                    /**
                     * Hook: arraysubs_view_subscription_after_payment_method
                     *
                     * Allows addons (e.g. pro) to render additional payment
                     * method details such as gateway status, card info, or
                     * update payment method links.
                     *
                     * @param int   $arraysubs_subscription_id The subscription post ID.
                     * @param array $arraysubs_subscription_data The subscription data.
                     */
                    do_action('arraysubs_view_subscription_after_payment_method', $arraysubs_subscription_id, $arraysubs_subscription_data);
                    ?>
                </tbody>
            </table>
        </div>

        <!-- Actions -->
        <?php
        // Check if any actions are available
        $arraysubs_can_cancel = function_exists('arraysubs_can_customer_cancel_by_subs_id')
            && arraysubs_can_customer_cancel_by_subs_id($arraysubs_subscription_id);
        $arraysubs_show_actions_section = $arraysubs_can_cancel
            || in_array($arraysubs_status, ['arraysubs-active', 'arraysubs-on-hold'], true);
        $arraysubs_can_switch_plan = $arraysubs_status === 'arraysubs-active'
            && !$arraysubs_is_waiting_cancellation
            && function_exists('arraysubs_is_customer_switch_allowed')
            && arraysubs_is_customer_switch_allowed();

        // Reactivation: surface for cancelled/expired subscriptions when customer actions allow it
        $arraysubs_can_reactivate = function_exists('arraysubs_can_customer_reactivate_by_subs_id')
            && arraysubs_can_customer_reactivate_by_subs_id($arraysubs_subscription_id);

        // Retry payment: surface only when there's a recorded payment failure
        // and the subscription is still in a state that can be charged.
        $arraysubs_last_payment_failure = (string) get_post_meta($arraysubs_subscription_id, '_last_payment_failure', true);
        $arraysubs_can_retry_payment = '' !== $arraysubs_last_payment_failure
            && in_array($arraysubs_status, ['arraysubs-active', 'arraysubs-on-hold'], true)
            && ! $arraysubs_is_waiting_cancellation;

        $arraysubs_has_core_actions = (
            $arraysubs_show_actions_section
            && ($arraysubs_can_cancel || $arraysubs_can_switch_plan || $arraysubs_can_undo_cancellation || $arraysubs_can_retry_payment)
        ) || $arraysubs_can_reactivate;

        /**
         * Filter: arraysubs_view_subscription_force_show_actions
         *
         * Allows addons (e.g. pro) to force the Subscription Actions section
         * to render even when no core action button is available, so they can
         * append their own controls via the action hooks below.
         *
         * @param bool  $force_show                Whether to force-render the actions section.
         * @param int   $arraysubs_subscription_id The subscription post ID.
         * @param array $arraysubs_subscription_data The subscription data array.
         */
        $arraysubs_force_show_actions = (bool) apply_filters(
            'arraysubs_view_subscription_force_show_actions',
            false,
            $arraysubs_subscription_id,
            $arraysubs_subscription_data
        );

        // Only show section if at least one action is available
        if ($arraysubs_has_core_actions || $arraysubs_force_show_actions) :
        ?>
            <div class="subscription-actions">
                <h3><?php esc_html_e('Subscription Actions', 'arraysubs'); ?></h3>

                <div class="subscription-actions__buttons">
                    <?php
                    // Plan Switch Button - only for active subscriptions when customer switching is allowed
                    if ($arraysubs_can_switch_plan) :
                    ?>
                        <button type="button"
                            class="button arraysubs-switch-plan"
                            data-subscription-id="<?php echo esc_attr($arraysubs_subscription_id); ?>"
                            data-switch-options-url="<?php echo esc_url(rest_url('arraysubs/v1/subscriptions/' . $arraysubs_subscription_id . '/switch-options')); ?>">
                            <?php esc_html_e('Change Plan', 'arraysubs'); ?>
                        </button>
                    <?php endif; ?>

                    <?php
                    // Cancel Button - only show if setting allows customer cancellation
                    if ($arraysubs_can_cancel) :
                    ?>
                        <button type="button"
                            class="button arraysubs-cancel-subscription"
                            data-subscription-id="<?php echo esc_attr($arraysubs_subscription_id); ?>"
                            data-cancel-url="<?php echo esc_url(rest_url('arraysubs/v1/my-subscriptions/' . $arraysubs_subscription_id . '/cancel')); ?>">
                            <?php esc_html_e('Cancel Subscription', 'arraysubs'); ?>
                        </button>
                    <?php endif; ?>

                    <?php
                    // Undo Cancellation Button - shown when subscription is scheduled to cancel
                    if ($arraysubs_can_undo_cancellation) :
                    ?>
                        <button type="button"
                            class="button button-primary arraysubs-undo-cancellation"
                            data-subscription-id="<?php echo esc_attr($arraysubs_subscription_id); ?>"
                            data-undo-url="<?php echo esc_url(rest_url('arraysubs/v1/my-subscriptions/' . $arraysubs_subscription_id . '/undo-cancellation')); ?>">
                            <?php esc_html_e('Undo Scheduled Cancellation', 'arraysubs'); ?>
                        </button>
                    <?php endif; ?>

                    <?php
                    // Reactivate Button - only show on cancelled/expired subscriptions when allowed
                    if ($arraysubs_can_reactivate) :
                    ?>
                        <button type="button"
                            class="button button-primary arraysubs-reactivate-subscription"
                            data-subscription-id="<?php echo esc_attr($arraysubs_subscription_id); ?>"
                            data-reactivate-url="<?php echo esc_url(rest_url('arraysubs/v1/my-subscriptions/' . $arraysubs_subscription_id . '/reactivate')); ?>">
                            <?php esc_html_e('Reactivate Subscription', 'arraysubs'); ?>
                        </button>
                    <?php endif; ?>

                    <?php
                    // Retry Payment Button - shown when the latest renewal charge failed
                    if ($arraysubs_can_retry_payment) :
                    ?>
                        <button type="button"
                            class="button button-primary arraysubs-retry-payment"
                            data-subscription-id="<?php echo esc_attr($arraysubs_subscription_id); ?>"
                            data-retry-url="<?php echo esc_url(rest_url('arraysubs/v1/my-subscriptions/' . $arraysubs_subscription_id . '/retry-payment')); ?>">
                            <?php esc_html_e('Retry Payment', 'arraysubs'); ?>
                        </button>
                    <?php endif; ?>
                </div>

                <?php
                /**
                 * Hook: arraysubs_view_subscription_actions
                 *
                 * Fires inside the Subscription Actions section after the core
                 * action buttons. Allows addons (e.g. pro) to append their own
                 * action controls such as the auto-renew toggle.
                 *
                 * @param int   $arraysubs_subscription_id The subscription post ID.
                 * @param array $arraysubs_subscription_data The subscription data array.
                 */
                do_action(
                    'arraysubs_view_subscription_actions',
                    $arraysubs_subscription_id,
                    $arraysubs_subscription_data
                );
                ?>
            </div>
        <?php endif; ?>

        <?php
        // Skip & Pause Section - only for active/trial subscriptions
        $arraysubs_skip_enabled = function_exists('arraysubs_get_setting')
            && arraysubs_get_setting('skip_renewal.enabled', false)
            && arraysubs_get_setting('skip_renewal.customer_can_skip', false);
        $arraysubs_pause_enabled = function_exists('arraysubs_get_setting')
            && arraysubs_get_setting('pause_subscription.enabled', false)
            && arraysubs_get_setting('pause_subscription.customer_can_pause', false);
        $arraysubs_can_skip_or_pause = in_array($arraysubs_status, ['arraysubs-active', 'arraysubs-trial'], true);
        $arraysubs_is_paused = ($arraysubs_status === 'arraysubs-on-hold');

        // Get current skip/pause status
        $arraysubs_skip_manager = null;
        $arraysubs_pause_manager = null;
        $arraysubs_skip_status = [];
        $arraysubs_pause_status = [];

        if ($arraysubs_skip_enabled && class_exists('\ArraySubs\Features\SkipRenewal\Services\SkipManager')) {
            $arraysubs_skip_manager = new \ArraySubs\Features\SkipRenewal\Services\SkipManager();
            $arraysubs_skip_status = $arraysubs_skip_manager->getSkipStatus($arraysubs_subscription_id);
        }

        if ($arraysubs_pause_enabled && class_exists('\ArraySubs\Features\SkipRenewal\Services\PauseManager')) {
            $arraysubs_pause_manager = new \ArraySubs\Features\SkipRenewal\Services\PauseManager();
            $arraysubs_pause_status = $arraysubs_pause_manager->getPauseStatus($arraysubs_subscription_id);
        }

        if (($arraysubs_skip_enabled && $arraysubs_can_skip_or_pause) || ($arraysubs_pause_enabled && ($arraysubs_can_skip_or_pause || $arraysubs_is_paused))) :
        ?>
            <div class="subscription-skip-pause">
                <h3><?php esc_html_e('Manage Your Subscription', 'arraysubs'); ?></h3>

                <?php if ($arraysubs_skip_enabled && $arraysubs_can_skip_or_pause) : ?>
                    <div class="subscription-skip-pause__section">
                        <h4><?php esc_html_e('Skip Next Renewal', 'arraysubs'); ?></h4>

                        <?php if (!empty($arraysubs_skip_status['is_skipping'])) : ?>
                            <div class="subscription-skip-pause__status subscription-skip-pause__status--warning">
                                <span class="subscription-skip-pause__icon">⏭</span>
                                <div class="subscription-skip-pause__info">
                                    <strong>
                                        <?php
                                        printf(
                                            /* translators: %d: number of cycles remaining */
                                            esc_html__('Skipping %d cycle(s)', 'arraysubs'),
                                            (int) $arraysubs_skip_status['cycles_remaining']
                                        );
                                        ?>
                                    </strong>
                                    <?php if (!empty($arraysubs_skip_status['requested_at'])) : ?>
                                        <p>
                                            <?php
                                            printf(
                                                /* translators: %s: date when skip was initiated */
                                                esc_html__('Skipped on: %s', 'arraysubs'),
                                                esc_html(arraysubs_format_date_local($arraysubs_skip_status['requested_at']))
                                            );
                                            ?>
                                        </p>
                                    <?php endif; ?>
                                </div>
                                <button type="button"
                                    class="button arraysubs-undo-skip"
                                    data-subscription-id="<?php echo esc_attr($arraysubs_subscription_id); ?>"
                                    data-undo-url="<?php echo esc_url(rest_url('arraysubs/v1/my-subscriptions/' . $arraysubs_subscription_id . '/undo-skip')); ?>">
                                    <?php esc_html_e('Undo Skip', 'arraysubs'); ?>
                                </button>
                            </div>
                        <?php else : ?>
                            <?php
                            $arraysubs_can_skip_result = $arraysubs_skip_manager ? $arraysubs_skip_manager->canSkip($arraysubs_subscription_id) : ['can_skip' => false];
                            if ($arraysubs_can_skip_result['can_skip']) :
                            ?>
                                <p class="subscription-skip-pause__description">
                                    <?php esc_html_e('Need a break? Skip your next billing cycle without cancelling your subscription.', 'arraysubs'); ?>
                                </p>
                                <button type="button"
                                    class="button arraysubs-skip-renewal"
                                    data-subscription-id="<?php echo esc_attr($arraysubs_subscription_id); ?>"
                                    data-skip-url="<?php echo esc_url(rest_url('arraysubs/v1/my-subscriptions/' . $arraysubs_subscription_id . '/skip')); ?>"
                                    data-max-cycles="<?php echo esc_attr($arraysubs_can_skip_result['max_cycles']); ?>">
                                    <?php esc_html_e('Skip Next Renewal', 'arraysubs'); ?>
                                </button>
                            <?php elseif (!empty($arraysubs_can_skip_result['reason'])) : ?>
                                <p class="subscription-skip-pause__notice">
                                    <?php echo esc_html($arraysubs_can_skip_result['reason']); ?>
                                </p>
                            <?php endif; ?>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>

                <?php if ($arraysubs_pause_enabled && ($arraysubs_can_skip_or_pause || $arraysubs_is_paused)) : ?>
                    <div class="subscription-skip-pause__section">
                        <h4><?php esc_html_e('Vacation Mode', 'arraysubs'); ?></h4>

                        <?php if ($arraysubs_is_paused && !empty($arraysubs_pause_status['is_paused'])) : ?>
                            <div class="subscription-skip-pause__status subscription-skip-pause__status--paused">
                                <span class="subscription-skip-pause__icon">⏸</span>
                                <div class="subscription-skip-pause__info">
                                    <strong><?php esc_html_e('Subscription Paused', 'arraysubs'); ?></strong>
                                    <?php if (!empty($arraysubs_pause_status['start_date'])) : ?>
                                        <p>
                                            <?php
                                            printf(
                                                /* translators: %s: date when pause started */
                                                esc_html__('Paused on: %s', 'arraysubs'),
                                                esc_html(arraysubs_format_date_local($arraysubs_pause_status['start_date']))
                                            );
                                            ?>
                                        </p>
                                    <?php endif; ?>
                                    <?php if (!empty($arraysubs_pause_status['end_date'])) : ?>
                                        <p>
                                            <?php
                                            printf(
                                                /* translators: %s: scheduled resume date */
                                                esc_html__('Scheduled resume: %s', 'arraysubs'),
                                                esc_html(arraysubs_format_date_local($arraysubs_pause_status['end_date']))
                                            );
                                            ?>
                                        </p>
                                    <?php endif; ?>
                                </div>
                                <button type="button"
                                    class="button button-primary arraysubs-resume-subscription"
                                    data-subscription-id="<?php echo esc_attr($arraysubs_subscription_id); ?>"
                                    data-resume-url="<?php echo esc_url(rest_url('arraysubs/v1/my-subscriptions/' . $arraysubs_subscription_id . '/resume')); ?>">
                                    <?php esc_html_e('Resume Now', 'arraysubs'); ?>
                                </button>
                            </div>
                        <?php elseif ($arraysubs_can_skip_or_pause && !empty($arraysubs_skip_status['is_skipping'])) : ?>
                            <p class="subscription-skip-pause__notice">
                                <?php esc_html_e('Pause is unavailable while a renewal skip is active.', 'arraysubs'); ?>
                            </p>
                        <?php elseif ($arraysubs_can_skip_or_pause) : ?>
                            <?php
                            $arraysubs_can_pause_result = $arraysubs_pause_manager ? $arraysubs_pause_manager->canPause($arraysubs_subscription_id) : ['can_pause' => false];
                            if ($arraysubs_can_pause_result['can_pause']) :
                            ?>
                                <p class="subscription-skip-pause__description">
                                    <?php esc_html_e('Going on vacation? Pause your subscription and resume when you\'re back.', 'arraysubs'); ?>
                                </p>
                                <button type="button"
                                    class="button arraysubs-pause-subscription"
                                    data-subscription-id="<?php echo esc_attr($arraysubs_subscription_id); ?>"
                                    data-pause-url="<?php echo esc_url(rest_url('arraysubs/v1/my-subscriptions/' . $arraysubs_subscription_id . '/pause')); ?>"
                                    data-max-days="<?php echo esc_attr($arraysubs_can_pause_result['max_duration_days']); ?>"
                                    data-require-reason="<?php echo esc_attr(arraysubs_get_setting('pause_subscription.require_reason', false) ? 'true' : 'false'); ?>">
                                    <?php esc_html_e('Pause Subscription', 'arraysubs'); ?>
                                </button>
                            <?php elseif (!empty($arraysubs_can_pause_result['reason'])) : ?>
                                <p class="subscription-skip-pause__notice">
                                    <?php echo esc_html($arraysubs_can_pause_result['reason']); ?>
                                </p>
                            <?php endif; ?>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>
            </div>
        <?php endif; ?>

        <?php
        // Shipping Address Section
        $arraysubs_needs_shipping = get_post_meta($arraysubs_subscription_id, '_arraysubs_needs_shipping', true) === 'yes';
        $arraysubs_shipping_address_json = get_post_meta($arraysubs_subscription_id, '_shipping_address', true);
        $arraysubs_shipping_address = !empty($arraysubs_shipping_address_json) ? json_decode($arraysubs_shipping_address_json, true) : [];

        if ($arraysubs_needs_shipping) :
            // Get shipping type
            $arraysubs_shipping_type = get_post_meta($arraysubs_subscription_id, '_arraysubs_shipping_type', true) ?: 'recurring';

            // Check if customer can update (cutoff period)
            $arraysubs_can_update_address = true;
            $arraysubs_cutoff_message = '';
            $arraysubs_next_payment_date = $arraysubs_subscription_data['next_payment_date'] ?? '';

            if (!empty($arraysubs_next_payment_date)) {
                $arraysubs_cutoff_days = apply_filters('arraysubs_shipping_address_cutoff_days', 3);
                $arraysubs_cutoff_time = strtotime($arraysubs_next_payment_date) - ($arraysubs_cutoff_days * DAY_IN_SECONDS);

                if (time() >= $arraysubs_cutoff_time) {
                    $arraysubs_can_update_address = false;
                    $arraysubs_cutoff_message = sprintf(
                        /* translators: %d: number of days */
                        __('Shipping address cannot be changed within %d days of the next payment.', 'arraysubs'),
                        $arraysubs_cutoff_days
                    );
                }
            }

            // Only allow update for active/on-hold/trial subscriptions
            $arraysubs_updateable_statuses = ['arraysubs-active', 'arraysubs-on-hold', 'arraysubs-trial'];
            $arraysubs_can_update_address = $arraysubs_can_update_address && in_array($arraysubs_status, $arraysubs_updateable_statuses, true);
        ?>
            <div class="subscription-shipping">
                <h3><?php esc_html_e('Shipping Address', 'arraysubs'); ?></h3>

                <?php if ($arraysubs_shipping_type === 'recurring') : ?>
                    <p class="subscription-shipping__note">
                        <small><?php esc_html_e('Shipping is charged on each renewal order.', 'arraysubs'); ?></small>
                    </p>
                <?php else : ?>
                    <p class="subscription-shipping__note">
                        <small><?php esc_html_e('Shipping was charged on the initial order only.', 'arraysubs'); ?></small>
                    </p>
                <?php endif; ?>

                <div class="subscription-shipping__address">
                    <?php if (!empty($arraysubs_shipping_address) && is_array($arraysubs_shipping_address)) : ?>
                        <address>
                            <?php
                            $arraysubs_address_parts = [];
                            if (!empty($arraysubs_shipping_address['first_name']) || !empty($arraysubs_shipping_address['last_name'])) {
                                $arraysubs_address_parts[] = esc_html(trim($arraysubs_shipping_address['first_name'] . ' ' . $arraysubs_shipping_address['last_name']));
                            }
                            if (!empty($arraysubs_shipping_address['company'])) {
                                $arraysubs_address_parts[] = esc_html($arraysubs_shipping_address['company']);
                            }
                            if (!empty($arraysubs_shipping_address['address_1'])) {
                                $arraysubs_address_parts[] = esc_html($arraysubs_shipping_address['address_1']);
                            }
                            if (!empty($arraysubs_shipping_address['address_2'])) {
                                $arraysubs_address_parts[] = esc_html($arraysubs_shipping_address['address_2']);
                            }
                            $arraysubs_city_state_zip = [];
                            if (!empty($arraysubs_shipping_address['city'])) {
                                $arraysubs_city_state_zip[] = $arraysubs_shipping_address['city'];
                            }
                            if (!empty($arraysubs_shipping_address['state'])) {
                                $arraysubs_city_state_zip[] = $arraysubs_shipping_address['state'];
                            }
                            if (!empty($arraysubs_shipping_address['postcode'])) {
                                $arraysubs_city_state_zip[] = $arraysubs_shipping_address['postcode'];
                            }
                            if (!empty($arraysubs_city_state_zip)) {
                                $arraysubs_address_parts[] = esc_html(implode(', ', $arraysubs_city_state_zip));
                            }
                            if (!empty($arraysubs_shipping_address['country'])) {
                                $arraysubs_address_parts[] = esc_html($arraysubs_shipping_address['country']);
                            }
                            echo wp_kses_post(implode('<br>', $arraysubs_address_parts));
                            ?>
                        </address>
                    <?php else : ?>
                        <p><em><?php esc_html_e('No shipping address on file.', 'arraysubs'); ?></em></p>
                    <?php endif; ?>
                </div>

                <?php if ($arraysubs_can_update_address) : ?>
                    <div class="subscription-shipping__actions">
                        <button type="button"
                            class="button arraysubs-edit-shipping-address"
                            data-subscription-id="<?php echo esc_attr($arraysubs_subscription_id); ?>"
                            data-update-url="<?php echo esc_url(rest_url('arraysubs/v1/my-subscriptions/' . $arraysubs_subscription_id . '/shipping-address')); ?>">
                            <?php esc_html_e('Update Shipping Address', 'arraysubs'); ?>
                        </button>
                    </div>
                <?php elseif (!empty($arraysubs_cutoff_message)) : ?>
                    <p class="subscription-shipping__cutoff-notice">
                        <small><?php echo esc_html($arraysubs_cutoff_message); ?></small>
                    </p>
                <?php endif; ?>
            </div>
        <?php endif; ?>

        <!-- Related Orders -->
        <?php if (!empty($arraysubs_order_ids)) : ?>
            <div class="subscription-orders">
                <h3><?php esc_html_e('Related Orders', 'arraysubs'); ?></h3>

                <table class="woocommerce-table woocommerce-table--order-details shop_table order_details">
                    <thead>
                        <tr>
                            <th><?php esc_html_e('Order', 'arraysubs'); ?></th>
                            <th><?php esc_html_e('Date', 'arraysubs'); ?></th>
                            <th><?php esc_html_e('Status', 'arraysubs'); ?></th>
                            <th><?php esc_html_e('Total', 'arraysubs'); ?></th>
                            <th><?php esc_html_e('Actions', 'arraysubs'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        foreach ($arraysubs_order_ids as $arraysubs_order_id) :
                            $arraysubs_order = wc_get_order($arraysubs_order_id);
                            if (!$arraysubs_order) {
                                continue;
                            }

                            // Get invoice URL - check for popular PDF invoice plugins
                            $arraysubs_invoice_url = null;
                            $arraysubs_invoice_label = __('Invoice', 'arraysubs');

                            // WooCommerce PDF Invoices & Packing Slips
                            if (function_exists('wcpdf_get_invoice')) {
                                $arraysubs_invoice = wcpdf_get_invoice($arraysubs_order);
                                if ($arraysubs_invoice && $arraysubs_invoice->exists()) {
                                    $arraysubs_invoice_url = $arraysubs_invoice->get_endpoint_url('download');
                                }
                            } elseif (class_exists('WC_PDF_Invoices') && method_exists('WC_PDF_Invoices', 'get_invoice_url')) {
                                // WooCommerce PDF Invoices (alternative plugin)
                                $arraysubs_invoice_url = \WC_PDF_Invoices::get_invoice_url($arraysubs_order_id);
                            }

                        ?>
                            <tr>
                                <td>
                                    <a href="<?php echo esc_url($arraysubs_order->get_view_order_url()); ?>">
                                        #<?php echo esc_html($arraysubs_order->get_order_number()); ?>
                                    </a>
                                </td>
                                <td><?php echo esc_html($arraysubs_order->get_date_created()->date_i18n(wc_date_format())); ?></td>
                                <td><?php echo esc_html(wc_get_order_status_name($arraysubs_order->get_status())); ?></td>
                                <td><?php echo wp_kses_post($arraysubs_order->get_formatted_order_total()); ?></td>
                                <td class="subscription-order-actions">
                                    <a href="<?php echo esc_url($arraysubs_order->get_view_order_url()); ?>" class="woocommerce-button button view">
                                        <?php esc_html_e('View', 'arraysubs'); ?>
                                    </a>
                                    <?php if ($arraysubs_invoice_url) : ?>
                                        <a href="<?php echo esc_url($arraysubs_invoice_url); ?>" class="woocommerce-button button alt invoice" target="_blank">
                                            <?php echo esc_html($arraysubs_invoice_label); ?>
                                        </a>
                                    <?php endif; ?>
                                    <?php
                                    // Pay button for genuinely unpaid orders only.
                                    if ($arraysubs_order->needs_payment() && in_array($arraysubs_order->get_status(), ['pending', 'failed'], true)) :
                                    ?>
                                        <a href="<?php echo esc_url($arraysubs_order->get_checkout_payment_url()); ?>" class="woocommerce-button button pay">
                                            <?php esc_html_e('Pay', 'arraysubs'); ?>
                                        </a>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>

        <!-- Refund History -->
        <?php
        // Get refund history for this subscription via meta
        $arraysubs_refund_history = get_post_meta($arraysubs_subscription_id, '_refund_history', true);
        $arraysubs_refund_history = is_array($arraysubs_refund_history) ? $arraysubs_refund_history : [];

        if (!empty($arraysubs_refund_history)) :
        ?>
            <div class="subscription-refunds">
                <h3><?php esc_html_e('Refund History', 'arraysubs'); ?></h3>

                <table class="woocommerce-table woocommerce-table--order-details shop_table order_details">
                    <thead>
                        <tr>
                            <th><?php esc_html_e('Date', 'arraysubs'); ?></th>
                            <th><?php esc_html_e('Order', 'arraysubs'); ?></th>
                            <th><?php esc_html_e('Amount', 'arraysubs'); ?></th>
                            <th><?php esc_html_e('Reason', 'arraysubs'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        foreach ($arraysubs_refund_history as $arraysubs_refund) :
                            $arraysubs_refund_date = isset($arraysubs_refund['date']) ? $arraysubs_refund['date'] : '';
                            $arraysubs_refund_order_id = isset($arraysubs_refund['order_id']) ? absint($arraysubs_refund['order_id']) : 0;
                            $arraysubs_refund_amount = isset($arraysubs_refund['amount']) ? floatval($arraysubs_refund['amount']) : 0;
                            $arraysubs_refund_reason = isset($arraysubs_refund['reason']) ? $arraysubs_refund['reason'] : '';
                            $arraysubs_refund_order = $arraysubs_refund_order_id ? wc_get_order($arraysubs_refund_order_id) : null;
                        ?>
                            <tr>
                                <td>
                                    <?php
                                    if ($arraysubs_refund_date) {
                                        echo esc_html(arraysubs_format_date_local($arraysubs_refund_date));
                                    } else {
                                        echo '—';
                                    }
                                    ?>
                                </td>
                                <td>
                                    <?php if ($arraysubs_refund_order) : ?>
                                        <a href="<?php echo esc_url($arraysubs_refund_order->get_view_order_url()); ?>">
                                            #<?php echo esc_html($arraysubs_refund_order->get_order_number()); ?>
                                        </a>
                                    <?php else : ?>
                                        <?php echo $arraysubs_refund_order_id ? '#' . esc_html($arraysubs_refund_order_id) : '—'; ?>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <?php echo wp_kses_post(wc_price($arraysubs_refund_amount)); ?>
                                </td>
                                <td>
                                    <?php echo $arraysubs_refund_reason ? esc_html($arraysubs_refund_reason) : '—'; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>

        <!-- Subscription Notes (Customer Visible) -->
        <?php
        $arraysubs_customer_notes = function_exists('arraysubs_get_customer_notes')
            ? arraysubs_get_customer_notes($arraysubs_subscription_id)
            : [];
        ?>
        <?php if (!empty($arraysubs_customer_notes)) : ?>
            <div class="subscription-notes">
                <h3><?php esc_html_e('Subscription Notes', 'arraysubs'); ?></h3>

                <ul class="subscription-notes-list">
                    <?php
                    foreach ($arraysubs_customer_notes as $arraysubs_note) :
                        // Format the note content (auto-link URLs)
                        $arraysubs_note_data = function_exists('arraysubs_format_note')
                            ? arraysubs_format_note($arraysubs_note)
                            : null;
                        $arraysubs_note_content = $arraysubs_note_data ? wp_kses_post($arraysubs_note_data['content']) : wp_kses_post($arraysubs_note->post_content);
                        $arraysubs_is_system = $arraysubs_note_data ? $arraysubs_note_data['is_system'] : (bool) get_post_meta($arraysubs_note->ID, '_is_system', true);
                        $arraysubs_note_author_role = $arraysubs_note_data['author_role'] ?? ($arraysubs_is_system ? 'system' : 'customer');
                        $arraysubs_note_author_label = $arraysubs_note_data['author_label'] ?? ($arraysubs_is_system ? __('System', 'arraysubs') : __('Customer', 'arraysubs'));
                        $arraysubs_note_author_name = $arraysubs_note_data['author_name'] ?? '';
                    ?>
                        <li class="subscription-note <?php echo esc_attr($arraysubs_is_system ? 'subscription-note--system' : ''); ?>">
                            <div class="subscription-note__content">
                                <?php echo wp_kses_post($arraysubs_note_content); ?>
                            </div>
                            <div class="subscription-note__meta">
                                <span class="subscription-note__author-badge subscription-note__author-badge--<?php echo esc_attr($arraysubs_note_author_role); ?>">
                                    <?php echo esc_html($arraysubs_note_author_label); ?>
                                </span>
                                <?php if (!empty($arraysubs_note_author_name) && $arraysubs_note_author_name !== $arraysubs_note_author_label) : ?>
                                    <span class="subscription-note__author-name"><?php echo esc_html($arraysubs_note_author_name); ?></span>
                                <?php endif; ?>
                                <span class="subscription-note__date">
                                    <?php
                                    echo esc_html(
                                        $arraysubs_note_data['date_display']
                                            ?? (!empty($arraysubs_note->post_date_gmt) && $arraysubs_note->post_date_gmt !== '0000-00-00 00:00:00'
                                                ? arraysubs_format_date_local($arraysubs_note->post_date_gmt)
                                                : arraysubs_format_site_local_date($arraysubs_note->post_date))
                                    );
                                    ?>
                                </span>
                            </div>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        <?php endif; ?>

        <?php
        // Include cancel modal (hidden by default)
        if (isset($arraysubs_can_cancel) && $arraysubs_can_cancel) :
            // Check if cancellation is immediate or end-of-period
            $arraysubs_cancel_immediately = function_exists('arraysubs_cancel_immediately')
                ? arraysubs_cancel_immediately()
                : true;

            // Get cancellation reasons from settings
            $arraysubs_cancellation_reasons = arraysubs_get_cancellation_reasons();
            $arraysubs_require_reason = function_exists('arraysubs_require_cancel_reason')
                ? arraysubs_require_cancel_reason()
                : false;
        ?>
            <!-- Cancel Subscription Modal -->
            <div id="arraysubs-cancel-modal" class="arraysubs-modal" style="display: none;" role="dialog" aria-modal="true" aria-labelledby="cancel-modal-title">
                <div class="arraysubs-modal__backdrop"></div>
                <div class="arraysubs-modal__container">
                    <div class="arraysubs-modal__content">
                        <button type="button" class="arraysubs-modal__close" aria-label="<?php esc_attr_e('Close', 'arraysubs'); ?>">
                            <span aria-hidden="true">&times;</span>
                        </button>

                        <div class="arraysubs-modal__header">
                            <h3 id="cancel-modal-title" class="arraysubs-modal__title">
                                <?php esc_html_e('Cancel Subscription', 'arraysubs'); ?>
                            </h3>
                        </div>

                        <div class="arraysubs-modal__body">
                            <p class="arraysubs-modal__message">
                                <?php if ($arraysubs_cancel_immediately) : ?>
                                    <?php esc_html_e('Are you sure you want to cancel this subscription? Your subscription will be cancelled immediately and you will lose access to your subscription benefits.', 'arraysubs'); ?>
                                <?php else : ?>
                                    <?php esc_html_e('Are you sure you want to cancel this subscription? Your subscription will remain active until the end of your current billing period, after which you will lose access to your subscription benefits.', 'arraysubs'); ?>
                                <?php endif; ?>
                            </p>

                            <div class="arraysubs-modal__form">
                                <label for="cancel-reason" class="arraysubs-modal__label">
                                    <?php
                                    if ($arraysubs_require_reason) {
                                        esc_html_e('Please tell us why you\'re cancelling:', 'arraysubs');
                                    } else {
                                        esc_html_e('Please tell us why you\'re cancelling (optional):', 'arraysubs');
                                    }
                                    ?>
                                </label>
                                <select id="cancel-reason" class="arraysubs-modal__select" <?php echo $arraysubs_require_reason ? 'required' : ''; ?>>
                                    <option value=""><?php esc_html_e('Select a reason...', 'arraysubs'); ?></option>
                                    <?php foreach ($arraysubs_cancellation_reasons as $arraysubs_key => $arraysubs_label) : ?>
                                        <option value="<?php echo esc_attr($arraysubs_key); ?>"><?php echo esc_html($arraysubs_label); ?></option>
                                    <?php endforeach; ?>
                                </select>

                                <div id="cancel-other-reason-wrapper" class="arraysubs-modal__other-reason" style="display: none;">
                                    <label for="cancel-other-reason" class="arraysubs-modal__label">
                                        <?php esc_html_e('Please provide more details:', 'arraysubs'); ?>
                                    </label>
                                    <textarea id="cancel-other-reason" class="arraysubs-modal__textarea" rows="3" placeholder="<?php esc_attr_e('Please provide more details...', 'arraysubs'); ?>"></textarea>
                                </div>
                            </div>
                        </div>

                        <div class="arraysubs-modal__footer">
                            <button type="button" class="button arraysubs-modal__cancel-btn">
                                <?php esc_html_e('Keep Subscription', 'arraysubs'); ?>
                            </button>
                            <button type="button" class="button arraysubs-modal__confirm-btn" <?php disabled($arraysubs_require_reason); ?>>
                                <?php esc_html_e('Continue', 'arraysubs'); ?>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        <?php endif; ?>

        <?php
        // Plan Switch Modal (hidden by default)
        if (isset($arraysubs_can_switch_plan) && $arraysubs_can_switch_plan) :
        ?>
            <!-- Plan Switch Modal -->
            <div id="arraysubs-switch-modal" class="arraysubs-modal" style="display: none;" role="dialog" aria-modal="true" aria-labelledby="switch-modal-title">
                <div class="arraysubs-modal__backdrop"></div>
                <div class="arraysubs-modal__container arraysubs-modal__container--wide">
                    <div class="arraysubs-modal__content">
                        <button type="button" class="arraysubs-modal__close" aria-label="<?php esc_attr_e('Close', 'arraysubs'); ?>">
                            <span aria-hidden="true">&times;</span>
                        </button>

                        <div class="arraysubs-modal__header">
                            <h3 id="switch-modal-title" class="arraysubs-modal__title">
                                <?php esc_html_e('Change Your Plan', 'arraysubs'); ?>
                            </h3>
                        </div>

                        <div class="arraysubs-modal__body">
                            <div class="arraysubs-switch-options" id="switch-options-container">
                                <p class="arraysubs-switch-loading"><?php esc_html_e('Loading available plans...', 'arraysubs'); ?></p>
                            </div>

                            <div class="arraysubs-switch-preview" id="switch-preview-container" style="display: none;">
                                <h4><?php esc_html_e('Plan Change Summary', 'arraysubs'); ?></h4>
                                <div class="arraysubs-switch-preview__details"></div>
                            </div>
                        </div>

                        <div class="arraysubs-modal__footer">
                            <button type="button" class="button arraysubs-modal__cancel-btn">
                                <?php esc_html_e('Cancel', 'arraysubs'); ?>
                            </button>
                            <button type="button" class="button button-primary arraysubs-modal__confirm-btn" id="confirm-switch-btn" style="display: none;">
                                <?php esc_html_e('Confirm Plan Change', 'arraysubs'); ?>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        <?php endif; ?>

        <?php
        // Retention Offers Modal (shown when customer clicks cancel if retention offers are enabled)
        $arraysubs_show_retention_offers = function_exists('arraysubs_is_retention_offers_enabled')
            && arraysubs_is_retention_offers_enabled()
            && isset($arraysubs_can_cancel)
            && $arraysubs_can_cancel;

        if ($arraysubs_show_retention_offers) :
        ?>
            <!-- Retention Offers Modal -->
            <div id="arraysubs-retention-modal" class="arraysubs-modal" style="display: none;" role="dialog" aria-modal="true" aria-labelledby="retention-modal-title">
                <div class="arraysubs-modal__backdrop"></div>
                <div class="arraysubs-modal__container arraysubs-modal__container--wide">
                    <div class="arraysubs-modal__content">
                        <button type="button" class="arraysubs-modal__close" aria-label="<?php esc_attr_e('Close', 'arraysubs'); ?>">
                            <span aria-hidden="true">&times;</span>
                        </button>

                        <div class="arraysubs-modal__header">
                            <h3 id="retention-modal-title" class="arraysubs-modal__title">
                                <?php esc_html_e('Before You Go...', 'arraysubs'); ?>
                            </h3>
                        </div>

                        <div class="arraysubs-modal__body">
                            <p class="arraysubs-retention__intro">
                                <?php esc_html_e("We'd hate to see you leave! Here are some options that might help:", 'arraysubs'); ?>
                            </p>

                            <div class="arraysubs-retention-offers" id="retention-offers-container">
                                <p class="arraysubs-retention-loading"><?php esc_html_e('Loading offers...', 'arraysubs'); ?></p>
                            </div>
                        </div>

                        <div class="arraysubs-modal__footer">
                            <button type="button" class="button button-primary arraysubs-retention__keep-btn">
                                <?php esc_html_e('Keep Subscription', 'arraysubs'); ?>
                            </button>
                            <button type="button" class="button arraysubs-retention__skip-btn">
                                <?php esc_html_e('No thanks, continue to cancel', 'arraysubs'); ?>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        <?php endif; ?>

        <?php if ($arraysubs_needs_shipping && $arraysubs_can_update_address) : ?>
            <!-- Shipping Address Modal -->
            <div id="arraysubs-shipping-modal" class="arraysubs-modal" style="display: none;" role="dialog" aria-modal="true" aria-labelledby="shipping-modal-title">
                <div class="arraysubs-modal__backdrop"></div>
                <div class="arraysubs-modal__container arraysubs-modal__container--wide">
                    <div class="arraysubs-modal__content">
                        <button type="button" class="arraysubs-modal__close" aria-label="<?php esc_attr_e('Close', 'arraysubs'); ?>">&times;</button>

                        <div class="arraysubs-modal__header">
                            <h3 class="arraysubs-modal__title" id="shipping-modal-title">
                                <?php esc_html_e('Update Shipping Address', 'arraysubs'); ?>
                            </h3>
                        </div>

                        <div class="arraysubs-modal__body">
                            <p class="arraysubs-modal__message">
                                <?php esc_html_e('Update the shipping address for future renewal orders.', 'arraysubs'); ?>
                            </p>

                            <form class="arraysubs-shipping-form" id="arraysubs-shipping-form">
                                <div class="arraysubs-shipping-form__row">
                                    <div class="arraysubs-shipping-form__field">
                                        <label class="arraysubs-modal__label" for="shipping_first_name">
                                            <?php esc_html_e('First Name', 'arraysubs'); ?> <span class="required">*</span>
                                        </label>
                                        <input type="text" id="shipping_first_name" name="first_name" class="arraysubs-modal__input" required
                                            value="<?php echo esc_attr($arraysubs_shipping_address['first_name'] ?? ''); ?>">
                                    </div>
                                    <div class="arraysubs-shipping-form__field">
                                        <label class="arraysubs-modal__label" for="shipping_last_name">
                                            <?php esc_html_e('Last Name', 'arraysubs'); ?> <span class="required">*</span>
                                        </label>
                                        <input type="text" id="shipping_last_name" name="last_name" class="arraysubs-modal__input" required
                                            value="<?php echo esc_attr($arraysubs_shipping_address['last_name'] ?? ''); ?>">
                                    </div>
                                </div>

                                <div class="arraysubs-shipping-form__row">
                                    <div class="arraysubs-shipping-form__field arraysubs-shipping-form__field--full">
                                        <label class="arraysubs-modal__label" for="shipping_company">
                                            <?php esc_html_e('Company', 'arraysubs'); ?>
                                        </label>
                                        <input type="text" id="shipping_company" name="company" class="arraysubs-modal__input"
                                            value="<?php echo esc_attr($arraysubs_shipping_address['company'] ?? ''); ?>">
                                    </div>
                                </div>

                                <div class="arraysubs-shipping-form__row">
                                    <div class="arraysubs-shipping-form__field arraysubs-shipping-form__field--full">
                                        <label class="arraysubs-modal__label" for="shipping_address_1">
                                            <?php esc_html_e('Street Address', 'arraysubs'); ?> <span class="required">*</span>
                                        </label>
                                        <input type="text" id="shipping_address_1" name="address_1" class="arraysubs-modal__input" required
                                            value="<?php echo esc_attr($arraysubs_shipping_address['address_1'] ?? ''); ?>">
                                    </div>
                                </div>

                                <div class="arraysubs-shipping-form__row">
                                    <div class="arraysubs-shipping-form__field arraysubs-shipping-form__field--full">
                                        <label class="arraysubs-modal__label" for="shipping_address_2">
                                            <?php esc_html_e('Apartment, suite, etc.', 'arraysubs'); ?>
                                        </label>
                                        <input type="text" id="shipping_address_2" name="address_2" class="arraysubs-modal__input"
                                            value="<?php echo esc_attr($arraysubs_shipping_address['address_2'] ?? ''); ?>">
                                    </div>
                                </div>

                                <div class="arraysubs-shipping-form__row">
                                    <div class="arraysubs-shipping-form__field">
                                        <label class="arraysubs-modal__label" for="shipping_city">
                                            <?php esc_html_e('City', 'arraysubs'); ?> <span class="required">*</span>
                                        </label>
                                        <input type="text" id="shipping_city" name="city" class="arraysubs-modal__input" required
                                            value="<?php echo esc_attr($arraysubs_shipping_address['city'] ?? ''); ?>">
                                    </div>
                                    <div class="arraysubs-shipping-form__field">
                                        <label class="arraysubs-modal__label" for="shipping_state">
                                            <?php esc_html_e('State / Province', 'arraysubs'); ?>
                                        </label>
                                        <input type="text" id="shipping_state" name="state" class="arraysubs-modal__input"
                                            value="<?php echo esc_attr($arraysubs_shipping_address['state'] ?? ''); ?>">
                                    </div>
                                </div>

                                <div class="arraysubs-shipping-form__row">
                                    <div class="arraysubs-shipping-form__field">
                                        <label class="arraysubs-modal__label" for="shipping_postcode">
                                            <?php esc_html_e('Postcode / ZIP', 'arraysubs'); ?> <span class="required">*</span>
                                        </label>
                                        <input type="text" id="shipping_postcode" name="postcode" class="arraysubs-modal__input" required
                                            value="<?php echo esc_attr($arraysubs_shipping_address['postcode'] ?? ''); ?>">
                                    </div>
                                    <div class="arraysubs-shipping-form__field">
                                        <label class="arraysubs-modal__label" for="shipping_country">
                                            <?php esc_html_e('Country', 'arraysubs'); ?> <span class="required">*</span>
                                        </label>
                                        <input type="text" id="shipping_country" name="country" class="arraysubs-modal__input" required
                                            value="<?php echo esc_attr($arraysubs_shipping_address['country'] ?? ''); ?>">
                                    </div>
                                </div>

                                <div class="arraysubs-shipping-form__row">
                                    <div class="arraysubs-shipping-form__field arraysubs-shipping-form__field--full">
                                        <label class="arraysubs-modal__label" for="shipping_phone">
                                            <?php esc_html_e('Phone', 'arraysubs'); ?>
                                        </label>
                                        <input type="tel" id="shipping_phone" name="phone" class="arraysubs-modal__input"
                                            value="<?php echo esc_attr($arraysubs_shipping_address['phone'] ?? ''); ?>">
                                    </div>
                                </div>
                            </form>
                        </div>

                        <div class="arraysubs-modal__footer">
                            <button type="button" class="arraysubs-modal__cancel-btn">
                                <?php esc_html_e('Cancel', 'arraysubs'); ?>
                            </button>
                            <button type="button" class="arraysubs-modal__confirm-btn arraysubs-shipping-save-btn">
                                <?php esc_html_e('Save Address', 'arraysubs'); ?>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        <?php endif; ?>

    </div>
</div>
