<?php
/**
 * Template for displaying subscription plans
 */

if (empty($plans)) : ?>
    <p><?php esc_html_e('No subscription plans available.', 'subscription-system'); ?></p>
<?php else : ?>
    <div class="subscription-plans">
        <?php foreach ($plans as $plan) : ?>
            <div class="subscription-plan" data-plan-id="<?php echo esc_attr($plan['id']); ?>">
                <h3><?php echo esc_html($plan['name']); ?></h3>
                <div class="plan-price">
                    <?php 
                    /* translators: %s: price amount (e.g. "29.99") */
                    echo sprintf(
                        esc_html__('$%s', 'subscription-system'),
                        number_format($plan['price'], 2)
                    ); 
                    ?>
                </div>
                <div class="plan-description">
                    <?php echo wp_kses_post($plan['description']); ?>
                </div>
                <?php if (is_user_logged_in()) : ?>
                    <button class="subscribe-button" type="button">
                        <?php esc_html_e('Subscribe Now', 'subscription-system'); ?>
                    </button>
                <?php else : ?>
                    <a href="<?php echo esc_url(admin_url('admin-ajax.php')); ?>?action=login" class="login-link">
                        <?php esc_html_e('Login to Subscribe', 'subscription-system'); ?>
                    </a>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
    </div>

    <script>
    jQuery(document).ready(function($) {
        $('.subscribe-button').on('click', function() {
            var planId = $(this).closest('.subscription-plan').data('plan-id');
            
            $.ajax({
                type: 'POST',
                url: '<?php echo esc_url(admin_url('admin-ajax.php')); ?>',
                data: {
                    action: 'subscribe_to_plan',
                    plan_id: planId,
                    nonce: '<?php echo esc_attr(wp_create_nonce('subscribe_nonce')); ?>'
                },
                success: function(response) {
                    if (response.success) {
                        alert('<?php esc_html_e('Subscription successful!', 'subscription-system'); ?>');
                    } else {
                        alert('<?php esc_html_e('Subscription failed. Please try again.', 'subscription-system'); ?>');
                    }
                }
            });
        });
    });
    </script>
<?php endif; ?> 