<?php
/**
 * Public subscription form display.
 *
 * @since      1.0.0
 * @package    Subscription_System
 */

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

// Get currency symbol
$currency_symbols = array(
    'USD' => '$',
    'EUR' => '€',
    'GBP' => '£',
    'JPY' => '¥',
    'AUD' => '$',
    'CAD' => '$'
);

$settings = get_option('ss_settings', array(
    'currency' => 'USD',
    'currency_position' => 'before'
));

$currency_symbol = isset($currency_symbols[$settings['currency']]) ? $currency_symbols[$settings['currency']] : $settings['currency'];

// Format price with currency
function ss_format_price($price, $currency_symbol, $position = 'before') {
    if ($position === 'before') {
        return $currency_symbol . $price;
    } else {
        return $price . $currency_symbol;
    }
}
?>

<div class="ss-form-container">
    <h2 class="ss-form-title">
        <?php
        if (isset($plan)) {
            /* translators: %s: name of the subscription plan */
            echo sprintf(esc_html__('Subscribe to %s', 'subscription-system'), esc_html($plan['name']));
        } else {
            esc_html_e('Subscribe', 'subscription-system');
        }
        ?>
    </h2>
    
    <form class="ss-subscription-form" data-button-text="<?php esc_attr_e('Subscribe', 'subscription-system'); ?>">
        <?php wp_nonce_field('ss_subscribe', 'ss_nonce'); ?>
        
        <div class="ss-message" style="display: none;"></div>
        
        <?php if (!isset($plan)) : ?>
            <div class="ss-form-row">
                <label for="plan_id"><?php esc_html_e('Select a Plan', 'subscription-system'); ?></label>
                <select id="plan_id" name="plan_id" required>
                    <option value=""><?php esc_html_e('Select a plan', 'subscription-system'); ?></option>
                    <?php foreach ($plans as $plan_id => $plan_data) : ?>
                        <option value="<?php echo esc_attr($plan_id); ?>">
                            <?php echo esc_html($plan_data['name'] . ' - ' . ss_format_price($plan_data['price'], $currency_symbol, $settings['currency_position'])); ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
        <?php else : ?>
            <input type="hidden" name="plan_id" value="<?php echo esc_attr($plan_id); ?>">
            
            <div class="ss-form-row">
                <p>
                    <?php
                    // translators: %1$s is the plan name, %2$s is the formatted price
                    echo sprintf(
                        esc_html__('You are subscribing to %1$s for %2$s.', 'subscription-system'),
                        '<strong>' . esc_html($plan['name']) . '</strong>',
                        '<strong>' . esc_html(ss_format_price($plan['price'], $currency_symbol, $settings['currency_position'])) . '</strong>'
                    );
                    
                    if ($plan['duration'] > 0) {
                        // translators: %d is the number of days the subscription will last
                        echo ' ' . esc_html(sprintf(_n('This subscription will last for %d day.', 'This subscription will last for %d days.', $plan['duration'], 'subscription-system'), $plan['duration']));
                    } else {
                        echo ' ' . esc_html__('This subscription has unlimited duration.', 'subscription-system');
                    }
                    ?>
                </p>
            </div>
        <?php endif; ?>
        
        <div class="ss-form-row">
            <label for="email"><?php esc_html_e('Email Address', 'subscription-system'); ?></label>
            <input type="email" id="email" name="email" required value="<?php echo is_user_logged_in() ? esc_attr(wp_get_current_user()->user_email) : ''; ?>">
        </div>
        
        <div class="ss-form-row">
            <button type="submit" class="ss-form-submit"><?php esc_html_e('Subscribe', 'subscription-system'); ?></button>
        </div>
    </form>
</div> 