<?php
/**
 * Template for displaying pricing plans (Layout 1)
 *
 * @package    Subscription_System
 */

// If this file is called directly, abort.
if (!defined('WPINC')) {
    die;
}

// Enqueue the layout-specific CSS
wp_enqueue_style('subscription-pricing-layout1', plugin_dir_url(dirname(__FILE__)) . 'css/pricing-layout1.css', array(), '1.0.0', 'all');

// Add responsive viewport meta tag if not already present
 

// Enqueue Stripe JS
wp_enqueue_script('stripe-js', 'https://js.stripe.com/v3/', array(), null, true);

// Enqueue our custom JS
wp_enqueue_script('subscription-pricing-js', plugin_dir_url(dirname(__FILE__)) . 'js/subscription-pricing.js', array('jquery', 'stripe-js'), '1.0.0', true);

// Localize script with necessary data
wp_localize_script('subscription-pricing-js', 'subscription_data', array(
    'ajax_url' => esc_url(admin_url('admin-ajax.php')),
    'stripe_publishable_key' => esc_attr(get_option('subscription_stripe_publishable_key')),
    'stripe_secret_key' => esc_attr(get_option('subscription_stripe_secret_key')),
    'is_user_logged_in' => is_user_logged_in(),
    'register_page_url' => esc_url(get_permalink(get_option('register_page'))),
    'account_page_url' => esc_url(get_permalink(get_option('account_page'))),
    'thank_you_page_url' => esc_url(get_permalink(get_option('thank_you_page'))),
    'pricing_plans_page_url' => esc_url(get_permalink(get_option('pricing_plans_page'))),
    'nonce' => esc_attr(wp_create_nonce('subscription_check_nonce')),
    'subscribe_now_text' => esc_html__('Subscribe Now', 'subscription-system')
));

 
?>

<div class="subscription-pricing-plans layout1 <?php echo esc_attr($orientation_class); ?> columns-<?php echo esc_attr($atts['columns']); ?> ss-responsive">
    <?php if (empty($plans)) : ?>
        <p class="no-plans"><?php esc_html_e('No subscription plans available.', 'subscription-system'); ?></p>
    <?php else : ?>
        <div class="ss-pricing-container ss-layout1">
            <div class="ss-pricing-plans">
                <?php 
                // Determine which plan should be featured (middle plan by default)
                $featured_index = ceil(count($plans) / 2) - 1;
                
                // Loop through plans
                foreach ($plans as $index => $plan) : 
                    $is_featured = ($index === $featured_index) ? 'ss-featured' : '';
                ?>
                    <div class="ss-pricing-plan <?php echo esc_attr($is_featured); ?>">
                        <div class="ss-plan-header">
                            <h3><?php echo esc_html($plan->plan_name); ?></h3>
                            <div class="ss-price">$<?php echo esc_html(number_format($plan->price, 2)); ?><span>/month</span></div>
                        </div>
                        <div class="ss-plan-features">
                            <ul>
                                <?php 
                                // If plan has features, display them
                                if (!empty($plan->description)) {
                                    // Clean up the description string - remove escaped quotes
                                    $clean_description = str_replace('\"', '"', $plan->description);
                                    
                                    // Try to decode the JSON
                                    $features = json_decode($clean_description);
                                    
                                    // If JSON decode failed, try other methods
                                    if ($features === null) {
                                        // Try to unserialize
                                        $features = maybe_unserialize($clean_description);
                                        
                                        // If still not an array, use as single item
                                        if (!is_array($features)) {
                                            $features = array($clean_description);
                                        }
                                    }
                                    
                                    // Display features
                                    if (is_array($features) || is_object($features)) {
                                        foreach ($features as $feature) {
                                            echo '<li>' . esc_html($feature) . '</li>';
                                        }
                                    } else {
                                        echo '<li>' . esc_html($clean_description) . '</li>';
                                    }
                                } else {
                                    echo '<!-- Debug: Plan description is empty -->';
                                }
                                ?>
                            </ul>
                        </div>
                        <div class="ss-plan-footer">
                            <button class="ss-button subscribe-button" data-plan-id="<?php echo esc_attr($plan->id); ?>">
                                <?php esc_html_e('Subscribe Now', 'subscription-system'); ?>
                            </button>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    <?php endif; ?>
