<?php
/**
 * Cancel Subscription Retention Modal.
 *
 * @var array $reasons         Cancellation reasons from settings.
 * @var array $settings        Full retention settings.
 * @var bool  $has_save_offers Whether save offers (discount, pause, downgrade) are available (Professional plan).
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
?>
<div id="strp-sub-retention-modal" class="strp-retention-overlay" style="display:none;" role="dialog" aria-modal="true" aria-labelledby="strp-retention-title">
    <div class="strp-retention-modal">
        <button type="button" class="strp-retention-close" aria-label="<?php esc_attr_e( 'Close', 'subscription-manager-for-stripe' ); ?>">&times;</button>

        <!-- Step 1: Reason Survey -->
        <div class="strp-retention-step" data-step="reason">
            <h2 id="strp-retention-title"><?php esc_html_e( "We're sorry to see you go.", 'subscription-manager-for-stripe' ); ?></h2>
            <p><?php esc_html_e( 'Before you cancel, could you tell us why?', 'subscription-manager-for-stripe' ); ?></p>
            <form class="strp-retention-reasons">
                <?php wp_nonce_field( 'strp_sub_retention_nonce', 'strp_sub_retention_nonce' ); ?>
                <?php foreach ( $reasons as $index => $reason ) : ?>
                    <label class="strp-retention-reason-option">
                        <input type="radio" name="cancel_reason" value="<?php echo esc_attr( $index ); ?>" />
                        <span><?php echo esc_html( $reason['label'] ); ?></span>
                    </label>
                    <?php if ( strtolower( $reason['label'] ) === strtolower( __( 'Other', 'subscription-manager-for-stripe' ) ) ) : ?>
                        <textarea class="strp-retention-other-text" name="cancel_reason_other" placeholder="<?php esc_attr_e( 'Please tell us more...', 'subscription-manager-for-stripe' ); ?>" style="display:none;"></textarea>
                    <?php endif; ?>
                <?php endforeach; ?>
            </form>
            <div class="strp-retention-actions">
                <button type="button" class="strp-retention-btn strp-retention-btn--primary" id="strp-retention-continue" disabled>
                    <?php esc_html_e( 'Continue', 'subscription-manager-for-stripe' ); ?>
                </button>
                <button type="button" class="strp-retention-btn strp-retention-btn--link strp-retention-close-btn">
                    <?php esc_html_e( 'Never mind', 'subscription-manager-for-stripe' ); ?>
                </button>
            </div>
        </div>

        <?php if ( ! empty( $has_save_offers ) ) : ?>
        <!-- Step 2: Save Offer (Professional plan only) -->
        <div class="strp-retention-step" data-step="offer" style="display:none;">
            <h2><?php esc_html_e( 'Before you go...', 'subscription-manager-for-stripe' ); ?></h2>
            <div class="strp-retention-offer-card" id="strp-retention-offer-content" aria-live="polite">
                <!-- Dynamically populated via AJAX response -->
            </div>
            <div class="strp-retention-actions">
                <button type="button" class="strp-retention-btn strp-retention-btn--success" id="strp-retention-accept">
                    <?php esc_html_e( 'Accept Offer', 'subscription-manager-for-stripe' ); ?>
                </button>
                <button type="button" class="strp-retention-btn strp-retention-btn--link" id="strp-retention-decline">
                    <?php esc_html_e( 'Cancel Anyway', 'subscription-manager-for-stripe' ); ?>
                </button>
            </div>
        </div>
        <?php endif; ?>

        <!-- Step 3: Confirm Cancellation -->
        <div class="strp-retention-step" data-step="confirm" style="display:none;">
            <h2><?php esc_html_e( 'Are you sure?', 'subscription-manager-for-stripe' ); ?></h2>
            <p><?php esc_html_e( 'Your subscription will be cancelled at the end of your current billing period. You can continue using the service until then.', 'subscription-manager-for-stripe' ); ?></p>
            <div class="strp-retention-actions">
                <button type="button" class="strp-retention-btn strp-retention-btn--danger" id="strp-retention-confirm">
                    <?php esc_html_e( 'Confirm Cancellation', 'subscription-manager-for-stripe' ); ?>
                </button>
                <button type="button" class="strp-retention-btn strp-retention-btn--link strp-retention-close-btn">
                    <?php esc_html_e( 'Keep My Subscription', 'subscription-manager-for-stripe' ); ?>
                </button>
            </div>
        </div>

        <!-- Step 4: Success (accepted offer or cancelled) -->
        <div class="strp-retention-step" data-step="success" style="display:none;">
            <div class="strp-retention-success-icon"><span class="dashicons dashicons-yes-alt"></span></div>
            <h2 id="strp-retention-success-title"></h2>
            <p id="strp-retention-success-message"></p>
            <div class="strp-retention-actions">
                <button type="button" class="strp-retention-btn strp-retention-btn--primary strp-retention-close-btn">
                    <?php esc_html_e( 'Done', 'subscription-manager-for-stripe' ); ?>
                </button>
            </div>
        </div>

        <div class="strp-retention-loading" style="display:none;">
            <div class="strp-retention-spinner"></div>
        </div>
    </div>
</div>