</div>

<style>
/* Override styles to match admin preview */
.subscription-pricing-plans.layout1 {
    background: transparent !important;
    padding: 40px 20px;
    min-height: auto;
}

.subscription-pricing-plans.layout1 .ss-pricing-container {
    display: flex;
    justify-content: center;
    width: 100%;
    padding: 20px 0;
    background: transparent !important;
}

.subscription-pricing-plans.layout1 .ss-pricing-plans {
    display: flex;
    justify-content: center;
    max-width: 1200px;
    width: 100%;
    margin: 0 auto;
    gap: 20px;
    flex-wrap: wrap;
}

.subscription-pricing-plans.layout1 .ss-pricing-plan {
    flex: 1;
    max-width: 350px;
    min-width: 280px;
    background-color: transparent !important;
    border-radius: 8px;
    box-shadow: 0 5px 15px rgba(0,0,0,0.08);
    overflow: hidden;
    transition: all 0.3s ease;
    border: 1px solid #e0e0e0;
    position: relative;
}

.subscription-pricing-plans.layout1 .ss-pricing-plan:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 25px rgba(0,0,0,0.1);
}

.subscription-pricing-plans.layout1 .ss-pricing-plan.ss-featured {
    border: 2px solid #0073aa;
    transform: translateY(-10px);
    position: relative;
}

.subscription-pricing-plans.layout1 .ss-pricing-plan.ss-featured::after {
    content: 'POPULAR';
    position: absolute;
    top: 15px;
    right: 15px;
    background-color: #0073aa;
    color: white;
    padding: 5px 10px;
    font-size: 12px;
    border-radius: 20px;
    font-weight: bold;
}

.subscription-pricing-plans.layout1 .ss-plan-header {
    padding: 25px 20px;
    text-align: center;
    border-bottom: 1px solid #f0f0f0;
    position: relative;
}

.subscription-pricing-plans.layout1 .ss-plan-header h3 {
    margin: 0;
    font-size: 20px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #333;
}

.subscription-pricing-plans.layout1 .ss-featured .ss-plan-header h3 {
    color: #0073aa;
}

.subscription-pricing-plans.layout1 .ss-price {
    font-size: 34px;
    font-weight: bold;
    margin-top: 10px;
    color: #0073aa;
}

.subscription-pricing-plans.layout1 .ss-price span {
    font-size: 16px;
    font-weight: normal;
    opacity: 0.8;
}

.subscription-pricing-plans.layout1 .ss-plan-features {
    padding: 0;
}

.subscription-pricing-plans.layout1 .ss-plan-features ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.subscription-pricing-plans.layout1 .ss-plan-features li {
    padding: 15px 20px;
    border-bottom: 1px solid #f0f0f0;
    text-align: center;
    font-size: 14px;
    color: #555;
}

.subscription-pricing-plans.layout1 .ss-plan-footer {
    padding: 25px;
    text-align: center;
    background-color: transparent;
}

.subscription-pricing-plans.layout1 .ss-button {
    display: inline-block;
    padding: 12px 30px;
    border-radius: 50px;
    text-decoration: none;
    font-weight: 600;
    transition: all 0.3s ease;
    font-size: 15px;
    letter-spacing: 0.5px;
    text-transform: uppercase;
    background-color: #0073aa;
    color: white;
    border: none;
}

.subscription-pricing-plans.layout1 .ss-button:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(0,0,0,0.1);
    background-color: #005a87;
}

/* Responsive adjustments */
@media (max-width: 992px) {
    .subscription-pricing-plans.layout1 .ss-pricing-plans {
        flex-direction: column;
        align-items: center;
    }
    
    .subscription-pricing-plans.layout1 .ss-pricing-plan {
        width: 100%;
        max-width: 450px;
        margin-bottom: 20px;
    }
    
    .subscription-pricing-plans.layout1 .ss-pricing-plan.ss-featured {
        transform: none;
    }
}
</style> 